-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / lint
Check failure on line 34 in pkg/observability/tracing.go GitHub Actions / lint
Check failure on line 34 in pkg/observability/tracing.go GitHub Actions / lint
Check failure on line 34 in pkg/observability/tracing.go GitHub Actions / lint
|
||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |