Skip to content

Commit

Permalink
ref: add otel metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Nov 14, 2021
1 parent f626bbd commit c0358fc
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions pkg/badgerdb/client.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

0 comments on commit c0358fc

Please sign in to comment.