forked from mtusi2026/mtusi2026lab6
87 lines
4.7 KiB
Go
87 lines
4.7 KiB
Go
// Package telemetry owns bounded OpenTelemetry providers and safe instrumentation.
|
|
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
|
otellog "go.opentelemetry.io/otel/log"
|
|
"go.opentelemetry.io/otel/metric"
|
|
sdklog "go.opentelemetry.io/otel/sdk/log"
|
|
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"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
|
)
|
|
|
|
// Runtime owns providers; Shutdown is called once by the application lifecycle.
|
|
type Runtime struct {
|
|
traces *sdktrace.TracerProvider
|
|
metrics *sdkmetric.MeterProvider
|
|
logs *sdklog.LoggerProvider
|
|
tracer trace.Tracer
|
|
logger otellog.Logger
|
|
meter metric.Meter
|
|
instruments instruments
|
|
enabled bool
|
|
}
|
|
|
|
// New builds nonblocking bounded exporters; backend availability is never a startup dependency.
|
|
func New(ctx context.Context, cfg *config.Telemetry) (*Runtime, error) {
|
|
if cfg == nil {
|
|
return nil, fmt.Errorf("telemetry configuration is required")
|
|
}
|
|
if math.IsNaN(cfg.SampleRatio) || cfg.SampleRatio < 0 || cfg.SampleRatio > 1 {
|
|
return nil, fmt.Errorf("telemetry sampling ratio must be in [0,1]")
|
|
}
|
|
if cfg.Endpoint != "" {
|
|
endpoint, err := url.Parse(cfg.Endpoint)
|
|
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.User != nil {
|
|
return nil, fmt.Errorf("invalid telemetry endpoint")
|
|
}
|
|
}
|
|
res := resource.NewSchemaless(attribute.String("service.name", cfg.Service), attribute.String("service.version", cfg.Version), attribute.String("service.instance.id", cfg.Instance), attribute.String("deployment.environment.name", "lab"))
|
|
traceOptions := []sdktrace.TracerProviderOption{sdktrace.WithResource(res), sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(cfg.SampleRatio)))}
|
|
metricOptions := []sdkmetric.Option{sdkmetric.WithResource(res), sdkmetric.WithView(sdkmetric.NewView(sdkmetric.Instrument{Kind: sdkmetric.InstrumentKindHistogram}, sdkmetric.Stream{Aggregation: sdkmetric.AggregationExplicitBucketHistogram{Boundaries: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2, 5, 10, 30, 60, 120}}}))}
|
|
logOptions := []sdklog.LoggerProviderOption{sdklog.WithResource(res)}
|
|
if cfg.Endpoint != "" {
|
|
traces, err := otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(strings.TrimRight(cfg.Endpoint, "/")+"/v1/traces"), otlptracehttp.WithTimeout(time.Second), otlptracehttp.WithRetry(otlptracehttp.RetryConfig{Enabled: false}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
metrics, err := otlpmetrichttp.New(ctx, otlpmetrichttp.WithEndpointURL(strings.TrimRight(cfg.Endpoint, "/")+"/v1/metrics"), otlpmetrichttp.WithTimeout(time.Second), otlpmetrichttp.WithRetry(otlpmetrichttp.RetryConfig{Enabled: false}))
|
|
if err != nil {
|
|
return nil, errors.Join(err, traces.Shutdown(ctx))
|
|
}
|
|
logs, err := otlploghttp.New(ctx, otlploghttp.WithEndpointURL(strings.TrimRight(cfg.Endpoint, "/")+"/v1/logs"), otlploghttp.WithTimeout(time.Second), otlploghttp.WithRetry(otlploghttp.RetryConfig{Enabled: false}))
|
|
if err != nil {
|
|
return nil, errors.Join(err, traces.Shutdown(ctx), metrics.Shutdown(ctx))
|
|
}
|
|
traceOptions = append(traceOptions, sdktrace.WithBatcher(traces, sdktrace.WithMaxQueueSize(256), sdktrace.WithMaxExportBatchSize(64), sdktrace.WithBatchTimeout(time.Second), sdktrace.WithExportTimeout(time.Second)))
|
|
metricOptions = append(metricOptions, sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metrics, sdkmetric.WithInterval(5*time.Second), sdkmetric.WithTimeout(time.Second))))
|
|
logOptions = append(logOptions, sdklog.WithProcessor(sdklog.NewBatchProcessor(logs, sdklog.WithMaxQueueSize(256), sdklog.WithExportMaxBatchSize(64), sdklog.WithExportInterval(time.Second), sdklog.WithExportTimeout(time.Second), sdklog.WithExportBufferSize(1))))
|
|
}
|
|
r := &Runtime{traces: sdktrace.NewTracerProvider(traceOptions...), metrics: sdkmetric.NewMeterProvider(metricOptions...), logs: sdklog.NewLoggerProvider(logOptions...), enabled: cfg.Endpoint != ""}
|
|
r.tracer = r.traces.Tracer("messenger")
|
|
r.meter = r.metrics.Meter("messenger")
|
|
r.logger = r.logs.Logger("messenger")
|
|
r.instruments = newInstruments(r.meter)
|
|
return r, nil
|
|
}
|
|
|
|
// Shutdown flushes all providers under the lifecycle's bounded deadline.
|
|
func (r *Runtime) Shutdown(ctx context.Context) error {
|
|
return errors.Join(r.traces.Shutdown(ctx), r.metrics.Shutdown(ctx), r.logs.Shutdown(ctx))
|
|
}
|