master
nikkiing 1 year ago
parent aa8195688e
commit c27049b784
  1. 48
      go.mod
  2. 1294
      go.sum
  3. 37
      tracer/ali/ali_trace.go
  4. 64
      tracer/generator/id_generator.go

@ -4,40 +4,64 @@ go 1.18
require ( require (
gitea.drugeyes.vip/pharnexbase/tools v1.0.0 gitea.drugeyes.vip/pharnexbase/tools v1.0.0
github.com/aliyun-sls/opentelemetry-go-provider-sls v0.9.0
github.com/aliyun/aliyun-log-go-sdk v0.1.43 github.com/aliyun/aliyun-log-go-sdk v0.1.43
github.com/elastic/go-elasticsearch/v7 v7.10.0 github.com/elastic/go-elasticsearch/v7 v7.10.0
github.com/go-kratos/kratos/v2 v2.5.4 github.com/go-kratos/kratos/v2 v2.5.4
github.com/tidwall/gjson v1.14.4 github.com/tidwall/gjson v1.14.4
github.com/tjfoc/gmsm v1.4.1 github.com/tjfoc/gmsm v1.4.1
go.opentelemetry.io/otel v1.7.0 go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.7.0 go.opentelemetry.io/otel/sdk v1.14.0
google.golang.org/grpc v1.46.2 go.opentelemetry.io/otel/trace v1.14.0
google.golang.org/protobuf v1.28.0 google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
) )
require ( require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/go-kit/kit v0.10.0 // indirect github.com/go-kit/kit v0.10.0 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect github.com/go-logfmt/logfmt v0.5.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/form/v4 v4.2.0 // indirect github.com/go-playground/form/v4 v4.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect
github.com/pierrec/lz4 v2.6.0+incompatible // indirect github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/sethvargo/go-envconfig v0.9.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.1 // indirect
github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/contrib/instrumentation/host v0.40.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.40.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.37.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.37.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.5.0 // indirect go.uber.org/atomic v1.5.0 // indirect
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.3.8 // indirect golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.1.12 // indirect golang.org/x/tools v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect google.golang.org/genproto v0.0.0-20230227214838-9b19f0bdc514 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

1294
go.sum

File diff suppressed because it is too large Load Diff

@ -0,0 +1,37 @@
package ali
import (
"gitea.drugeyes.vip/pharnexbase/utils/tracer/generator"
"github.com/aliyun-sls/opentelemetry-go-provider-sls/provider"
)
type AliSLSConfig struct {
Name string
Version string
Env string
TraceExporterEndpoint string
MetricExporterEndpoint string
Project string
InstanceId string
AccessKeyId string
AccessKeySecret string
}
// Set global trace provider 设置链路追逐的方法 ali
func initTracerProvider(bc *AliSLSConfig) (*provider.Config, error) {
slsConfig, err := provider.NewConfig(provider.WithServiceName(bc.Name),
provider.WithServiceVersion(bc.Version),
provider.WithServiceNamespace(bc.Env),
provider.WithTraceExporterEndpoint(bc.TraceExporterEndpoint),
provider.WithMetricExporterEndpoint(bc.MetricExporterEndpoint),
provider.WithIDGenerator(generator.DefaultIDGenerator()),
provider.WithSLSConfig(bc.Project, bc.InstanceId, bc.AccessKeyId, bc.AccessKeySecret))
if err != nil {
panic(err)
}
if err := provider.Start(slsConfig); err != nil {
panic(err)
}
return slsConfig, nil
}

@ -0,0 +1,64 @@
package generator
import (
"bytes"
"context"
crand "crypto/rand"
"encoding/binary"
"encoding/hex"
"github.com/go-kratos/kratos/v2/transport"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"io"
"math/rand"
"sync"
)
type IDGenerator struct {
sync.Mutex
randSource *rand.Rand
}
var _ sdktrace.IDGenerator = &IDGenerator{}
// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
func (gen *IDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID {
gen.Lock()
defer gen.Unlock()
sid := trace.SpanID{}
_, _ = gen.randSource.Read(sid[:])
return sid
}
// NewIDs returns a non-zero trace ID and a non-zero span ID from a
// randomly-chosen sequence.
func (gen *IDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
gen.Lock()
defer gen.Unlock()
var (
tid = trace.TraceID{}
sid = trace.SpanID{}
tidReader io.Reader
)
tidReader = gen.randSource
if tr, ok := transport.FromServerContext(ctx); ok {
traceId := tr.RequestHeader().Get("Trace-Id")
traceHex, err := hex.DecodeString(traceId)
if err == nil && traceId != "" {
tidReader = bytes.NewBuffer(traceHex)
}
}
_, _ = tidReader.Read(tid[:])
_, _ = gen.randSource.Read(sid[:])
return tid, sid
}
func DefaultIDGenerator() sdktrace.IDGenerator {
gen := &IDGenerator{}
var rngSeed int64
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
gen.randSource = rand.New(rand.NewSource(rngSeed))
return gen
}
Loading…
Cancel
Save