123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package opentelemetry
- import (
- "context"
- "go.opentelemetry.io/otel/attribute"
- "go.opentelemetry.io/otel/sdk/resource"
- semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
- extConfig "go-admin/config"
- )
- func Setup() {
- // Create resource.
- res, err := newResource()
- if err != nil {
- panic(err)
- }
- initMeter(res)
- initTracer(res)
- // Handle shutdown properly so nothing leaks.
- // defer func() {
- // if err := meterProvider.Shutdown(context.Background()); err != nil {
- // log.Println(err)
- // }
- // }()
- }
- func newResource() (res *resource.Resource, err error) {
- res = resource.Default()
- extres, _ := resource.New(
- context.TODO(),
- resource.WithHost(),
- resource.WithOS(),
- resource.WithProcess(),
- resource.WithContainer(),
- )
- res, err = resource.Merge(res, extres)
- com := extConfig.ExtConfig.Otel.Common
- serviceName := com.ServiceName
- serviceVersion := com.ServiceVersion
- if serviceName != "" && serviceVersion != "" {
- res, err = resource.Merge(res, resource.NewWithAttributes(
- semconv.SchemaURL,
- semconv.ServiceName(serviceName),
- semconv.ServiceVersion(serviceVersion),
- semconv.ProcessCommandLine("-DAPP_NAME=OBSERVE"), // 从这里固定commandline,用于applias解析;golang从不能从命令行直接写
- attribute.String("app.name", "OBSERVE"),
- ))
- }
- return
- }
|