Skip to content

Commit

Permalink
feat: sender trace parent videre i deployment request
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Tore Jensen <kimtjen@gmail.com>
  • Loading branch information
Kyrremann and kimtore committed Sep 9, 2024
1 parent 413da33 commit ae02c3e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cmd/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nais/deploy/pkg/pb"
"github.com/nais/deploy/pkg/telemetry"
"github.com/nais/deploy/pkg/version"
"go.opentelemetry.io/otel/attribute"
"google.golang.org/protobuf/encoding/protojson"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -70,6 +71,11 @@ func run() error {
return err
}

rootSpan.SetAttributes(attribute.KeyValue{
Key: "correlation-id",
Value: attribute.StringValue(request.ID),
})

// Set up asynchronous gRPC connection
grpcConnection, err := deployclient.NewGrpcConnection(*cfg)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployclient/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/nais/deploy/pkg/hookd/logproxy"
"github.com/nais/deploy/pkg/pb"
"github.com/nais/deploy/pkg/telemetry"
)

type TemplateVariables map[string]interface{}
Expand Down Expand Up @@ -141,7 +142,9 @@ func Prepare(ctx context.Context, cfg *Config) (*pb.DeploymentRequest, error) {

deadline, _ := ctx.Deadline()

return MakeDeploymentRequest(*cfg, deadline, kube), nil
traceParent := telemetry.TraceParentHeader(ctx)

return MakeDeploymentRequest(*cfg, traceParent, deadline, kube), nil
}

func (d *Deployer) Deploy(ctx context.Context, cfg *Config, deployRequest *pb.DeploymentRequest) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/deployclient/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func makeMockDeployRequest(cfg deployclient.Config) *pb.DeploymentRequest {
tm := time.Now()
deadline := time.Now().Add(1 * time.Minute)
request := deployclient.MakeDeploymentRequest(cfg, deadline, &pb.Kubernetes{})
request := deployclient.MakeDeploymentRequest(cfg, "", deadline, &pb.Kubernetes{})
request.Time = pb.TimeAsTimestamp(tm)
return request
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/deployclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/nais/deploy/pkg/pb"
)

func MakeDeploymentRequest(cfg Config, deadline time.Time, kubernetes *pb.Kubernetes) *pb.DeploymentRequest {
func MakeDeploymentRequest(cfg Config, traceParent string, deadline time.Time, kubernetes *pb.Kubernetes) *pb.DeploymentRequest {
return &pb.DeploymentRequest{
Cluster: cfg.Cluster,
Deadline: pb.TimeAsTimestamp(deadline),
Expand All @@ -17,7 +17,8 @@ func MakeDeploymentRequest(cfg Config, deadline time.Time, kubernetes *pb.Kubern
Owner: cfg.Owner,
Name: cfg.Repository,
},
Team: cfg.Team,
Time: pb.TimeAsTimestamp(time.Now()),
Team: cfg.Team,
Time: pb.TimeAsTimestamp(time.Now()),
TraceParent: traceParent,
}
}
15 changes: 14 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ func Tracer() otrace.Tracer {
return tracer.Tracer("")
}

// TraceParentHeader extract the trace parent header value from the context
//
// Example of a trace parent:
//
// Version - Trace ID - Span ID - Inc:
// `00-3b03c24a4efad25e514890c874dc9e33-59c10f1945da62ca-01`
func TraceParentHeader(ctx context.Context) string {
traceCarrier := propagation.MapCarrier{}
traceCtx := propagation.TraceContext{}
traceCtx.Inject(ctx, traceCarrier)
return traceCarrier.Get("traceparent")
}

func newPropagator() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
Expand All @@ -72,7 +85,7 @@ func newPropagator() propagation.TextMapPropagator {

func newTraceProvider(ctx context.Context, res *resource.Resource, endpointURL string) (*trace.TracerProvider, error) {
// When debugging, you can replace the existing exporter with this one to get JSON on stdout.
//traceExporter, err := stdouttrace.New()
// traceExporter, err := stdouttrace.New()

traceExporter, err := otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(endpointURL))
if err != nil {
Expand Down

0 comments on commit ae02c3e

Please sign in to comment.