Skip to content

Commit

Permalink
better tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 12, 2024
1 parent b9fa889 commit 9673754
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions impl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/TBD54566975/did-dht-method/config"
int "github.com/TBD54566975/did-dht-method/internal/util"
"github.com/TBD54566975/did-dht-method/pkg/dht"
"github.com/TBD54566975/did-dht-method/pkg/server"
"github.com/TBD54566975/did-dht-method/pkg/telemetry"
Expand Down Expand Up @@ -54,6 +55,8 @@ func run() error {

// set up telemetry
if cfg.ServerConfig.Telemetry {
// add trace hook to logrus
logrus.AddHook(&int.TraceHook{})
if err = telemetry.SetupTelemetry(ctx); err != nil {
logrus.WithContext(ctx).WithError(err).Fatal("error initializing telemetry")
}
Expand Down
2 changes: 1 addition & 1 deletion impl/internal/did/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestClient(t *testing.T) {
client, err := NewGatewayClient("https://diddht.tbddev.org")
client, err := NewGatewayClient("http://localhost:8305")

require.NoError(t, err)
require.NotNil(t, client)
Expand Down
33 changes: 33 additions & 0 deletions impl/internal/util/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package util

import (
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)

// TraceHook is a logrus hook that adds trace information to log entries
type TraceHook struct{}

func (h *TraceHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *TraceHook) Fire(entry *logrus.Entry) error {
ctx := entry.Context
if ctx == nil {
return nil
}

span := trace.SpanFromContext(ctx)
if !span.SpanContext().IsValid() {
return nil
}

traceID := span.SpanContext().TraceID().String()
spanID := span.SpanContext().SpanID().String()

entry.Data["traceID"] = traceID
entry.Data["spanID"] = spanID

return nil
}
5 changes: 5 additions & 0 deletions impl/pkg/server/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/TBD54566975/did-dht-method/pkg/telemetry"
)

type GetHealthCheckResponse struct {
Expand All @@ -25,6 +27,9 @@ const (
// @Success 200 {object} GetHealthCheckResponse
// @Router /health [get]
func Health(c *gin.Context) {
_, span := telemetry.GetTracer().Start(c, "HealthHTTP.Health")
defer span.End()

status := GetHealthCheckResponse{Status: HealthOK}
Respond(c, status, http.StatusOK)
}
11 changes: 9 additions & 2 deletions impl/pkg/server/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/TBD54566975/did-dht-method/internal/util"
"github.com/TBD54566975/did-dht-method/pkg/pkarr"
"github.com/TBD54566975/did-dht-method/pkg/service"
"github.com/TBD54566975/did-dht-method/pkg/telemetry"
)

// PkarrRouter is the router for the Pkarr API
Expand All @@ -39,6 +40,9 @@ func NewPkarrRouter(service *service.PkarrService) (*PkarrRouter, error) {
// @Failure 500 {string} string "Internal server error"
// @Router /{id} [get]
func (r *PkarrRouter) GetRecord(c *gin.Context) {
ctx, span := telemetry.GetTracer().Start(c, "PkarrHTTP.GetRecord")
defer span.End()

id := GetParam(c, IDParam)
if id == nil || *id == "" {
LoggingRespondErrMsg(c, "missing id param", http.StatusBadRequest)
Expand All @@ -56,7 +60,7 @@ func (r *PkarrRouter) GetRecord(c *gin.Context) {
return
}

resp, err := r.service.GetPkarr(c, *id)
resp, err := r.service.GetPkarr(ctx, *id)
if err != nil {
// TODO(gabe): provide a more maintainable way to handle custom errors
if strings.Contains(err.Error(), "spam") {
Expand Down Expand Up @@ -93,6 +97,9 @@ func (r *PkarrRouter) GetRecord(c *gin.Context) {
// @Failure 500 {string} string "Internal server error"
// @Router /{id} [put]
func (r *PkarrRouter) PutRecord(c *gin.Context) {
ctx, span := telemetry.GetTracer().Start(c, "PkarrHTTP.PutRecord")
defer span.End()

id := GetParam(c, IDParam)
if id == nil || *id == "" {
LoggingRespondErrMsg(c, "missing id param", http.StatusBadRequest)
Expand Down Expand Up @@ -132,7 +139,7 @@ func (r *PkarrRouter) PutRecord(c *gin.Context) {
return
}

if err = r.service.PublishPkarr(c, *id, *request); err != nil {
if err = r.service.PublishPkarr(ctx, *id, *request); err != nil {
LoggingRespondErrWithMsg(c, err, "failed to publish pkarr record", http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 9673754

Please sign in to comment.