"refactor(common): 移除OpenTelemetry相关代码及测试文件"
This commit is contained in:
@@ -1,93 +0,0 @@
|
||||
package serialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"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.15.0"
|
||||
)
|
||||
|
||||
const (
|
||||
SERVICE_NAME = "blazing"
|
||||
SERVICE_VERSION = "1.1.1"
|
||||
DEPLOY_ENVIRONMENT = "test"
|
||||
HTTP_ENDPOINT = "tracing-analysis-dc-hz.aliyuncs.com"
|
||||
HTTP_URL_PATH = "adapt_gvvug3dmib@f020b8d77be60b5_gvvug3dmib@53df7ad2afe8301/api/otlp/traces"
|
||||
)
|
||||
|
||||
// 设置应用资源
|
||||
func newResource(ctx context.Context) *resource.Resource {
|
||||
hostName, _ := os.Hostname()
|
||||
|
||||
r, err := resource.New(
|
||||
ctx,
|
||||
resource.WithFromEnv(),
|
||||
resource.WithProcess(),
|
||||
resource.WithTelemetrySDK(),
|
||||
resource.WithHost(),
|
||||
resource.WithAttributes(
|
||||
semconv.ServiceNameKey.String(SERVICE_NAME), // 应用名
|
||||
semconv.ServiceVersionKey.String(SERVICE_VERSION), // 应用版本
|
||||
semconv.DeploymentEnvironmentKey.String(DEPLOY_ENVIRONMENT), // 部署环境
|
||||
semconv.HostNameKey.String(hostName), // 主机名
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("%s: %v", "Failed to create OpenTelemetry resource", err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func newHTTPExporterAndSpanProcessor(ctx context.Context) (*otlptrace.Exporter, sdktrace.SpanProcessor) {
|
||||
|
||||
traceExporter, err := otlptrace.New(ctx, otlptracehttp.NewClient(
|
||||
otlptracehttp.WithEndpoint(HTTP_ENDPOINT),
|
||||
otlptracehttp.WithURLPath(HTTP_URL_PATH),
|
||||
otlptracehttp.WithInsecure(),
|
||||
otlptracehttp.WithCompression(1)))
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("%s: %v", "Failed to create the OpenTelemetry trace exporter", err)
|
||||
}
|
||||
|
||||
batchSpanProcessor := sdktrace.NewBatchSpanProcessor(traceExporter)
|
||||
|
||||
return traceExporter, batchSpanProcessor
|
||||
}
|
||||
|
||||
// InitOpenTelemetry OpenTelemetry 初始化方法
|
||||
func InitOpenTelemetry() func() {
|
||||
ctx := context.Background()
|
||||
|
||||
var traceExporter *otlptrace.Exporter
|
||||
var batchSpanProcessor sdktrace.SpanProcessor
|
||||
|
||||
traceExporter, batchSpanProcessor = newHTTPExporterAndSpanProcessor(ctx)
|
||||
|
||||
otelResource := newResource(ctx)
|
||||
|
||||
traceProvider := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
||||
sdktrace.WithResource(otelResource),
|
||||
sdktrace.WithSpanProcessor(batchSpanProcessor))
|
||||
|
||||
otel.SetTracerProvider(traceProvider)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
||||
|
||||
return func() {
|
||||
cxt, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
if err := traceExporter.Shutdown(cxt); err != nil {
|
||||
otel.Handle(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package serialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
)
|
||||
|
||||
func TestOpenTelemetry(t *testing.T) {
|
||||
shutdown := InitOpenTelemetry()
|
||||
defer shutdown()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
ctx := context.Background()
|
||||
parentMethod(ctx)
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
|
||||
func parentMethod(ctx context.Context) {
|
||||
tracer := otel.Tracer("otel-go-tracer")
|
||||
ctx, span := tracer.Start(ctx, "parent span")
|
||||
fmt.Println(span.SpanContext().TraceID()) // 打印 TraceId
|
||||
span.SetAttributes(attribute.String("key", "哈哈哈"))
|
||||
span.SetStatus(codes.Ok, "Success")
|
||||
childMethod(ctx)
|
||||
span.End()
|
||||
}
|
||||
|
||||
func childMethod(ctx context.Context) {
|
||||
tracer := otel.Tracer("otel-go-tracer")
|
||||
ctx, span := tracer.Start(ctx, "child span")
|
||||
span.SetStatus(codes.Ok, "Success")
|
||||
grandChildMethod(ctx)
|
||||
span.End()
|
||||
}
|
||||
|
||||
func grandChildMethod(ctx context.Context) {
|
||||
tracer := otel.Tracer("otel-go-tracer")
|
||||
ctx, span := tracer.Start(ctx, "grandchild span")
|
||||
span.SetStatus(codes.Error, "error")
|
||||
|
||||
// 业务代码...
|
||||
|
||||
span.End()
|
||||
}
|
||||
Reference in New Issue
Block a user