forked from mtusi2026/mtusi2026lab7
на оценку 4
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type JobRecorder struct {
|
||||
runtime *Runtime
|
||||
processed metric.Int64Counter
|
||||
duration metric.Float64Histogram
|
||||
readyDuration metric.Float64Histogram
|
||||
busy atomic.Int64
|
||||
capacity int64
|
||||
}
|
||||
|
||||
type JobAttempt struct {
|
||||
recorder *JobRecorder
|
||||
ctx context.Context
|
||||
span trace.Span
|
||||
started time.Time
|
||||
createdAt time.Time
|
||||
jobType string
|
||||
attempt int
|
||||
ended atomic.Bool
|
||||
}
|
||||
|
||||
func NewJobRecorder(runtime *Runtime, capacity int) (*JobRecorder, error) {
|
||||
if capacity < 1 {
|
||||
return nil, fmt.Errorf("worker capacity must be positive")
|
||||
}
|
||||
processed, err := runtime.Meter.Int64Counter(
|
||||
"messenger.worker.jobs",
|
||||
metric.WithDescription("Number of completed worker processing attempts"),
|
||||
metric.WithUnit("{job}"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker jobs counter: %w", err)
|
||||
}
|
||||
duration, err := runtime.Meter.Float64Histogram(
|
||||
"messenger.worker.job.duration",
|
||||
metric.WithDescription("Worker processing attempt duration"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker duration histogram: %w", err)
|
||||
}
|
||||
readyDuration, err := runtime.Meter.Float64Histogram(
|
||||
"messenger.attachment.ready.duration",
|
||||
metric.WithDescription("Time from durable acceptance until attachment ready"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create attachment ready duration histogram: %w", err)
|
||||
}
|
||||
busy, err := runtime.Meter.Int64ObservableGauge(
|
||||
"messenger.worker.busy",
|
||||
metric.WithDescription("Number of workers processing jobs"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker busy metric: %w", err)
|
||||
}
|
||||
workerCapacity, err := runtime.Meter.Int64ObservableGauge(
|
||||
"messenger.worker.capacity",
|
||||
metric.WithDescription("Configured worker processing capacity"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create worker capacity metric: %w", err)
|
||||
}
|
||||
recorder := &JobRecorder{
|
||||
runtime: runtime,
|
||||
processed: processed,
|
||||
duration: duration,
|
||||
readyDuration: readyDuration,
|
||||
capacity: int64(capacity),
|
||||
}
|
||||
_, err = runtime.Meter.RegisterCallback(
|
||||
func(ctx context.Context, observer metric.Observer) error {
|
||||
observer.ObserveInt64(busy, recorder.busy.Load())
|
||||
observer.ObserveInt64(workerCapacity, recorder.capacity)
|
||||
return nil
|
||||
},
|
||||
busy,
|
||||
workerCapacity,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("register worker saturation metrics: %w", err)
|
||||
}
|
||||
return recorder, nil
|
||||
}
|
||||
|
||||
func (recorder *JobRecorder) Start(
|
||||
ctx context.Context,
|
||||
jobType string,
|
||||
destination string,
|
||||
attempt int,
|
||||
createdAt time.Time,
|
||||
) (context.Context, *JobAttempt) {
|
||||
ctx, span := recorder.runtime.Tracer.Start(
|
||||
ctx,
|
||||
"attachment.process",
|
||||
trace.WithSpanKind(trace.SpanKindConsumer),
|
||||
trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.destination.name", destination),
|
||||
attribute.String("messaging.operation.name", "process"),
|
||||
attribute.String("job.type", jobType),
|
||||
attribute.Int("job.attempt", attempt),
|
||||
),
|
||||
)
|
||||
recorder.busy.Add(1)
|
||||
return ctx, &JobAttempt{
|
||||
recorder: recorder,
|
||||
ctx: ctx,
|
||||
span: span,
|
||||
started: time.Now(),
|
||||
createdAt: createdAt,
|
||||
jobType: jobType,
|
||||
attempt: attempt,
|
||||
}
|
||||
}
|
||||
|
||||
func (attempt *JobAttempt) End(processErr error) {
|
||||
if !attempt.ended.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
defer attempt.recorder.busy.Add(-1)
|
||||
elapsed := time.Since(attempt.started)
|
||||
outcome := "succeeded"
|
||||
event := "attachment_job_completed"
|
||||
if processErr != nil {
|
||||
outcome = "failed"
|
||||
event = "attachment_job_failed"
|
||||
attempt.span.SetStatus(codes.Error, "job processing failed")
|
||||
} else {
|
||||
attempt.span.SetStatus(codes.Ok, "")
|
||||
}
|
||||
labels := metric.WithAttributes(
|
||||
attribute.String("job_type", attempt.jobType),
|
||||
attribute.String("outcome", outcome),
|
||||
)
|
||||
attempt.recorder.processed.Add(attempt.ctx, 1, labels)
|
||||
attempt.recorder.duration.Record(attempt.ctx, elapsed.Seconds(), labels)
|
||||
if processErr == nil && !attempt.createdAt.IsZero() {
|
||||
readySeconds := time.Since(attempt.createdAt).Seconds()
|
||||
if readySeconds >= 0 {
|
||||
attempt.recorder.readyDuration.Record(
|
||||
attempt.ctx,
|
||||
readySeconds,
|
||||
metric.WithAttributes(attribute.String("job_type", attempt.jobType)),
|
||||
)
|
||||
}
|
||||
}
|
||||
fields := Fields{
|
||||
"job_type": attempt.jobType,
|
||||
"outcome": outcome,
|
||||
"attempt": attempt.attempt,
|
||||
"duration_ms": float64(elapsed.Microseconds()) / 1000,
|
||||
}
|
||||
if processErr != nil {
|
||||
attempt.recorder.runtime.Logger.Warn(attempt.ctx, event, fields)
|
||||
} else {
|
||||
attempt.recorder.runtime.Logger.Info(attempt.ctx, event, fields)
|
||||
}
|
||||
attempt.span.End()
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
otellog "go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type Fields map[string]any
|
||||
|
||||
type Logger struct {
|
||||
logger otellog.Logger
|
||||
serviceName string
|
||||
instanceID string
|
||||
}
|
||||
|
||||
var stdoutMu sync.Mutex
|
||||
|
||||
func NewLogger(logger otellog.Logger, serviceName, instanceID string) *Logger {
|
||||
return &Logger{logger: logger, serviceName: serviceName, instanceID: instanceID}
|
||||
}
|
||||
|
||||
func (logger *Logger) Info(ctx context.Context, event string, fields Fields) {
|
||||
logger.emit(ctx, otellog.SeverityInfo, "INFO", event, fields)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warn(ctx context.Context, event string, fields Fields) {
|
||||
logger.emit(ctx, otellog.SeverityWarn, "WARN", event, fields)
|
||||
}
|
||||
|
||||
func (logger *Logger) emit(
|
||||
ctx context.Context,
|
||||
severity otellog.Severity,
|
||||
severityText string,
|
||||
event string,
|
||||
fields Fields,
|
||||
) {
|
||||
now := time.Now().UTC()
|
||||
spanContext := trace.SpanContextFromContext(ctx)
|
||||
traceID := ""
|
||||
spanID := ""
|
||||
if spanContext.IsValid() {
|
||||
traceID = spanContext.TraceID().String()
|
||||
spanID = spanContext.SpanID().String()
|
||||
}
|
||||
|
||||
line := map[string]any{
|
||||
"timestamp": now.Format(time.RFC3339Nano),
|
||||
"severity": severityText,
|
||||
"service.name": logger.serviceName,
|
||||
"service.instance.id": logger.instanceID,
|
||||
"event": event,
|
||||
"trace_id": traceID,
|
||||
"span_id": spanID,
|
||||
}
|
||||
attributes := []attribute.KeyValue{
|
||||
attribute.String("event", event),
|
||||
attribute.String("trace_id", traceID),
|
||||
attribute.String("span_id", spanID),
|
||||
}
|
||||
for key, value := range fields {
|
||||
if _, protected := line[key]; protected {
|
||||
continue
|
||||
}
|
||||
line[key] = value
|
||||
attributes = append(attributes, logAttribute(key, value))
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(line)
|
||||
if err == nil {
|
||||
stdoutMu.Lock()
|
||||
_, _ = fmt.Fprintln(os.Stdout, string(encoded))
|
||||
stdoutMu.Unlock()
|
||||
}
|
||||
|
||||
var record otellog.Record
|
||||
record.SetTimestamp(now)
|
||||
record.SetObservedTimestamp(now)
|
||||
record.SetSeverity(severity)
|
||||
record.SetSeverityText(severityText)
|
||||
record.SetEventName(event)
|
||||
record.SetBody(attribute.StringValue(event))
|
||||
record.AddAttributes(attributes...)
|
||||
logger.logger.Emit(ctx, record)
|
||||
}
|
||||
|
||||
func logAttribute(key string, value any) attribute.KeyValue {
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return attribute.String(key, typed)
|
||||
case int:
|
||||
return attribute.Int(key, typed)
|
||||
case int64:
|
||||
return attribute.Int64(key, typed)
|
||||
case float64:
|
||||
return attribute.Float64(key, typed)
|
||||
case bool:
|
||||
return attribute.Bool(key, typed)
|
||||
default:
|
||||
return attribute.String(key, fmt.Sprint(value))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
func registerProcessMetrics(meter metric.Meter) error {
|
||||
cpu, err := meter.Float64ObservableCounter(
|
||||
"messenger.process.cpu.time",
|
||||
metric.WithDescription("CPU time consumed by the process"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create process CPU metric: %w", err)
|
||||
}
|
||||
memory, err := meter.Int64ObservableGauge(
|
||||
"messenger.process.memory",
|
||||
metric.WithDescription("Resident memory used by the process"),
|
||||
metric.WithUnit("By"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create process memory metric: %w", err)
|
||||
}
|
||||
_, err = meter.RegisterCallback(
|
||||
func(ctx context.Context, observer metric.Observer) error {
|
||||
if seconds, readErr := processCPUSeconds(); readErr == nil {
|
||||
observer.ObserveFloat64(cpu, seconds)
|
||||
}
|
||||
if bytes, readErr := processResidentBytes(); readErr == nil {
|
||||
observer.ObserveInt64(memory, bytes)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
cpu,
|
||||
memory,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register process metrics: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func processCPUSeconds() (float64, error) {
|
||||
data, err := os.ReadFile("/proc/self/schedstat")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) < 1 {
|
||||
return 0, fmt.Errorf("invalid /proc/self/schedstat")
|
||||
}
|
||||
nanoseconds, err := strconv.ParseUint(fields[0], 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return float64(nanoseconds) / 1_000_000_000, nil
|
||||
}
|
||||
|
||||
func processResidentBytes() (int64, error) {
|
||||
data, err := os.ReadFile("/proc/self/statm")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) < 2 {
|
||||
return 0, fmt.Errorf("invalid /proc/self/statm")
|
||||
}
|
||||
pages, err := strconv.ParseInt(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return pages * int64(os.Getpagesize()), nil
|
||||
}
|
||||
|
||||
func RegisterDBPool(runtime *Runtime, pool *pgxpool.Pool) error {
|
||||
connections, err := runtime.Meter.Int64ObservableGauge(
|
||||
"messenger.db.pool.connections",
|
||||
metric.WithDescription("PostgreSQL pool connections by state"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create DB pool connection metric: %w", err)
|
||||
}
|
||||
waits, err := runtime.Meter.Int64ObservableCounter(
|
||||
"messenger.db.pool.waits",
|
||||
metric.WithDescription("Number of acquisitions that waited for a PostgreSQL connection"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create DB pool wait metric: %w", err)
|
||||
}
|
||||
_, err = runtime.Meter.RegisterCallback(
|
||||
func(ctx context.Context, observer metric.Observer) error {
|
||||
stats := pool.Stat()
|
||||
observer.ObserveInt64(
|
||||
connections,
|
||||
int64(stats.AcquiredConns()),
|
||||
metric.WithAttributes(attribute.String("state", "used")),
|
||||
)
|
||||
observer.ObserveInt64(
|
||||
connections,
|
||||
int64(stats.IdleConns()),
|
||||
metric.WithAttributes(attribute.String("state", "idle")),
|
||||
)
|
||||
observer.ObserveInt64(
|
||||
connections,
|
||||
int64(stats.MaxConns()),
|
||||
metric.WithAttributes(attribute.String("state", "capacity")),
|
||||
)
|
||||
observer.ObserveInt64(waits, stats.EmptyAcquireCount())
|
||||
return nil
|
||||
},
|
||||
connections,
|
||||
waits,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register DB pool metrics: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type QueueRecorder struct {
|
||||
depth metric.Int64Gauge
|
||||
age metric.Float64Gauge
|
||||
}
|
||||
|
||||
func NewQueueRecorder(runtime *Runtime) (*QueueRecorder, error) {
|
||||
depth, err := runtime.Meter.Int64Gauge(
|
||||
"messenger.queue.depth",
|
||||
metric.WithDescription("Number of queued attachment jobs"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create queue depth metric: %w", err)
|
||||
}
|
||||
age, err := runtime.Meter.Float64Gauge(
|
||||
"messenger.queue.oldest_job_age",
|
||||
metric.WithDescription("Age of the oldest queued attachment job"),
|
||||
metric.WithUnit("s"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create queue age metric: %w", err)
|
||||
}
|
||||
return &QueueRecorder{depth: depth, age: age}, nil
|
||||
}
|
||||
|
||||
func (recorder *QueueRecorder) Record(ctx context.Context, depth int64, ageSeconds float64) {
|
||||
labels := metric.WithAttributes(attribute.String("queue", "attachment.process.v1"))
|
||||
recorder.depth.Record(ctx, depth, labels)
|
||||
recorder.age.Record(ctx, ageSeconds, labels)
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab7/internal/security"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"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"
|
||||
otellogglobal "go.opentelemetry.io/otel/log/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
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"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceVersion = "6.0.0"
|
||||
instrumentationScope = "mtusi2026lab7"
|
||||
)
|
||||
|
||||
type Runtime struct {
|
||||
Tracer trace.Tracer
|
||||
Meter metric.Meter
|
||||
Logger *Logger
|
||||
|
||||
tracerProvider *sdktrace.TracerProvider
|
||||
meterProvider *sdkmetric.MeterProvider
|
||||
loggerProvider *sdklog.LoggerProvider
|
||||
}
|
||||
|
||||
func Start(
|
||||
ctx context.Context,
|
||||
serviceName string,
|
||||
instanceID string,
|
||||
deploymentEnvironment string,
|
||||
) (*Runtime, error) {
|
||||
serverName := os.Getenv("OTEL_TLS_SERVER_NAME")
|
||||
if serverName == "" {
|
||||
serverName = "telemetry-gateway"
|
||||
}
|
||||
clientTLS, err := security.ClientMTLSConfig(
|
||||
os.Getenv("CLIENT_TLS_CA_FILE"),
|
||||
os.Getenv("CLIENT_TLS_CERT_FILE"),
|
||||
os.Getenv("CLIENT_TLS_KEY_FILE"),
|
||||
serverName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("configure OpenTelemetry mTLS: %w", err)
|
||||
}
|
||||
|
||||
res, err := resource.New(ctx, resource.WithAttributes(
|
||||
attribute.String("service.name", serviceName),
|
||||
attribute.String("service.version", ServiceVersion),
|
||||
attribute.String("service.instance.id", instanceID),
|
||||
attribute.String("deployment.environment.name", deploymentEnvironment),
|
||||
))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create OpenTelemetry resource: %w", err)
|
||||
}
|
||||
|
||||
traceExporter, err := otlptracehttp.New(ctx,
|
||||
otlptracehttp.WithTimeout(3*time.Second),
|
||||
otlptracehttp.WithTLSClientConfig(clientTLS.Clone()),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create OTLP trace exporter: %w", err)
|
||||
}
|
||||
tracerProvider := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithResource(res),
|
||||
sdktrace.WithBatcher(
|
||||
traceExporter,
|
||||
sdktrace.WithBatchTimeout(time.Second),
|
||||
sdktrace.WithExportTimeout(3*time.Second),
|
||||
sdktrace.WithMaxQueueSize(2048),
|
||||
sdktrace.WithMaxExportBatchSize(256),
|
||||
),
|
||||
)
|
||||
|
||||
metricExporter, err := otlpmetrichttp.New(ctx,
|
||||
otlpmetrichttp.WithTimeout(3*time.Second),
|
||||
otlpmetrichttp.WithTLSClientConfig(clientTLS.Clone()),
|
||||
)
|
||||
if err != nil {
|
||||
_ = tracerProvider.Shutdown(context.Background())
|
||||
return nil, fmt.Errorf("create OTLP metric exporter: %w", err)
|
||||
}
|
||||
metricReader := sdkmetric.NewPeriodicReader(
|
||||
metricExporter,
|
||||
sdkmetric.WithInterval(5*time.Second),
|
||||
sdkmetric.WithTimeout(3*time.Second),
|
||||
)
|
||||
meterProvider := sdkmetric.NewMeterProvider(
|
||||
sdkmetric.WithResource(res),
|
||||
sdkmetric.WithReader(metricReader),
|
||||
sdkmetric.WithView(durationHistogramView(
|
||||
"messenger.http.server.duration",
|
||||
[]float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
|
||||
)),
|
||||
sdkmetric.WithView(durationHistogramView(
|
||||
"messenger.worker.job.duration",
|
||||
[]float64{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60},
|
||||
)),
|
||||
sdkmetric.WithView(durationHistogramView(
|
||||
"messenger.attachment.ready.duration",
|
||||
[]float64{0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 120},
|
||||
)),
|
||||
)
|
||||
|
||||
logExporter, err := otlploghttp.New(ctx,
|
||||
otlploghttp.WithTimeout(3*time.Second),
|
||||
otlploghttp.WithTLSClientConfig(clientTLS.Clone()),
|
||||
)
|
||||
if err != nil {
|
||||
_ = meterProvider.Shutdown(context.Background())
|
||||
_ = tracerProvider.Shutdown(context.Background())
|
||||
return nil, fmt.Errorf("create OTLP log exporter: %w", err)
|
||||
}
|
||||
loggerProvider := sdklog.NewLoggerProvider(
|
||||
sdklog.WithResource(res),
|
||||
sdklog.WithAttributeCountLimit(32),
|
||||
sdklog.WithAttributeValueLengthLimit(1024),
|
||||
sdklog.WithProcessor(sdklog.NewBatchProcessor(
|
||||
logExporter,
|
||||
sdklog.WithMaxQueueSize(2048),
|
||||
sdklog.WithExportMaxBatchSize(256),
|
||||
sdklog.WithExportInterval(time.Second),
|
||||
sdklog.WithExportTimeout(3*time.Second),
|
||||
)),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
otel.SetMeterProvider(meterProvider)
|
||||
otellogglobal.SetLoggerProvider(loggerProvider)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{},
|
||||
propagation.Baggage{},
|
||||
))
|
||||
|
||||
runtime := &Runtime{
|
||||
Tracer: tracerProvider.Tracer(instrumentationScope),
|
||||
Meter: meterProvider.Meter(instrumentationScope),
|
||||
Logger: NewLogger(otellogglobal.Logger(instrumentationScope), serviceName, instanceID),
|
||||
tracerProvider: tracerProvider,
|
||||
meterProvider: meterProvider,
|
||||
loggerProvider: loggerProvider,
|
||||
}
|
||||
if err = registerProcessMetrics(runtime.Meter); err != nil {
|
||||
_ = runtime.Shutdown(context.Background())
|
||||
return nil, err
|
||||
}
|
||||
return runtime, nil
|
||||
}
|
||||
|
||||
func durationHistogramView(name string, boundaries []float64) sdkmetric.View {
|
||||
return sdkmetric.NewView(
|
||||
sdkmetric.Instrument{Name: name, Kind: sdkmetric.InstrumentKindHistogram},
|
||||
sdkmetric.Stream{Aggregation: sdkmetric.AggregationExplicitBucketHistogram{
|
||||
Boundaries: boundaries,
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
func (runtime *Runtime) Shutdown(ctx context.Context) error {
|
||||
if runtime == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.Join(
|
||||
runtime.loggerProvider.Shutdown(ctx),
|
||||
runtime.meterProvider.Shutdown(ctx),
|
||||
runtime.tracerProvider.Shutdown(ctx),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user