Skip to content

Commit

Permalink
feat: Tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Oct 3, 2023
1 parent 7b71448 commit deba1cb
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/observability/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package observability

Check failure on line 1 in pkg/observability/config.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/ethpandaops/xatu/pkg/observability

import (
"crypto/tls"

"github.com/ethpandaops/beacon/pkg/human"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
)

type TracingConfig struct {
Enabled bool `yaml:"enabled" default:"false"`
Endpoint string `yaml:"endpoint" default:""`
URLPath string `yaml:"urlPath" default:""`
Timeout human.Duration `yaml:"timeout" default:"15s"`
Compression bool `yaml:"compression" default:"true"`
Headers map[string]string `yaml:"headers"`
Insecure bool `yaml:"insecure" default:"false"`
Retry *otlptracehttp.RetryConfig `yaml:"retry"`
TLS *tls.Config `yaml:"tls"`
}

func (t *TracingConfig) Validate() error {
return nil
}

func (t *TracingConfig) AsOTelOpts() []otlptracehttp.Option {
var opts []otlptracehttp.Option

if t.Endpoint != "" {
opts = append(opts, otlptracehttp.WithEndpoint(t.Endpoint))
}

if t.URLPath != "" {
opts = append(opts, otlptracehttp.WithURLPath(t.URLPath))
}

if t.Timeout.Duration != 0 {
opts = append(opts, otlptracehttp.WithTimeout(t.Timeout.Duration))
}

if t.Compression {
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.GzipCompression))
} else {
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.NoCompression))
}

if len(t.Headers) > 0 {
opts = append(opts, otlptracehttp.WithHeaders(t.Headers))
}

if t.Insecure {
opts = append(opts, otlptracehttp.WithInsecure())
}

if t.Retry != nil && t.Retry.Enabled {
opts = append(opts, otlptracehttp.WithRetry(*t.Retry))
}

if t.TLS != nil {
opts = append(opts, otlptracehttp.WithTLSClientConfig(t.TLS))
}

return opts
}
81 changes: 81 additions & 0 deletions pkg/observability/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package observability

import (
"context"
"errors"
"fmt"

"github.com/ethpandaops/xatu/pkg/proto/xatu"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
otrace "go.opentelemetry.io/otel/trace"
)

func Tracer() otrace.Tracer {
return otel.GetTracerProvider().Tracer(fmt.Sprintf("%s/observability", xatu.Full()))
}

// SetupOTelSDK bootstraps the OpenTelemetry pipeline.
// If it does not return an error, make sure to call shutdown for proper cleanup.
func SetupOTelSDK(ctx context.Context, tracerProvider *trace.TracerProvider) (shutdown func(context.Context) error, err error) {
var shutdownFuncs []func(context.Context) error

// shutdown calls cleanup functions registered via shutdownFuncs.
// The errors from the calls are joined.
// Each registered cleanup will be invoked once.
shutdown = func(ctx context.Context) error {
var err error

for _, fn := range shutdownFuncs {
err = errors.Join(err, fn(ctx))

Check failure on line 34 in pkg/observability/tracing.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join

Check failure on line 34 in pkg/observability/tracing.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join

Check failure on line 34 in pkg/observability/tracing.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join

Check failure on line 34 in pkg/observability/tracing.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join

Check failure on line 34 in pkg/observability/tracing.go

View workflow job for this annotation

GitHub Actions / full_ci (1.19.x)

undefined: errors.Join
}

shutdownFuncs = nil

return err
}

shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown)
otel.SetTracerProvider(tracerProvider)

return shutdown, nil
}

func NewResource(serviceName, serviceVersion string) (*resource.Resource, error) {
res, err := resource.New(context.Background(),
resource.WithFromEnv(),
resource.WithProcess(),
resource.WithOS(),
resource.WithContainer(),
resource.WithHost())
if err != nil {
return nil, fmt.Errorf("creating resource: %w", err)
}

return resource.Merge(res,
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName(serviceName),
semconv.ServiceVersion(serviceVersion),
),
)
}

func NewHTTPTraceProvider(ctx context.Context, res *resource.Resource, opts ...otlptracehttp.Option) (*trace.TracerProvider, error) {
client := otlptracehttp.NewClient(opts...)

exporter, err := otlptrace.New(ctx, client)
if err != nil {
return nil, fmt.Errorf("creating OTLP trace exporter: %w", err)
}

traceProvider := trace.NewTracerProvider(
trace.WithBatcher(exporter),
trace.WithResource(res),
)

return traceProvider, nil
}
12 changes: 12 additions & 0 deletions pkg/proto/xatu/mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xatu

type Mode string

const (
ModeUnknown Mode = ""
ModeSentry Mode = "sentry"
ModeCannon Mode = "cannon"
ModeServer Mode = "server"
ModeMimicry Mode = "mimicry"
ModeDiscovery Mode = "discovery"
)

0 comments on commit deba1cb

Please sign in to comment.