forked from mtusi2026/mtusi2026lab6
feat(telemetry): lab6 add OTLP metrics pipeline and otel collector
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Tracer возвращает tracer из глобального провайдера.
|
||||
func Tracer(name string) trace.Tracer {
|
||||
return otel.Tracer(name)
|
||||
}
|
||||
|
||||
// Meter возвращает meter из глобального провайдера.
|
||||
func Meter(name string) metric.Meter {
|
||||
return otel.Meter(name)
|
||||
}
|
||||
@@ -4,10 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
otellog "go.opentelemetry.io/otel/sdk/log"
|
||||
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
||||
@@ -67,9 +70,25 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) {
|
||||
sdktrace.WithResource(res),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
// Метрики
|
||||
metricExp, err := otlpmetrichttp.New(ctx,
|
||||
otlpmetrichttp.WithEndpointURL(endpoint+"/v1/metrics"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("экспортёр метрик: %w", err)
|
||||
}
|
||||
mp := sdkmetric.NewMeterProvider(
|
||||
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(
|
||||
metricExp,
|
||||
sdkmetric.WithInterval(metricInterval()),
|
||||
)),
|
||||
sdkmetric.WithResource(res),
|
||||
)
|
||||
|
||||
return &Providers{Tracer: tp}, nil
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetMeterProvider(mp)
|
||||
|
||||
return &Providers{Tracer: tp, Meter: mp}, nil
|
||||
}
|
||||
|
||||
func disabled() bool {
|
||||
@@ -83,3 +102,15 @@ func baseEndpoint() string {
|
||||
}
|
||||
return strings.TrimRight(v, "/")
|
||||
}
|
||||
|
||||
func metricInterval() time.Duration {
|
||||
v := os.Getenv("OTEL_METRIC_EXPORT_INTERVAL")
|
||||
if v == "" {
|
||||
return 15 * time.Second
|
||||
}
|
||||
ms, err := strconv.Atoi(v)
|
||||
if err != nil || ms <= 0 {
|
||||
return 15 * time.Second
|
||||
}
|
||||
return time.Duration(ms) * time.Millisecond
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user