Files
gf/contrib/trace/otlphttp/otlphttp.go
2023-05-29 10:00:41 +08:00

90 lines
2.6 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package otlphttp provides gtrace.Tracer implementation using OpenTelemetry protocol.
package otlphttp
import (
"context"
"github.com/gogf/gf/v2/net/gipv4"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
)
const (
tracerHostnameTagKey = "hostname"
)
// Init initializes and registers `otlphttp` to global TracerProvider.
//
// The output parameter `Shutdown` is used for waiting exported trace spans to be uploaded,
// which is useful if your program is ending, and you do not want to lose recent spans.
func Init(serviceName, endpoint, path string) (*sdktrace.TracerProvider, error) {
// Try retrieving host ip for tracing info.
var (
intranetIPArray, err = gipv4.GetIntranetIpArray()
hostIP = "NoHostIpFound"
)
if err != nil {
return nil, err
}
if len(intranetIPArray) == 0 {
if intranetIPArray, err = gipv4.GetIpArray(); err != nil {
return nil, err
}
}
if len(intranetIPArray) > 0 {
hostIP = intranetIPArray[0]
}
traceClientHTTP := otlptracehttp.NewClient(
otlptracehttp.WithEndpoint(endpoint),
otlptracehttp.WithURLPath(path),
otlptracehttp.WithInsecure(),
otlptracehttp.WithCompression(1),
)
ctx := context.Background()
traceExp, err := otlptrace.New(ctx, traceClientHTTP)
if err != nil {
return nil, err
}
res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithProcess(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithAttributes(
// The name of the service displayed on the traceback end。
semconv.ServiceNameKey.String(serviceName),
semconv.HostNameKey.String(hostIP),
attribute.String(tracerHostnameTagKey, hostIP),
),
)
bsp := sdktrace.NewBatchSpanProcessor(traceExp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
// Set the global propagator to traceContext (not set by default).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
return tracerProvider, nil
}