mirror of
https://github.com/compute-blade-community/compute-blade-agent.git
synced 2026-04-21 17:45:43 +02:00
feat(OpenTelemetry): Integrate OpenTelemetry into agent (#90)
* feat(OpenTelemetry): Integrate OpenTelemetry into agent - integrate OpenTelemetry logging with zap logger for better observability - add OpenTelemetry gRPC middleware for enhanced tracing capabilities - document new OTLP exporter endpoint for better configuration guidance * docs: document OTEL env var --------- Co-authored-by: Cedric Kienzler <cedric@specht-labs.de>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/events"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/spechtlabs/go-otel-utils/otelzap"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -63,7 +64,7 @@ func (s *computebladeStateImpl) RegisterEvent(event events.Event) {
|
||||
s.criticalConfirmChan = make(chan struct{})
|
||||
|
||||
default:
|
||||
zap.L().Warn("Unknown event", zap.String("event", event.String()))
|
||||
otelzap.L().Warn("Unknown event", zap.String("event", event.String()))
|
||||
}
|
||||
|
||||
// Set identify state metric
|
||||
|
||||
@@ -199,7 +199,7 @@ func (bcm *bcm2711) setup(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
log.FromContext(ctx).Info("no smart fan unit detected, assuming standard fan unit", zap.Error(err))
|
||||
log.FromContext(ctx).WithError(err).Info("no smart fan unit detected, assuming standard fan unit")
|
||||
// FAN PWM output for standard fan unit (GPIO 12)
|
||||
// -> bcm2711RegGpfsel1 8:6, alt0
|
||||
bcm.gpioMem[bcm2711RegGpfsel1] = (bcm.gpioMem[bcm2711RegGpfsel1] &^ (0b111 << 6)) | (0b100 << 6)
|
||||
@@ -258,7 +258,7 @@ func (bcm *bcm2711) WaitForEdgeButtonPress(parentCtx context.Context) error {
|
||||
go func() {
|
||||
err := bcm.fanUnit.WaitForButtonPress(ctx)
|
||||
if err != nil && err != context.Canceled {
|
||||
log.FromContext(ctx).Error("failed to wait for button press", zap.Error(err))
|
||||
log.FromContext(ctx).WithError(err).Error("failed to wait for button press")
|
||||
} else {
|
||||
close(fanUnitChan)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/hal/led"
|
||||
"github.com/spechtlabs/go-otel-utils/otelzap"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -19,7 +20,7 @@ type SimulatedHal struct {
|
||||
}
|
||||
|
||||
func NewCm4Hal(_ context.Context, _ ComputeBladeHalOpts) (ComputeBladeHal, error) {
|
||||
logger := zap.L().Named("hal").Named("simulated-cm4")
|
||||
logger := otelzap.L().Named("hal").Named("simulated-cm4")
|
||||
logger.Warn("Using simulated hal")
|
||||
|
||||
computeModule.WithLabelValues("simulated").Set(1)
|
||||
|
||||
@@ -4,9 +4,10 @@ package hal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/log"
|
||||
"go.uber.org/zap"
|
||||
"math"
|
||||
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/hal/led"
|
||||
"github.com/warthog618/gpiod"
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/smartfanunit"
|
||||
"github.com/compute-blade-community/compute-blade-agent/pkg/smartfanunit/proto"
|
||||
"go.bug.st/serial"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@@ -32,7 +31,7 @@ func SmartFanUnitPresent(ctx context.Context, portName string) (bool, error) {
|
||||
defer func(rwc serial.Port) {
|
||||
err := rwc.Close()
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Warn("Error while closing serial port", zap.Error(err))
|
||||
log.FromContext(ctx).WithError(err).Warn("Error while closing serial port")
|
||||
}
|
||||
}(rwc)
|
||||
|
||||
@@ -42,7 +41,7 @@ func SmartFanUnitPresent(ctx context.Context, portName string) (bool, error) {
|
||||
log.FromContext(ctx).Warn("Closing serial port")
|
||||
err := rwc.Close()
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Warn("Error while closing serial port", zap.Error(err))
|
||||
log.FromContext(ctx).WithError(err).Warn("Error while closing serial port")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -117,7 +116,7 @@ func (fuc *smartFanUnit) Run(parentCtx context.Context) error {
|
||||
|
||||
pkt, err := proto.ReadPacket(ctx, fuc.rwc)
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Error("Failed to read packet from serial port", zap.Error(err))
|
||||
log.FromContext(ctx).WithError(err).Error("Failed to read packet from serial port")
|
||||
continue
|
||||
}
|
||||
fuc.eb.Publish(inboundTopic, pkt)
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
|
||||
"github.com/spechtlabs/go-otel-utils/otelzap"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// InterceptorLogger adapts zap logger to interceptor logger.
|
||||
// This code is simple enough to be copied and not imported.
|
||||
func InterceptorLogger(l *zap.Logger) logging.Logger {
|
||||
func InterceptorLogger(l *otelzap.Logger) logging.Logger {
|
||||
return logging.LoggerFunc(func(ctx context.Context, lvl logging.Level, msg string, fields ...any) {
|
||||
f := make([]zap.Field, 0, len(fields)/2)
|
||||
|
||||
@@ -24,12 +25,14 @@ func InterceptorLogger(l *zap.Logger) logging.Logger {
|
||||
f = append(f, zap.Int(key.(string), v))
|
||||
case bool:
|
||||
f = append(f, zap.Bool(key.(string), v))
|
||||
case zap.Field:
|
||||
f = append(f, v)
|
||||
default:
|
||||
f = append(f, zap.Any(key.(string), v))
|
||||
}
|
||||
}
|
||||
|
||||
logger := zap.L().WithOptions(zap.AddCallerSkip(4)).With(f...)
|
||||
logger := l.WithOptions(zap.AddCallerSkip(4)).With(f...)
|
||||
|
||||
switch lvl {
|
||||
case logging.LevelDebug:
|
||||
|
||||
@@ -3,21 +3,22 @@ package log
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/spechtlabs/go-otel-utils/otelzap"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type logCtxKey int
|
||||
|
||||
func IntoContext(ctx context.Context, logger *zap.Logger) context.Context {
|
||||
func IntoContext(ctx context.Context, logger *otelzap.Logger) context.Context {
|
||||
return context.WithValue(ctx, logCtxKey(0), logger)
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) *zap.Logger {
|
||||
func FromContext(ctx context.Context) *otelzap.Logger {
|
||||
val := ctx.Value(logCtxKey(0))
|
||||
if val != nil {
|
||||
return val.(*zap.Logger)
|
||||
return val.(*otelzap.Logger)
|
||||
}
|
||||
|
||||
zap.L().WithOptions(zap.AddCallerSkip(1)).Warn("No logger in context, passing default")
|
||||
return zap.L()
|
||||
otelzap.L().WithOptions(zap.AddCallerSkip(1)).Warn("No logger in context, passing default")
|
||||
return otelzap.L()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user