Skip to content

Commit

Permalink
chore: Move otel helpers into their own package
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok committed Sep 12, 2023
1 parent 0657ec0 commit cfdb892
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
32 changes: 12 additions & 20 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"os"

"dagger.io/dagger"
"github.com/grafana/grafana-build/otel"
"github.com/grafana/grafana-build/pipelines"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
)

var app = &cli.App{
Expand Down Expand Up @@ -47,38 +46,34 @@ func PipelineAction(pf pipelines.PipelineFunc) cli.ActionFunc {
}
client, err := dagger.Connect(ctx, opts...)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to connect to Dagger")
otel.RecordFailed(span, err, "failed to connect to Dagger")
return err
}
defer client.Close()

args, err := pipelines.PipelineArgsFromContext(ctx, c)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to load arguments")
otel.RecordFailed(span, err, "failed to load arguments")
return err
}

grafanaDir, err := args.GrafanaOpts.Grafana(ctx, client)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to load grafana directory")
otel.RecordFailed(span, err, "failed to load grafana directory")
return err
}

v, err := args.GrafanaOpts.DetectVersion(ctx, client, grafanaDir)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to detect version")
otel.RecordFailed(span, err, "failed to detect version")
return err
}
args.GrafanaOpts.Version = v
pipelines.InjectPipelineArgsIntoSpan(span, args)

if err := pf(ctx, client, grafanaDir, args); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "pipeline failed")
otel.RecordFailed(span, err, "pipeline failed")
return err
}
return nil
Expand All @@ -98,29 +93,26 @@ func PipelineActionWithPackageInput(pf pipelines.PipelineFuncWithPackageInput) c
}
client, err := dagger.Connect(ctx, opts...)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to connect to Dagger")
otel.RecordFailed(span, err, "failed to connect to Dagger")
return err
}
defer client.Close()

args, err := pipelines.PipelineArgsFromContext(ctx, c)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to load arguments")
otel.RecordFailed(span, err, "failed to load arguments")
return err
}

pipelines.InjectPipelineArgsIntoSpan(span, args)

if len(args.PackageInputOpts.Packages) == 0 {
span.SetStatus(codes.Error, "no package provided")
otel.RecordFailed(span, err, "no package provided")
return errors.New("expected at least one package from a '--package' flag")
}

if err := pf(ctx, client, args); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "pipeline failed")
otel.RecordFailed(span, err, "pipeline failed")
return err
}
return nil
Expand All @@ -129,8 +121,8 @@ func PipelineActionWithPackageInput(pf pipelines.PipelineFuncWithPackageInput) c

func main() {
ctx := context.Background()
shutdown := setupOTEL(ctx)
if err := app.RunContext(findParentTrace(ctx), os.Args); err != nil {
shutdown := otel.Setup(ctx)
if err := app.RunContext(otel.FindParentTrace(ctx), os.Args); err != nil {
if err := shutdown(context.Background()); err != nil {
log.Printf("Failed to shutdown tracer: %s", err.Error())
}
Expand Down
18 changes: 15 additions & 3 deletions cmd/otel.go → otel/otel.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main
package otel

import (
"context"
"log"
"os"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
Expand All @@ -15,7 +16,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

func setupOTEL(ctx context.Context) func(context.Context) error {
func Setup(ctx context.Context) func(context.Context) error {
client := otlptracehttp.NewClient()
exporter, err := otlptrace.New(ctx, client)
if err != nil {
Expand All @@ -40,7 +41,7 @@ func setupOTEL(ctx context.Context) func(context.Context) error {
return tp.Shutdown
}

func findParentTrace(ctx context.Context) context.Context {
func FindParentTrace(ctx context.Context) context.Context {
traceParent := os.Getenv("TRACEPARENT")
if traceParent == "" {
return ctx
Expand All @@ -56,3 +57,14 @@ func findParentTrace(ctx context.Context) context.Context {
}
return trace.ContextWithRemoteSpanContext(ctx, span.SpanContext())
}

// Tracer is a simple wrapper around otel.Tracer in order to abstract that
// package.
func Tracer(name string) trace.Tracer {
return otel.Tracer(name)
}

func RecordFailed(span trace.Span, err error, msg string) {
span.RecordError(err)
span.SetStatus(codes.Error, msg)
}
24 changes: 6 additions & 18 deletions pipelines/pipeline_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,12 @@ func InjectPipelineArgsIntoSpan(span trace.Span, args PipelineArgs) {
attributes := make([]attribute.KeyValue, 0, 10)
attributes = append(attributes, attribute.String("platform", string(args.Platform)))
if args.GrafanaOpts != nil {
if args.GrafanaOpts.GoVersion != "" {
attributes = append(attributes, attribute.String("go-version", args.GrafanaOpts.GoVersion))
}
if args.GrafanaOpts.Version != "" {
attributes = append(attributes, attribute.String("version", args.GrafanaOpts.Version))
}
if args.GrafanaOpts.GrafanaDir != "" {
attributes = append(attributes, attribute.String("grafana-dir", args.GrafanaOpts.GrafanaDir))
}
if args.GrafanaOpts.GrafanaRef != "" {
attributes = append(attributes, attribute.String("grafana-ref", args.GrafanaOpts.GrafanaRef))
}
if args.GrafanaOpts.EnterpriseDir != "" {
attributes = append(attributes, attribute.String("enterprise-dir", args.GrafanaOpts.EnterpriseDir))
}
if args.GrafanaOpts.EnterpriseRef != "" {
attributes = append(attributes, attribute.String("enterprise-ref", args.GrafanaOpts.EnterpriseRef))
}
attributes = append(attributes, attribute.String("go-version", args.GrafanaOpts.GoVersion))
attributes = append(attributes, attribute.String("version", args.GrafanaOpts.Version))
attributes = append(attributes, attribute.String("grafana-dir", args.GrafanaOpts.GrafanaDir))
attributes = append(attributes, attribute.String("grafana-ref", args.GrafanaOpts.GrafanaRef))
attributes = append(attributes, attribute.String("enterprise-dir", args.GrafanaOpts.EnterpriseDir))
attributes = append(attributes, attribute.String("enterprise-ref", args.GrafanaOpts.EnterpriseRef))
}
if args.PackageOpts != nil {
distros := []string{}
Expand Down

0 comments on commit cfdb892

Please sign in to comment.