完善 otel 配置

This commit is contained in:
2026-05-08 13:53:47 +08:00
parent a0b0be2b8e
commit 042c8d1a51
7 changed files with 108 additions and 66 deletions

View File

@@ -3,6 +3,7 @@ package globals
import (
"context"
"fmt"
"log/slog"
"platform/pkg/env"
"go.opentelemetry.io/otel"
@@ -17,11 +18,17 @@ import (
var tp *trace.TracerProvider
func initOtel(ctx context.Context) error {
addr := env.OtelHost + ":" + env.OtelPort
name := "lanhu-platform"
if env.OtelNameSuffix != "" {
name += "-" + env.OtelNameSuffix
}
slog.Info("初始化 otel", "endpoint", addr, "service_suffix", name)
if env.OtelHost == "" || env.OtelPort == "" {
return nil
}
addr := env.OtelHost + ":" + env.OtelPort
exporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithEndpoint(addr),
otlptracegrpc.WithInsecure(),
@@ -36,7 +43,7 @@ func initOtel(ctx context.Context) error {
trace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("lanhu-platform"),
semconv.ServiceNameKey.String(name),
),
),
)