Skip to content

Commit

Permalink
feat: rework otel resource initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 28, 2025
1 parent 8e36af1 commit cefec7d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ Implements automatic setup of observability and daemonization based on environme

Metrics and pprof can be served from same address if needed, set both addresses to the same value.

### Example

```.dotenv
OTEL_LOG_LEVEL=debug
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
OTEL_EXPORTER_OTLP_INSECURE=true
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4317
OTEL_RESOURCE_ATTRIBUTES=service.name=go-faster.oteldb
# metrics exporter
OTEL_METRIC_EXPORT_INTERVAL=10000
OTEL_METRIC_EXPORT_TIMEOUT=5000
# pyroscope
PYROSCOPE_URL=http://127.0.0.1:4040
# should be same as service.name
PYROSCOPE_APP_NAME=go-faster.oteldb
PYROSCOPE_ENABLE=true
# use new metrics
OTEL_GO_X_DEPRECATED_RUNTIME_METRICS=false
# generate instance id
OTEL_GO_X_RESOURCE=true
```

### Reference

| Name | Description | Example | Default |
|---------------------------------------|----------------------------------|-------------------------|------------------------|
| `AUTOMAXPROCS` | Use [automaxprocs][automaxprocs] | `0` | `1` |
Expand Down
25 changes: 21 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/go-faster/errors"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -46,10 +47,26 @@ func Go(f func(ctx context.Context, t *Telemetry) error, op ...Option) {
func Run(f func(ctx context.Context, lg *zap.Logger, m *Telemetry) error, op ...Option) {
// Apply options.
opts := options{
zapConfig: zap.NewProductionConfig(),
zapTee: true,
ctx: context.Background(),
resourceFn: Resource,
zapConfig: zap.NewProductionConfig(),
zapTee: true,
ctx: context.Background(),
resourceOptions: []resource.Option{
resource.WithProcessRuntimeDescription(),
resource.WithProcessRuntimeVersion(),
resource.WithProcessRuntimeName(),
resource.WithOS(),
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithProcess(),
},
}
opts.resourceFn = func(ctx context.Context) (*resource.Resource, error) {
r, err := resource.New(ctx, opts.resourceOptions...)
if err != nil {
return nil, errors.Wrap(err, "new")
}
return resource.Merge(resource.Default(), r)
}
if v, err := strconv.ParseBool(os.Getenv("OTEL_ZAP_TEE")); err == nil {
// Override default.
Expand Down
35 changes: 30 additions & 5 deletions app/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
"go.uber.org/zap"

"github.com/go-faster/sdk/autologs"
Expand All @@ -17,10 +18,11 @@ type options struct {
zapTee bool
ctx context.Context

meterOptions []autometer.Option
tracerOptions []autotracer.Option
loggerOptions []autologs.Option
resourceFn func(ctx context.Context) (*resource.Resource, error)
meterOptions []autometer.Option
tracerOptions []autotracer.Option
loggerOptions []autologs.Option
resourceOptions []resource.Option
resourceFn func(ctx context.Context) (*resource.Resource, error)
}

type optionFunc func(*options)
Expand Down Expand Up @@ -69,6 +71,29 @@ func WithTracerOptions(opts ...autotracer.Option) Option {
})
}

// WithResourceOptions sets the default resource options.
//
// Use before [WithResource] or [WithServiceName] to override default resource options.
func WithResourceOptions(opts ...resource.Option) Option {
return optionFunc(func(o *options) {
o.resourceOptions = opts
})
}

// WithServiceName sets the default service name for the application.
func WithServiceName(name string) Option {
return optionFunc(func(o *options) {
o.resourceOptions = append(o.resourceOptions, resource.WithAttributes(semconv.ServiceName(name)))
})
}

// WithServiceNamespace sets the default service namespace for the application.
func WithServiceNamespace(namespace string) Option {
return optionFunc(func(o *options) {
o.resourceOptions = append(o.resourceOptions, resource.WithAttributes(semconv.ServiceNamespace(namespace)))
})
}

// WithContext sets the base context for the application. Background context is used by default.
func WithContext(ctx context.Context) Option {
return optionFunc(func(o *options) {
Expand All @@ -78,7 +103,7 @@ func WithContext(ctx context.Context) Option {

// WithResource sets the function that will be called to retrieve telemetry resource for application.
//
// Defaults to [Resource] function.
// Defaults to function that enables most common resource detectors.
func WithResource(fn func(ctx context.Context) (*resource.Resource, error)) Option {
return optionFunc(func(o *options) {
o.resourceFn = fn
Expand Down
2 changes: 2 additions & 0 deletions app/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
// - ProcessRuntimeVersion
// - ProcessRuntimeName
// And merges it with default resource.
//
// Deprecated: use [WithResourceOptions], [WithServiceName], [WithServiceNamespace].
func Resource(ctx context.Context) (*resource.Resource, error) {
opts := []resource.Option{
resource.WithProcessRuntimeDescription(),
Expand Down
19 changes: 18 additions & 1 deletion cmd/sdk-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@ func main() {
// Set base context. Background context is used by default.
app.WithContext(context.Background()),

// Allows to set custom resource.
// Set default service name and namespace.
// Incompatible with [app.WithResource].
app.WithServiceName("example"),
app.WithServiceNamespace("sdk"),

// Set default resource options.
app.WithResourceOptions(
resource.WithProcessRuntimeDescription(),
resource.WithProcessRuntimeVersion(),
resource.WithProcessRuntimeName(),
resource.WithOS(),
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithProcess(),
),

// Also allows to set custom resource.
app.WithResource(func(ctx context.Context) (*resource.Resource, error) {
return resource.Default(), nil
}),
Expand Down

0 comments on commit cefec7d

Please sign in to comment.