Skip to content

Commit

Permalink
tracing: send traces to the otel collector when enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jsternberg committed Dec 12, 2023
1 parent 57dc457 commit 07b5add
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type bakeOptions struct {
func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
ctx := appcontext.Context()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "bake")
ctx, end, err := tracing.TraceCurrentCommand(ctx, dockerCli, "bake")
if err != nil {
return err
}
Expand Down
65 changes: 62 additions & 3 deletions util/tracing/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ package tracing

import (
"context"
"fmt"
"os"
"strings"

"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/tracing/detect"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

func TraceCurrentCommand(ctx context.Context, name string) (context.Context, func(error), error) {
tp, err := detect.TracerProvider()
const otelConfigFieldName = "otel"

func TraceCurrentCommand(ctx context.Context, cli command.Cli, name string) (context.Context, func(error), error) {
opts, err := tracerProviderOptions(cli)
if err != nil {
return context.Background(), nil, err
}

tp, err := detect.NewTracerProvider(ctx, opts...)
if err != nil {
return context.Background(), nil, err
}
Expand All @@ -25,6 +36,54 @@ func TraceCurrentCommand(ctx context.Context, name string) (context.Context, fun
}
span.End()

detect.Shutdown(context.TODO())
tp.Shutdown(context.TODO())
}, nil
}

func tracerProviderOptions(cli command.Cli) ([]sdktrace.TracerProviderOption, error) {
meta, err := cli.ContextStore().GetMetadata(cli.CurrentContext())
if err != nil {
return nil, err
}

var otelCfg interface{}
switch m := meta.Metadata.(type) {
case command.DockerContext:
otelCfg = m.AdditionalFields[otelConfigFieldName]
case map[string]interface{}:
otelCfg = m[otelConfigFieldName]
}

if otelCfg == nil {
return nil, nil
}

otelMap, ok := otelCfg.(map[string]interface{})
if !ok {
return nil, fmt.Errorf(
"unexpected type for field %q: %T (expected: %T)",
otelConfigFieldName,
otelCfg,
otelMap,
)
}

// keys from https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/
endpoint, _ := otelMap["OTEL_EXPORTER_OTLP_ENDPOINT"].(string)
if endpoint == "" {
return nil, nil
}

// Hardcoded endpoint from the endpoint.
exp, err := otlptracegrpc.New(context.Background(),
otlptracegrpc.WithEndpoint(endpoint),
otlptracegrpc.WithInsecure())
if err != nil {
return nil, err
}

sp := sdktrace.NewBatchSpanProcessor(exp)
return []sdktrace.TracerProviderOption{
sdktrace.WithSpanProcessor(sp),
}, nil
}

0 comments on commit 07b5add

Please sign in to comment.