Skip to content

Commit

Permalink
telemetry: add a bunch of span attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtore committed Sep 12, 2024
1 parent b816c3c commit 06696b1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 1 addition & 5 deletions pkg/deployclient/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"google.golang.org/grpc/codes"

"github.com/nais/deploy/pkg/hookd/logproxy"
Expand Down Expand Up @@ -182,10 +181,7 @@ func (d *Deployer) Deploy(ctx context.Context, cfg *Config, deployRequest *pb.De
log.Infof("Deployment request accepted by NAIS deploy and dispatched to cluster '%s'.", deployStatus.GetRequest().GetCluster())

deployRequest.ID = deployStatus.GetRequest().GetID()
rootSpan.SetAttributes(attribute.KeyValue{
Key: "correlation-id",
Value: attribute.StringValue(deployRequest.ID),
})
telemetry.AddDeploymentRequestSpanAttributes(rootSpan, deployStatus.GetRequest())

urlPrefix := "https://" + strings.Split(cfg.DeployServerURL, ":")[0]
log.Infof("Deployment information:")
Expand Down
12 changes: 12 additions & 0 deletions pkg/deployd/deployd/deployd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

nais_io_v1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,6 +68,17 @@ func Run(op *operation.Operation, client kubeclient.Interface) {
})

_, span := telemetry.Tracer().Start(op.Context, fmt.Sprintf("%s/%s", identifier.Kind, identifier.Name))
telemetry.AddDeploymentRequestSpanAttributes(span, op.Request)
span.SetAttributes(
attribute.KeyValue{
Key: "k8s.kind",
Value: attribute.StringValue(identifier.Kind),
},
attribute.KeyValue{
Key: "k8s.name",
Value: attribute.StringValue(identifier.Name),
},
)

resourceInterface, err := client.ResourceInterface(&resource)
if err == nil {
Expand Down
27 changes: 26 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"strings"
"time"

"github.com/nais/deploy/pkg/pb"
"github.com/nais/deploy/pkg/version"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -92,6 +94,29 @@ func TraceParentHeader(ctx context.Context) string {
return traceCarrier.Get(traceParentKey)
}

// Copies interesting values from the deployment request
// onto the span, so it can be filtered in Grafana.
func AddDeploymentRequestSpanAttributes(span otrace.Span, request *pb.DeploymentRequest) {
span.SetAttributes(
attribute.KeyValue{
Key: "deploy.id",
Value: attribute.StringValue(request.GetID()),
}, attribute.KeyValue{
Key: "deploy.cluster",
Value: attribute.StringValue(request.GetCluster()),
}, attribute.KeyValue{
Key: "deploy.team",
Value: attribute.StringValue(request.GetTeam()),
}, attribute.KeyValue{
Key: "deploy.git-ref-sha",
Value: attribute.StringValue(request.GetGitRefSha()),
}, attribute.KeyValue{
Key: "deploy.repository",
Value: attribute.StringValue(request.GetRepository().String()),
},
)
}

// Holds timestamps from pipeline indicating when certain steps were started or finished.
// If `Validate()` returns nil, this object is safe to use and contains chronologically ordered timestamps
// for every field.
Expand Down Expand Up @@ -143,7 +168,7 @@ func (pt *PipelineTimings) StartTracing(ctx context.Context, name string) (conte
// If all timing data is valid, returns a timings object and nil error.
func ParsePipelineTelemetry(s string) (*PipelineTimings, error) {

// Github makes it very difficult to have multiple equal signs in a single "echo ... > $GITHUB_OUTPUT" line.
// GitHub makes it very difficult to have multiple equal signs in a single "echo ... > $GITHUB_OUTPUT" line.
// We must quote it, but the quotes follow into the nais deploy client, and must be stripped here.
s = strings.Trim(s, `"`)

Expand Down

0 comments on commit 06696b1

Please sign in to comment.