diff --git a/pkg/badgerdb/client.go b/pkg/badgerdb/client.go index 6311271..e1ce5ce 100644 --- a/pkg/badgerdb/client.go +++ b/pkg/badgerdb/client.go @@ -4,13 +4,9 @@ import ( "context" badger "github.com/dgraph-io/badger/v3" - "github.com/kenriortega/ngonx/pkg/errors" - "github.com/kenriortega/ngonx/pkg/logger" + "github.com/kenriortega/ngonx/pkg/otelify" "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" - "go.uber.org/zap" ) var pathDB = "./badger.data" @@ -32,21 +28,12 @@ func GetBadgerDB(ctx context.Context, embedMem bool) *badger.DB { db, err := badger.Open(opt) if err != nil { - span.RecordError(err) - span.SetStatus(codes.Error, err.Error()) - logger.LogError( - errors.Errorf("badger: %v", err).Error(), - zap.String("traceID", traceID), - ) - + otelify.InstrumentedError(span, "badger", traceID, err) panic(err) } clientBadger = db - logger.LogInfo( - "proxy.GetBadgerDB", - zap.String("traceID", traceID), - ) - // defer clientBadger.Close() - span.SetAttributes(attribute.String("badgerdb.create.client", "Success")) + + otelify.InstrumentedInfo(span, "proxy.GetBadgerDB", traceID) + return clientBadger } diff --git a/pkg/otelify/otelify.go b/pkg/otelify/otelify.go index 461ecc3..77e7815 100644 --- a/pkg/otelify/otelify.go +++ b/pkg/otelify/otelify.go @@ -4,10 +4,15 @@ import ( "context" "log" + "github.com/kenriortega/ngonx/pkg/errors" + "github.com/kenriortega/ngonx/pkg/logger" + "go.opentelemetry.io/otel/trace" + "go.uber.org/zap" "google.golang.org/grpc" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" @@ -65,3 +70,20 @@ func NewResource(name, version, namEnv string) *resource.Resource { ) return r } + +func InstrumentedError(span trace.Span, source, traceID string, err error) { + span.RecordError(err) + span.SetStatus(codes.Error, err.Error()) + logger.LogError( + errors.Errorf("%s: %v", source, err).Error(), + zap.String("traceID", traceID), + ) +} + +func InstrumentedInfo(span trace.Span, source, traceID string) { + logger.LogInfo( + source, + zap.String("traceID", traceID), + ) + span.SetAttributes(attribute.String(source, "Success")) +} diff --git a/pkg/otelify/prometheus.go b/pkg/otelify/prometheus.go index 14fb9a4..4e2a469 100644 --- a/pkg/otelify/prometheus.go +++ b/pkg/otelify/prometheus.go @@ -2,9 +2,9 @@ package otelify import ( "fmt" - "log" "net/http" + "github.com/kenriortega/ngonx/pkg/logger" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -20,5 +20,5 @@ var MetricRequestLatencyProxy = promauto.NewHistogram(prometheus.HistogramOpts{ func ExposeMetricServer(configPort int) { http.Handle("/metrics", promhttp.Handler()) port := fmt.Sprintf(":%d", configPort) - log.Fatal(http.ListenAndServe(port, nil)) + logger.LogError(http.ListenAndServe(port, nil).Error()) }