Stage 12: observability & performance (OTel/OTLP, domain metrics, guest GC)
- pkg/telemetry: shared OTel provider bootstrap (none/stdout/otlp + W3C
propagators + Go runtime metrics); backend/internal/telemetry becomes a thin
facade keeping its gin middleware.
- Telemetry parity: gateway and the Telegram connector gain telemetry runtimes
and config (GATEWAY_/TELEGRAM_ SERVICE_NAME + OTEL_*); otelgrpc instruments the
backend push server, the gateway's backend+connector clients and the connector
server. Default exporter stays none (collector/dashboards are Stage 14).
- Operational metrics (variant attribute on game-scoped ones): game_replay_duration,
game_move_validate_duration, games_started_total, games_abandoned_total,
game_cache_active, chat_messages_total{kind}, gateway edge_request_duration.
Wired via the SetMetrics setter pattern (default no-op meter).
- TODO-3: account.GuestReaper deletes guests with no game seat past
BACKEND_GUEST_RETENTION (default 30d, swept every BACKEND_GUEST_REAP_INTERVAL).
- Tests: pkg/telemetry exporter selection; game/social/edge metric recording via
a manual reader; config (otlp accepted, guest knobs); inttest guest reaper.
- Docs: PLAN.md re-scopes Stage 12 and adds Stage 13 (alphabet-on-wire) + Stage 14
(CI/deploy) with the agreed dictionary-versioning resolution; ARCHITECTURE 11/13,
TESTING, the three READMEs and FUNCTIONAL(+ru) updated.
This commit is contained in:
+20
-2
@@ -4,15 +4,33 @@ go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/google/flatbuffers v23.5.26+incompatible
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.68.0
|
||||
go.opentelemetry.io/otel v1.43.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.43.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.43.0
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.43.0
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0
|
||||
go.opentelemetry.io/otel/metric v1.43.0
|
||||
go.opentelemetry.io/otel/sdk v1.43.0
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0
|
||||
go.opentelemetry.io/otel/trace v1.43.0
|
||||
google.golang.org/grpc v1.80.0
|
||||
google.golang.org/protobuf v1.36.11
|
||||
)
|
||||
|
||||
require (
|
||||
go.opentelemetry.io/otel v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 // indirect
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
|
||||
golang.org/x/net v0.53.0 // indirect
|
||||
golang.org/x/sys v0.43.0 // indirect
|
||||
golang.org/x/text v0.36.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529 // indirect
|
||||
)
|
||||
|
||||
+40
@@ -1,5 +1,10 @@
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
@@ -12,20 +17,55 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 h1:HWRh5R2+9EifMyIHV7ZV+MIZqgz+PMpZ14Jynv3O2Zs=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0/go.mod h1:JfhWUomR1baixubs02l85lZYYOm7LV6om4ceouMv45c=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.68.0 h1:jhVIQEprwUTV+KfzzliLidclhoTOoHTgdz96kAyR8mU=
|
||||
go.opentelemetry.io/contrib/instrumentation/runtime v0.68.0/go.mod h1:4HsdbLUbernaTnA8CNaNE+1g026SciXb3juRYe3l8EY=
|
||||
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
|
||||
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.43.0 h1:8UQVDcZxOJLtX6gxtDt3vY2WTgvZqMQRzjsqiIHQdkc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.43.0/go.mod h1:2lmweYCiHYpEjQ/lSJBYhj9jP1zvCvQW4BqL9dnT7FQ=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 h1:88Y4s2C8oTui1LGM6bTWkw0ICGcOLCAI5l6zsD1j20k=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0/go.mod h1:Vl1/iaggsuRlrHf/hfPJPvVag77kKyvrLeD10kpMl+A=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.43.0 h1:RAE+JPfvEmvy+0LzyUA25/SGawPwIUbZ6u0Wug54sLc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.43.0/go.mod h1:AGmbycVGEsRx9mXMZ75CsOyhSP6MFIcj/6dnG+vhVjk=
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.43.0 h1:TC+BewnDpeiAmcscXbGMfxkO+mwYUwE/VySwvw88PfA=
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.43.0/go.mod h1:J/ZyF4vfPwsSr9xJSPyQ4LqtcTPULFR64KwTikGLe+A=
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0 h1:mS47AX77OtFfKG4vtp+84kuGSFZHTyxtXIN269vChY0=
|
||||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0/go.mod h1:PJnsC41lAGncJlPUniSwM81gc80GkgWJWr3cu2nKEtU=
|
||||
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
|
||||
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
|
||||
go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpuCSL2g=
|
||||
go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
|
||||
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
|
||||
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
||||
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
||||
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 h1:VPWxll4HlMw1Vs/qXtN7BvhZqsS9cdAittCNvVENElA=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:7QBABkRtR8z+TEnmXTqIqwJLlzrZKVfAUm7tY3yGv0M=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529 h1:XF8+t6QQiS0o9ArVan/HW8Q7cycNPGsJf6GA2nXxYAg=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM=
|
||||
google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
// Package telemetry provides the shared OpenTelemetry runtime bootstrap for the
|
||||
// Scrabble services (backend, gateway and the Telegram connector). New builds the
|
||||
// tracer and meter providers from a Config, registers them as the OpenTelemetry
|
||||
// globals and installs the W3C trace-context/baggage propagators; the per-service
|
||||
// HTTP/RPC middleware and instruments live in the owning service.
|
||||
//
|
||||
// Three exporters are supported per signal: "none" (the default — no collector is
|
||||
// required locally or in CI), "stdout" (debugging) and "otlp" (gRPC export to a
|
||||
// collector). The OTLP endpoint and security are taken from the standard
|
||||
// OTEL_EXPORTER_OTLP_* environment variables read by the SDK, so no bespoke
|
||||
// configuration is introduced; the collector itself is stood up with the deploy
|
||||
// (PLAN.md Stage 14).
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.opentelemetry.io/contrib/instrumentation/runtime"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Exporter selectors supported per signal.
|
||||
const (
|
||||
ExporterNone = "none"
|
||||
ExporterStdout = "stdout"
|
||||
ExporterOTLP = "otlp"
|
||||
)
|
||||
|
||||
// Config selects a service's telemetry providers: the reported service name and
|
||||
// the per-signal exporters.
|
||||
type Config struct {
|
||||
// ServiceName is reported as the OpenTelemetry service.name resource.
|
||||
ServiceName string
|
||||
// TracesExporter is one of ExporterNone, ExporterStdout or ExporterOTLP.
|
||||
TracesExporter string
|
||||
// MetricsExporter is one of ExporterNone, ExporterStdout or ExporterOTLP.
|
||||
MetricsExporter string
|
||||
}
|
||||
|
||||
// DefaultConfig returns the default telemetry configuration for serviceName: both
|
||||
// exporters off, so no collector is required locally or in CI.
|
||||
func DefaultConfig(serviceName string) Config {
|
||||
return Config{
|
||||
ServiceName: serviceName,
|
||||
TracesExporter: ExporterNone,
|
||||
MetricsExporter: ExporterNone,
|
||||
}
|
||||
}
|
||||
|
||||
// Validate reports whether the configuration names a service and selects
|
||||
// supported exporters.
|
||||
func (c Config) Validate() error {
|
||||
if c.ServiceName == "" {
|
||||
return errors.New("telemetry: ServiceName must not be empty")
|
||||
}
|
||||
if err := validateExporter("traces", c.TracesExporter); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateExporter("metrics", c.MetricsExporter)
|
||||
}
|
||||
|
||||
func validateExporter(kind, value string) error {
|
||||
switch value {
|
||||
case ExporterNone, ExporterStdout, ExporterOTLP:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("telemetry: unsupported %s exporter %q", kind, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Runtime owns a service's OpenTelemetry providers.
|
||||
type Runtime struct {
|
||||
tracerProvider *sdktrace.TracerProvider
|
||||
meterProvider *sdkmetric.MeterProvider
|
||||
}
|
||||
|
||||
// New constructs the telemetry runtime, registers the global providers and the
|
||||
// W3C trace-context/baggage propagators, and returns the Runtime. Callers must
|
||||
// invoke Runtime.Shutdown during process exit. The OTLP exporters dial lazily, so
|
||||
// New does not fail when no collector is reachable.
|
||||
func New(ctx context.Context, cfg Config) (*Runtime, error) {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := resource.New(ctx, resource.WithAttributes(
|
||||
attribute.String("service.name", cfg.ServiceName),
|
||||
))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("telemetry: build resource: %w", err)
|
||||
}
|
||||
|
||||
tracerProvider, err := newTracerProvider(ctx, cfg, res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("telemetry: build tracer provider: %w", err)
|
||||
}
|
||||
meterProvider, err := newMeterProvider(ctx, cfg, res)
|
||||
if err != nil {
|
||||
_ = tracerProvider.Shutdown(ctx)
|
||||
return nil, fmt.Errorf("telemetry: build meter provider: %w", err)
|
||||
}
|
||||
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
otel.SetMeterProvider(meterProvider)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{},
|
||||
propagation.Baggage{},
|
||||
))
|
||||
|
||||
return &Runtime{tracerProvider: tracerProvider, meterProvider: meterProvider}, nil
|
||||
}
|
||||
|
||||
// TracerProvider returns the runtime tracer provider, or the global one when r is
|
||||
// not initialised.
|
||||
func (r *Runtime) TracerProvider() trace.TracerProvider {
|
||||
if r == nil || r.tracerProvider == nil {
|
||||
return otel.GetTracerProvider()
|
||||
}
|
||||
return r.tracerProvider
|
||||
}
|
||||
|
||||
// MeterProvider returns the runtime meter provider, or the global one when r is
|
||||
// not initialised.
|
||||
func (r *Runtime) MeterProvider() metric.MeterProvider {
|
||||
if r == nil || r.meterProvider == nil {
|
||||
return otel.GetMeterProvider()
|
||||
}
|
||||
return r.meterProvider
|
||||
}
|
||||
|
||||
// StartRuntimeMetrics begins collecting Go runtime metrics (heap, GC, goroutines)
|
||||
// against the runtime meter provider. It is a no-op observer set: nothing is
|
||||
// exported while the metrics exporter is "none". Call it once after New.
|
||||
func (r *Runtime) StartRuntimeMetrics() error {
|
||||
return runtime.Start(runtime.WithMeterProvider(r.MeterProvider()))
|
||||
}
|
||||
|
||||
// Shutdown flushes both providers within ctx.
|
||||
func (r *Runtime) Shutdown(ctx context.Context) error {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
if r.meterProvider != nil {
|
||||
err = errors.Join(err, r.meterProvider.Shutdown(ctx))
|
||||
}
|
||||
if r.tracerProvider != nil {
|
||||
err = errors.Join(err, r.tracerProvider.Shutdown(ctx))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func newTracerProvider(ctx context.Context, cfg Config, res *resource.Resource) (*sdktrace.TracerProvider, error) {
|
||||
switch cfg.TracesExporter {
|
||||
case ExporterNone:
|
||||
return sdktrace.NewTracerProvider(sdktrace.WithResource(res)), nil
|
||||
case ExporterStdout:
|
||||
exporter, err := stdouttrace.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stdout trace exporter: %w", err)
|
||||
}
|
||||
return sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exporter),
|
||||
sdktrace.WithResource(res),
|
||||
), nil
|
||||
case ExporterOTLP:
|
||||
exporter, err := otlptracegrpc.New(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("otlp trace exporter: %w", err)
|
||||
}
|
||||
return sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exporter),
|
||||
sdktrace.WithResource(res),
|
||||
), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported traces exporter %q", cfg.TracesExporter)
|
||||
}
|
||||
}
|
||||
|
||||
func newMeterProvider(ctx context.Context, cfg Config, res *resource.Resource) (*sdkmetric.MeterProvider, error) {
|
||||
switch cfg.MetricsExporter {
|
||||
case ExporterNone:
|
||||
return sdkmetric.NewMeterProvider(sdkmetric.WithResource(res)), nil
|
||||
case ExporterStdout:
|
||||
exporter, err := stdoutmetric.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stdout metric exporter: %w", err)
|
||||
}
|
||||
return sdkmetric.NewMeterProvider(
|
||||
sdkmetric.WithResource(res),
|
||||
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)),
|
||||
), nil
|
||||
case ExporterOTLP:
|
||||
exporter, err := otlpmetricgrpc.New(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("otlp metric exporter: %w", err)
|
||||
}
|
||||
return sdkmetric.NewMeterProvider(
|
||||
sdkmetric.WithResource(res),
|
||||
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exporter)),
|
||||
), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported metrics exporter %q", cfg.MetricsExporter)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestConfigValidate covers the supported and rejected exporter selections.
|
||||
func TestConfigValidate(t *testing.T) {
|
||||
for _, exp := range []string{ExporterNone, ExporterStdout, ExporterOTLP} {
|
||||
c := DefaultConfig("svc")
|
||||
c.TracesExporter = exp
|
||||
c.MetricsExporter = exp
|
||||
if err := c.Validate(); err != nil {
|
||||
t.Errorf("exporter %q must be valid: %v", exp, err)
|
||||
}
|
||||
}
|
||||
bad := DefaultConfig("svc")
|
||||
bad.TracesExporter = "prometheus"
|
||||
if err := bad.Validate(); err == nil {
|
||||
t.Error("unsupported exporter must be rejected")
|
||||
}
|
||||
if err := DefaultConfig("").Validate(); err == nil {
|
||||
t.Error("empty service name must be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewNoneAndStdout builds the providers with the none and stdout exporters,
|
||||
// starts the runtime metrics and shuts them down.
|
||||
func TestNewNoneAndStdout(t *testing.T) {
|
||||
for _, exp := range []string{ExporterNone, ExporterStdout} {
|
||||
cfg := DefaultConfig("svc")
|
||||
cfg.TracesExporter = exp
|
||||
cfg.MetricsExporter = exp
|
||||
rt, err := New(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("New(%q): %v", exp, err)
|
||||
}
|
||||
if rt.TracerProvider() == nil || rt.MeterProvider() == nil {
|
||||
t.Errorf("New(%q): nil provider", exp)
|
||||
}
|
||||
if err := rt.StartRuntimeMetrics(); err != nil {
|
||||
t.Errorf("StartRuntimeMetrics(%q): %v", exp, err)
|
||||
}
|
||||
if err := rt.Shutdown(context.Background()); err != nil {
|
||||
t.Errorf("Shutdown(%q): %v", exp, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewOTLPConstructs verifies the otlp exporter selection builds providers
|
||||
// without a reachable collector (the gRPC exporters dial lazily). Shutdown is
|
||||
// best-effort: with no collector the final export may error, so it is bounded and
|
||||
// its result ignored.
|
||||
func TestNewOTLPConstructs(t *testing.T) {
|
||||
cfg := DefaultConfig("svc")
|
||||
cfg.TracesExporter = ExporterOTLP
|
||||
cfg.MetricsExporter = ExporterOTLP
|
||||
rt, err := New(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("New(otlp): %v", err)
|
||||
}
|
||||
if rt.TracerProvider() == nil || rt.MeterProvider() == nil {
|
||||
t.Error("New(otlp): nil provider")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
_ = rt.Shutdown(ctx)
|
||||
}
|
||||
|
||||
// TestNilRuntime checks the nil-receiver fallbacks used before initialisation.
|
||||
func TestNilRuntime(t *testing.T) {
|
||||
var rt *Runtime
|
||||
if rt.TracerProvider() == nil {
|
||||
t.Error("nil runtime must fall back to the global tracer provider")
|
||||
}
|
||||
if rt.MeterProvider() == nil {
|
||||
t.Error("nil runtime must fall back to the global meter provider")
|
||||
}
|
||||
if err := rt.Shutdown(context.Background()); err != nil {
|
||||
t.Errorf("nil runtime Shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user