From c0358fcaab6f83f5bff12991790282ed8f94462d Mon Sep 17 00:00:00 2001 From: kenriortega Date: Sat, 13 Nov 2021 20:38:24 -0500 Subject: [PATCH] ref: add otel metrics --- pkg/badgerdb/client.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/badgerdb/client.go b/pkg/badgerdb/client.go index fc9ccfc..6311271 100644 --- a/pkg/badgerdb/client.go +++ b/pkg/badgerdb/client.go @@ -1,16 +1,27 @@ package badgerdb import ( + "context" + badger "github.com/dgraph-io/badger/v3" "github.com/kenriortega/ngonx/pkg/errors" "github.com/kenriortega/ngonx/pkg/logger" + "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" // GetBadgerDB return `*badger.DB` // this client provide GET and SAVE methods -func GetBadgerDB(embedMem bool) *badger.DB { +func GetBadgerDB(ctx context.Context, embedMem bool) *badger.DB { + ctx, span := otel.Tracer("badger.client").Start(ctx, "GetBadgerDB") + defer span.End() + traceID := trace.SpanContextFromContext(ctx).TraceID().String() + var opt badger.Options var clientBadger *badger.DB if embedMem { @@ -21,12 +32,21 @@ func GetBadgerDB(embedMem bool) *badger.DB { db, err := badger.Open(opt) if err != nil { - logger.LogError(errors.Errorf("badger: %v", err).Error()) + span.RecordError(err) + span.SetStatus(codes.Error, err.Error()) + logger.LogError( + errors.Errorf("badger: %v", err).Error(), + zap.String("traceID", traceID), + ) panic(err) } clientBadger = db + logger.LogInfo( + "proxy.GetBadgerDB", + zap.String("traceID", traceID), + ) // defer clientBadger.Close() - + span.SetAttributes(attribute.String("badgerdb.create.client", "Success")) return clientBadger }