mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-04 09:58:49 +00:00
fix(tracing): from opencensus to opentelemetry (#937)
* refactor: switch from opencensus to opentelemetry * tempo works as designed nooooot * fix: log traceids * with grafana agent * fix: http tracing * fix: cleanup files * chore: remove todo * fix: bad test * fix: ignore methods in grpc interceptors * fix: remove test log * clean up * typo * fix(config): configure tracing endpoint * fix(span): add error id to span
This commit is contained in:
@@ -5,8 +5,9 @@ import (
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
tracing_g "github.com/caos/zitadel/internal/tracing/google"
|
||||
tracing_log "github.com/caos/zitadel/internal/tracing/log"
|
||||
"github.com/caos/zitadel/internal/tracing/google"
|
||||
"github.com/caos/zitadel/internal/tracing/log"
|
||||
"github.com/caos/zitadel/internal/tracing/otel"
|
||||
)
|
||||
|
||||
type TracingConfig struct {
|
||||
@@ -15,8 +16,9 @@ type TracingConfig struct {
|
||||
}
|
||||
|
||||
var tracer = map[string]func() tracing.Config{
|
||||
"google": func() tracing.Config { return &tracing_g.Config{} },
|
||||
"log": func() tracing.Config { return &tracing_log.Config{} },
|
||||
"otel": func() tracing.Config { return &otel.Config{} },
|
||||
"google": func() tracing.Config { return &google.Config{} },
|
||||
"log": func() tracing.Config { return &log.Config{} },
|
||||
"none": func() tracing.Config { return &NoTracing{} },
|
||||
"": func() tracing.Config { return &NoTracing{} },
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package tracing
|
||||
|
||||
//go:generate mockgen -package mock -destination mock/tracing_mock.go github.com/caos/zitadel/internal/tracing Tracer
|
||||
@@ -1,24 +0,0 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ProjectID string
|
||||
MetricPrefix string
|
||||
Fraction float64
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
if !envIsSet() {
|
||||
return errors.ThrowInvalidArgument(nil, "GOOGL-sdh3a", "env not properly set, GOOGLE_APPLICATION_CREDENTIALS is misconfigured or missing")
|
||||
}
|
||||
|
||||
tracing.T = &Tracer{projectID: c.ProjectID, metricPrefix: c.MetricPrefix, sampler: trace.ProbabilitySampler(c.Fraction)}
|
||||
|
||||
return tracing.T.Start()
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
"github.com/caos/zitadel/internal/tracing/otel"
|
||||
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ProjectID string
|
||||
MetricPrefix string
|
||||
Fraction float64
|
||||
}
|
||||
|
||||
type Tracer struct {
|
||||
otel.Tracer
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
if !envIsSet() {
|
||||
return errors.ThrowInvalidArgument(nil, "GOOGL-sdh3a", "env not properly set, GOOGLE_APPLICATION_CREDENTIALS is misconfigured or missing")
|
||||
}
|
||||
|
||||
sampler := sdk_trace.ParentBased(sdk_trace.TraceIDRatioBased(c.Fraction))
|
||||
exporter, err := texporter.NewExporter(texporter.WithProjectID(c.ProjectID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func envIsSet() bool {
|
||||
gAuthCred := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
||||
return strings.Contains(gAuthCred, ".json")
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"contrib.go.opencensus.io/exporter/stackdriver"
|
||||
"go.opencensus.io/plugin/ocgrpc"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/stats/view"
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
type Tracer struct {
|
||||
Exporter *stackdriver.Exporter
|
||||
projectID string
|
||||
metricPrefix string
|
||||
sampler trace.Sampler
|
||||
}
|
||||
|
||||
func (t *Tracer) Start() (err error) {
|
||||
t.Exporter, err = stackdriver.NewExporter(stackdriver.Options{
|
||||
ProjectID: t.projectID,
|
||||
MetricPrefix: t.metricPrefix,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.ThrowInternal(err, "GOOGL-4dCnX", "unable to start exporter")
|
||||
}
|
||||
|
||||
views := append(ocgrpc.DefaultServerViews, ocgrpc.DefaultClientViews...)
|
||||
views = append(views, ochttp.DefaultClientViews...)
|
||||
views = append(views, ochttp.DefaultServerViews...)
|
||||
|
||||
if err = view.Register(views...); err != nil {
|
||||
return errors.ThrowInternal(err, "GOOGL-Q6L6w", "unable to register view")
|
||||
}
|
||||
|
||||
trace.RegisterExporter(t.Exporter)
|
||||
trace.ApplyConfig(trace.Config{DefaultSampler: t.sampler})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Tracer) Sampler() trace.Sampler {
|
||||
return t.sampler
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, trace.WithSpanKind(trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, trace.WithSpanKind(trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, trace.WithSpanKind(trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, trace.WithSpanKind(trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpan(ctx context.Context, caller string, options ...trace.StartOption) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, caller, options...)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpanFromName(ctx context.Context, name string, options ...trace.StartOption) (context.Context, *tracing.Span) {
|
||||
ctx, span := trace.StartSpan(ctx, name, options...)
|
||||
return ctx, tracing.CreateSpan(span)
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpanHTTP(r *http.Request, caller string) (*http.Request, *tracing.Span) {
|
||||
ctx, span := t.NewSpan(r.Context(), caller)
|
||||
r = r.WithContext(ctx)
|
||||
return r, span
|
||||
}
|
||||
|
||||
func envIsSet() bool {
|
||||
gAuthCred := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
||||
return strings.Contains(gAuthCred, ".json")
|
||||
}
|
||||
|
||||
func (t *Tracer) SetErrStatus(span *trace.Span, code int32, err error, obj ...string) {
|
||||
span.SetStatus(trace.Status{Code: code, Message: err.Error() + strings.Join(obj, ", ")})
|
||||
}
|
||||
@@ -4,29 +4,28 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/trace"
|
||||
http_trace "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
)
|
||||
|
||||
func TraceHandler(handler http.Handler, ignoredEndpoints ...string) http.Handler {
|
||||
return &ochttp.Handler{
|
||||
Handler: handler,
|
||||
FormatSpanName: func(r *http.Request) string {
|
||||
host := r.URL.Host
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
func shouldNotIgnore(endpoints ...string) func(r *http.Request) bool {
|
||||
return func(r *http.Request) bool {
|
||||
for _, endpoint := range endpoints {
|
||||
if strings.HasPrefix(r.URL.RequestURI(), endpoint) {
|
||||
return false
|
||||
}
|
||||
return host + r.URL.Path
|
||||
},
|
||||
|
||||
StartOptions: trace.StartOptions{Sampler: Sampler()},
|
||||
IsHealthEndpoint: func(r *http.Request) bool {
|
||||
for _, endpoint := range ignoredEndpoints {
|
||||
if strings.HasPrefix(r.URL.RequestURI(), endpoint) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func TraceHandler(handler http.Handler, ignoredEndpoints ...string) http.Handler {
|
||||
return http_trace.NewHandler(handler,
|
||||
"zitadel",
|
||||
http_trace.WithFilter(shouldNotIgnore(ignoredEndpoints...)),
|
||||
http_trace.WithPublicEndpoint(),
|
||||
http_trace.WithSpanNameFormatter(spanNameFormatter))
|
||||
}
|
||||
|
||||
func spanNameFormatter(_ string, r *http.Request) string {
|
||||
return r.Host + r.URL.EscapedPath()
|
||||
}
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
"github.com/caos/zitadel/internal/tracing/otel"
|
||||
"go.opentelemetry.io/otel/exporters/stdout"
|
||||
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Fraction float64
|
||||
Fraction float64
|
||||
MetricPrefix string
|
||||
}
|
||||
|
||||
type Tracer struct {
|
||||
otel.Tracer
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
if c.Fraction < 1 {
|
||||
c.Fraction = 1
|
||||
sampler := sdk_trace.ParentBased(sdk_trace.TraceIDRatioBased(c.Fraction))
|
||||
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tracing.T = &Tracer{trace.ProbabilitySampler(c.Fraction)}
|
||||
|
||||
return tracing.T.Start()
|
||||
tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"go.opencensus.io/examples/exporter"
|
||||
"go.opencensus.io/plugin/ocgrpc"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/stats/view"
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
type Tracer struct {
|
||||
sampler trace.Sampler
|
||||
}
|
||||
|
||||
func (t *Tracer) Start() error {
|
||||
trace.RegisterExporter(&exporter.PrintExporter{})
|
||||
|
||||
views := append(ocgrpc.DefaultServerViews, ocgrpc.DefaultClientViews...)
|
||||
views = append(views, ochttp.DefaultClientViews...)
|
||||
views = append(views, ochttp.DefaultServerViews...)
|
||||
|
||||
if err := view.Register(views...); err != nil {
|
||||
return errors.ThrowInternal(err, "LOG-PoFiB", "unable to register view")
|
||||
}
|
||||
|
||||
trace.ApplyConfig(trace.Config{DefaultSampler: t.sampler})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Tracer) Sampler() trace.Sampler {
|
||||
return t.sampler
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, trace.WithSpanKind(trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, trace.WithSpanKind(trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, trace.WithSpanKind(trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, trace.WithSpanKind(trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpan(ctx context.Context, caller string, options ...trace.StartOption) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, caller, options...)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpanFromName(ctx context.Context, name string, options ...trace.StartOption) (context.Context, *tracing.Span) {
|
||||
ctx, span := trace.StartSpan(ctx, name, options...)
|
||||
return ctx, tracing.CreateSpan(span)
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpanHTTP(r *http.Request, caller string) (*http.Request, *tracing.Span) {
|
||||
ctx, span := t.NewSpan(r.Context(), caller)
|
||||
r = r.WithContext(ctx)
|
||||
return r, span
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/caos/zitadel/internal/tracing (interfaces: Tracer)
|
||||
|
||||
// Package mock is a generated GoMock package.
|
||||
package mock
|
||||
|
||||
import (
|
||||
context "context"
|
||||
tracing "github.com/caos/zitadel/internal/tracing"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
trace "go.opencensus.io/trace"
|
||||
http "net/http"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockTracer is a mock of Tracer interface
|
||||
type MockTracer struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockTracerMockRecorder
|
||||
}
|
||||
|
||||
// MockTracerMockRecorder is the mock recorder for MockTracer
|
||||
type MockTracerMockRecorder struct {
|
||||
mock *MockTracer
|
||||
}
|
||||
|
||||
// NewMockTracer creates a new mock instance
|
||||
func NewMockTracer(ctrl *gomock.Controller) *MockTracer {
|
||||
mock := &MockTracer{ctrl: ctrl}
|
||||
mock.recorder = &MockTracerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockTracer) EXPECT() *MockTracerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// NewClientInterceptorSpan mocks base method
|
||||
func (m *MockTracer) NewClientInterceptorSpan(arg0 context.Context, arg1 string) (context.Context, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewClientInterceptorSpan", arg0, arg1)
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewClientInterceptorSpan indicates an expected call of NewClientInterceptorSpan
|
||||
func (mr *MockTracerMockRecorder) NewClientInterceptorSpan(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewClientInterceptorSpan", reflect.TypeOf((*MockTracer)(nil).NewClientInterceptorSpan), arg0, arg1)
|
||||
}
|
||||
|
||||
// NewClientSpan mocks base method
|
||||
func (m *MockTracer) NewClientSpan(arg0 context.Context, arg1 string) (context.Context, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewClientSpan", arg0, arg1)
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewClientSpan indicates an expected call of NewClientSpan
|
||||
func (mr *MockTracerMockRecorder) NewClientSpan(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewClientSpan", reflect.TypeOf((*MockTracer)(nil).NewClientSpan), arg0, arg1)
|
||||
}
|
||||
|
||||
// NewServerInterceptorSpan mocks base method
|
||||
func (m *MockTracer) NewServerInterceptorSpan(arg0 context.Context, arg1 string) (context.Context, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewServerInterceptorSpan", arg0, arg1)
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewServerInterceptorSpan indicates an expected call of NewServerInterceptorSpan
|
||||
func (mr *MockTracerMockRecorder) NewServerInterceptorSpan(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewServerInterceptorSpan", reflect.TypeOf((*MockTracer)(nil).NewServerInterceptorSpan), arg0, arg1)
|
||||
}
|
||||
|
||||
// NewServerSpan mocks base method
|
||||
func (m *MockTracer) NewServerSpan(arg0 context.Context, arg1 string) (context.Context, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewServerSpan", arg0, arg1)
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewServerSpan indicates an expected call of NewServerSpan
|
||||
func (mr *MockTracerMockRecorder) NewServerSpan(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewServerSpan", reflect.TypeOf((*MockTracer)(nil).NewServerSpan), arg0, arg1)
|
||||
}
|
||||
|
||||
// NewSpan mocks base method
|
||||
func (m *MockTracer) NewSpan(arg0 context.Context, arg1 string) (context.Context, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewSpan", arg0, arg1)
|
||||
ret0, _ := ret[0].(context.Context)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewSpan indicates an expected call of NewSpan
|
||||
func (mr *MockTracerMockRecorder) NewSpan(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewSpan", reflect.TypeOf((*MockTracer)(nil).NewSpan), arg0, arg1)
|
||||
}
|
||||
|
||||
// NewSpanHTTP mocks base method
|
||||
func (m *MockTracer) NewSpanHTTP(arg0 *http.Request, arg1 string) (*http.Request, *tracing.Span) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewSpanHTTP", arg0, arg1)
|
||||
ret0, _ := ret[0].(*http.Request)
|
||||
ret1, _ := ret[1].(*tracing.Span)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewSpanHTTP indicates an expected call of NewSpanHTTP
|
||||
func (mr *MockTracerMockRecorder) NewSpanHTTP(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewSpanHTTP", reflect.TypeOf((*MockTracer)(nil).NewSpanHTTP), arg0, arg1)
|
||||
}
|
||||
|
||||
// Sampler mocks base method
|
||||
func (m *MockTracer) Sampler() trace.Sampler {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Sampler")
|
||||
ret0, _ := ret[0].(trace.Sampler)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Sampler indicates an expected call of Sampler
|
||||
func (mr *MockTracerMockRecorder) Sampler() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sampler", reflect.TypeOf((*MockTracer)(nil).Sampler))
|
||||
}
|
||||
|
||||
// Start mocks base method
|
||||
func (m *MockTracer) Start() error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Start")
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Start indicates an expected call of Start
|
||||
func (mr *MockTracerMockRecorder) Start() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockTracer)(nil).Start))
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
func NewSimpleMockTracer(t *testing.T) *MockTracer {
|
||||
return NewMockTracer(gomock.NewController(t))
|
||||
}
|
||||
|
||||
func ExpectServerSpan(ctx context.Context, mock interface{}) {
|
||||
m := mock.(*MockTracer)
|
||||
any := gomock.Any()
|
||||
m.EXPECT().NewServerSpan(any, any).AnyTimes().Return(ctx, &tracing.Span{})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package otel
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
"go.opentelemetry.io/otel/exporters/otlp"
|
||||
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Fraction float64
|
||||
MetricPrefix string
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
sampler := sdk_trace.ParentBased(sdk_trace.TraceIDRatioBased(c.Fraction))
|
||||
exporter, err := otlp.NewExporter(otlp.WithAddress(c.Endpoint), otlp.WithInsecure())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tracing.T = NewTracer(c.MetricPrefix, sampler, exporter)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package otel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
"go.opentelemetry.io/otel/api/global"
|
||||
api_trace "go.opentelemetry.io/otel/api/trace"
|
||||
"go.opentelemetry.io/otel/propagators"
|
||||
"go.opentelemetry.io/otel/sdk/export/trace"
|
||||
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Tracer struct {
|
||||
Exporter api_trace.Tracer
|
||||
sampler sdk_trace.Sampler
|
||||
}
|
||||
|
||||
func NewTracer(name string, sampler sdk_trace.Sampler, exporter trace.SpanExporter) *Tracer {
|
||||
tp := sdk_trace.NewTracerProvider(
|
||||
sdk_trace.WithConfig(sdk_trace.Config{DefaultSampler: sampler}),
|
||||
sdk_trace.WithSyncer(exporter),
|
||||
)
|
||||
|
||||
global.SetTracerProvider(tp)
|
||||
tc := propagators.TraceContext{}
|
||||
global.SetTextMapPropagator(tc)
|
||||
|
||||
return &Tracer{Exporter: tp.Tracer(name), sampler: sampler}
|
||||
}
|
||||
|
||||
func (t *Tracer) Sampler() sdk_trace.Sampler {
|
||||
return t.sampler
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, api_trace.WithSpanKind(api_trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewServerSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, api_trace.WithSpanKind(api_trace.SpanKindServer))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, name, api_trace.WithSpanKind(api_trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewClientSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller, api_trace.WithSpanKind(api_trace.SpanKindClient))
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpan(ctx context.Context, caller string) (context.Context, *tracing.Span) {
|
||||
return t.newSpan(ctx, caller)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpan(ctx context.Context, caller string, options ...api_trace.SpanOption) (context.Context, *tracing.Span) {
|
||||
return t.newSpanFromName(ctx, caller, options...)
|
||||
}
|
||||
|
||||
func (t *Tracer) newSpanFromName(ctx context.Context, name string, options ...api_trace.SpanOption) (context.Context, *tracing.Span) {
|
||||
ctx, span := t.Exporter.Start(ctx, name, options...)
|
||||
return ctx, tracing.CreateSpan(span)
|
||||
}
|
||||
|
||||
func (t *Tracer) NewSpanHTTP(r *http.Request, caller string) (*http.Request, *tracing.Span) {
|
||||
ctx, span := t.NewSpan(r.Context(), caller)
|
||||
r = r.WithContext(ctx)
|
||||
return r, span
|
||||
}
|
||||
+14
-58
@@ -1,30 +1,29 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
errors2 "github.com/caos/zitadel/internal/api/grpc/errors"
|
||||
"strconv"
|
||||
"context"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
grpc_errs "github.com/caos/zitadel/internal/api/grpc/errors"
|
||||
api_trace "go.opentelemetry.io/otel/api/trace"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/label"
|
||||
)
|
||||
|
||||
type Span struct {
|
||||
span *trace.Span
|
||||
attributes []trace.Attribute
|
||||
span api_trace.Span
|
||||
opts []api_trace.SpanOption
|
||||
}
|
||||
|
||||
func CreateSpan(span *trace.Span) *Span {
|
||||
return &Span{span: span, attributes: []trace.Attribute{}}
|
||||
func CreateSpan(span api_trace.Span) *Span {
|
||||
return &Span{span: span, opts: []api_trace.SpanOption{}}
|
||||
}
|
||||
|
||||
func (s *Span) End() {
|
||||
if s.span == nil {
|
||||
return
|
||||
}
|
||||
s.span.AddAttributes(s.attributes...)
|
||||
s.span.End()
|
||||
|
||||
s.span.End(s.opts...)
|
||||
}
|
||||
|
||||
func (s *Span) EndWithError(err error) {
|
||||
@@ -36,53 +35,10 @@ func (s *Span) SetStatusByError(err error) {
|
||||
if s.span == nil {
|
||||
return
|
||||
}
|
||||
s.span.SetStatus(statusFromError(err))
|
||||
}
|
||||
|
||||
func statusFromError(err error) trace.Status {
|
||||
code, msg, _, _ := errors2.ExtractCaosError(err)
|
||||
return trace.Status{Code: int32(code), Message: msg}
|
||||
}
|
||||
|
||||
// AddAnnotation creates an annotation. The annotation will not be added to the tracing use Annotate(msg) afterwards
|
||||
func (s *Span) AddAnnotation(key string, value interface{}) *Span {
|
||||
attribute, err := toTraceAttribute(key, value)
|
||||
if err != nil {
|
||||
return s
|
||||
s.span.RecordError(context.TODO(), err, api_trace.WithErrorStatus(codes.Error))
|
||||
}
|
||||
s.attributes = append(s.attributes, attribute)
|
||||
return s
|
||||
}
|
||||
|
||||
// Annotate creates an annotation in tracing. Before added annotations will be set
|
||||
func (s *Span) Annotate(message string) *Span {
|
||||
if s.span == nil {
|
||||
return s
|
||||
}
|
||||
s.span.Annotate(s.attributes, message)
|
||||
s.attributes = []trace.Attribute{}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Span) Annotatef(format string, addiations ...interface{}) *Span {
|
||||
s.Annotate(fmt.Sprintf(format, addiations...))
|
||||
return s
|
||||
}
|
||||
|
||||
func toTraceAttribute(key string, value interface{}) (attr trace.Attribute, err error) {
|
||||
switch value := value.(type) {
|
||||
case bool:
|
||||
return trace.BoolAttribute(key, value), nil
|
||||
case string:
|
||||
return trace.StringAttribute(key, value), nil
|
||||
}
|
||||
if valueInt, err := convertToInt64(value); err == nil {
|
||||
return trace.Int64Attribute(key, valueInt), nil
|
||||
}
|
||||
return attr, errors.ThrowInternal(nil, "TRACE-jlq3s", "Attribute is not of type bool, string or int64")
|
||||
}
|
||||
|
||||
func convertToInt64(value interface{}) (int64, error) {
|
||||
valueString := fmt.Sprintf("%v", value)
|
||||
return strconv.ParseInt(valueString, 10, 64)
|
||||
code, msg, id, _ := grpc_errs.ExtractCaosError(err)
|
||||
s.span.SetAttributes(label.Uint32("grpc_code", uint32(code)), label.String("grpc_msg", msg), label.String("error_id", id))
|
||||
}
|
||||
|
||||
@@ -4,18 +4,18 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
api_trace "go.opentelemetry.io/otel/api/trace"
|
||||
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
type Tracer interface {
|
||||
Start() error
|
||||
NewSpan(ctx context.Context, caller string) (context.Context, *Span)
|
||||
NewClientSpan(ctx context.Context, caller string) (context.Context, *Span)
|
||||
NewServerSpan(ctx context.Context, caller string) (context.Context, *Span)
|
||||
NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *Span)
|
||||
NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *Span)
|
||||
NewSpanHTTP(r *http.Request, caller string) (*http.Request, *Span)
|
||||
Sampler() trace.Sampler
|
||||
Sampler() sdk_trace.Sampler
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
@@ -24,9 +24,9 @@ type Config interface {
|
||||
|
||||
var T Tracer
|
||||
|
||||
func Sampler() trace.Sampler {
|
||||
func Sampler() sdk_trace.Sampler {
|
||||
if T == nil {
|
||||
return trace.NeverSample()
|
||||
return sdk_trace.NeverSample()
|
||||
}
|
||||
return T.Sampler()
|
||||
}
|
||||
@@ -79,3 +79,7 @@ func NewSpanHTTP(r *http.Request) (*http.Request, *Span) {
|
||||
}
|
||||
return T.NewSpanHTTP(r, GetCaller())
|
||||
}
|
||||
|
||||
func TraceIDFromCtx(ctx context.Context) string {
|
||||
return api_trace.SpanFromContext(ctx).SpanContext().TraceID.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user