Skip to content

Commit

Permalink
Add Service Layer Tracing
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <[email protected]>
  • Loading branch information
rodneyosodo committed Jul 25, 2023
1 parent a903ca2 commit c6f6912
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
9 changes: 9 additions & 0 deletions agent/tracing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Package tracing provides tracing instrumentation for cocos auth service.
//
// This package provides tracing middleware for cocos auth service.
// It can be used to trace incoming requests and add tracing capabilities to
// cocos auth service.
//
// For more details about tracing instrumentation refer
// to the documentation at https://docs.mainflux.io/tracing/.
package tracing
70 changes: 70 additions & 0 deletions agent/tracing/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tracing

import (
"context"

"github.com/ultravioletrs/agent/agent"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

var _ agent.Service = (*tracingMiddleware)(nil)

type tracingMiddleware struct {
tracer trace.Tracer
svc agent.Service
}

// New returns a new auth service with tracing capabilities.
func New(svc agent.Service, tracer trace.Tracer) agent.Service {
return &tracingMiddleware{tracer, svc}
}

func (tm *tracingMiddleware) Ping(secret string) (string, error) {
_, span := tm.tracer.Start(context.Background(), "ping", trace.WithAttributes(
attribute.String("secret", secret),
))
defer span.End()

return tm.svc.Ping(secret)
}

func (tm *tracingMiddleware) Run(ctx context.Context, cmp agent.Computation) (string, error) {
ctx, span := tm.tracer.Start(context.Background(), "run", trace.WithAttributes(
attribute.String("id", cmp.ID),
attribute.String("name", cmp.Name),
attribute.String("description", cmp.Description),
attribute.String("status", cmp.Status),
attribute.String("start_time", cmp.StartTime.String()),
attribute.String("end_time", cmp.EndTime.String()),
attribute.StringSlice("dataset_providers", cmp.DatasetProviders),
attribute.StringSlice("algorithm_providers", cmp.AlgorithmProviders),
attribute.StringSlice("result_consumers", cmp.ResultConsumers),
))
defer span.End()

return tm.svc.Run(ctx, cmp)
}

func (tm *tracingMiddleware) Algo(ctx context.Context, algorithm []byte) (string, error) {
ctx, span := tm.tracer.Start(context.Background(), "algo")
defer span.End()

return tm.svc.Algo(ctx, algorithm)
}

func (tm *tracingMiddleware) Data(ctx context.Context, dataset string) (string, error) {
ctx, span := tm.tracer.Start(context.Background(), "data", trace.WithAttributes(
attribute.String("dataset", dataset),
))
defer span.End()

return tm.svc.Data(ctx, dataset)
}

func (tm *tracingMiddleware) Result(ctx context.Context) ([]byte, error) {
ctx, span := tm.tracer.Start(context.Background(), "result")
defer span.End()

return tm.svc.Result(ctx)
}
7 changes: 5 additions & 2 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
"github.com/ultravioletrs/agent/agent/api"
agentgrpc "github.com/ultravioletrs/agent/agent/api/grpc"
httpapi "github.com/ultravioletrs/agent/agent/api/http"
"github.com/ultravioletrs/agent/agent/tracing"
"github.com/ultravioletrs/agent/internal"
"github.com/ultravioletrs/agent/internal/env"
"github.com/ultravioletrs/agent/internal/server"
grpcserver "github.com/ultravioletrs/agent/internal/server/grpc"
httpserver "github.com/ultravioletrs/agent/internal/server/http"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Expand Down Expand Up @@ -65,7 +67,7 @@ func main() {
agentTracer, agentCloser := initJaeger("agent", cfg.JaegerURL, logger)
defer agentCloser.Close()

svc := newService(cfg.Secret, logger)
svc := newService(cfg.Secret, logger, trace.NewNoopTracerProvider().Tracer(svcName))

var httpServerConfig = server.Config{Port: defSvcHTTPPort}
if err := env.Parse(&httpServerConfig, env.Options{Prefix: envPrefixHTTP}); err != nil {
Expand Down Expand Up @@ -124,12 +126,13 @@ func initJaeger(svcName, url string, logger mflog.Logger) (opentracing.Tracer, i
return tracer, closer
}

func newService(secret string, logger mflog.Logger) agent.Service {
func newService(secret string, logger mflog.Logger, tracer trace.Tracer) agent.Service {
svc := agent.New(secret)

svc = api.LoggingMiddleware(svc, logger)
counter, latency := internal.MakeMetrics(svcName, "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)

return svc
}

0 comments on commit c6f6912

Please sign in to comment.