mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-21 20:38:47 +00:00
add tracing and refactor some api pkgs
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/caos/logging"
|
||||
)
|
||||
|
||||
func GetCaller() string {
|
||||
fpcs := make([]uintptr, 1)
|
||||
|
||||
n := runtime.Callers(3, fpcs)
|
||||
if n == 0 {
|
||||
logging.Log("HELPE-rWjfC").Debug("no caller")
|
||||
}
|
||||
|
||||
caller := runtime.FuncForPC(fpcs[0] - 1)
|
||||
if caller == nil {
|
||||
logging.Log("HELPE-25POw").Debug("caller was nil")
|
||||
}
|
||||
|
||||
// Print the name of the function
|
||||
return caller.Name()
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
tracing_g "github.com/caos/zitadel/internal/tracing/google"
|
||||
tracing_log "github.com/caos/zitadel/internal/tracing/log"
|
||||
)
|
||||
|
||||
type TracingConfig struct {
|
||||
Type string
|
||||
Config tracing.Config
|
||||
}
|
||||
|
||||
var tracer = map[string]func() tracing.Config{
|
||||
"google": func() tracing.Config { return &tracing_g.Config{} },
|
||||
"log": func() tracing.Config { return &tracing_log.Config{} },
|
||||
}
|
||||
|
||||
func (c *TracingConfig) UnmarshalJSON(data []byte) error {
|
||||
var rc struct {
|
||||
Type string
|
||||
Config json.RawMessage
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &rc); err != nil {
|
||||
return status.Errorf(codes.Internal, "%v parse config: %v", "TRACE-vmjS", err)
|
||||
}
|
||||
|
||||
c.Type = rc.Type
|
||||
|
||||
var err error
|
||||
c.Config, err = newTracingConfig(c.Type, rc.Config)
|
||||
if err != nil {
|
||||
return status.Errorf(codes.Internal, "%v parse config: %v", "TRACE-Ws9E", err)
|
||||
}
|
||||
|
||||
return c.Config.NewTracer()
|
||||
}
|
||||
|
||||
func newTracingConfig(tracerType string, configData []byte) (tracing.Config, error) {
|
||||
t, ok := tracer[tracerType]
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.Internal, "%v No config: %v", "TRACE-HMEJ", tracerType)
|
||||
}
|
||||
|
||||
tracingConfig := t()
|
||||
if len(configData) == 0 {
|
||||
return tracingConfig, nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(configData, tracingConfig); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "%v Could not read conifg: %v", "TRACE-1tSS", err)
|
||||
}
|
||||
|
||||
return tracingConfig, nil
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package tracing
|
||||
|
||||
//go:generate mockgen -package mock -destination mock/tracing_mock.go github.com/caos/zitadel/internal/tracing Tracer
|
||||
@@ -0,0 +1,25 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"go.opencensus.io/trace"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ProjectID string
|
||||
MetricPrefix string
|
||||
Fraction float64
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
if !envIsSet() {
|
||||
return status.Error(codes.InvalidArgument, "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,95 @@
|
||||
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, ", ")})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
func TraceHandler(handler http.Handler, ignoredMethods ...string) http.Handler {
|
||||
healthEndpoints := strings.Join(ignoredMethods, ";;")
|
||||
|
||||
return &ochttp.Handler{
|
||||
Handler: handler,
|
||||
FormatSpanName: func(r *http.Request) string {
|
||||
host := r.URL.Host
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
}
|
||||
return host + r.URL.Path
|
||||
},
|
||||
|
||||
StartOptions: trace.StartOptions{Sampler: Sampler()},
|
||||
IsHealthEndpoint: func(r *http.Request) bool {
|
||||
n := strings.Contains(healthEndpoints, r.URL.RequestURI())
|
||||
return n
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"go.opencensus.io/trace"
|
||||
|
||||
"github.com/caos/zitadel/internal/tracing"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Fraction float64
|
||||
}
|
||||
|
||||
func (c *Config) NewTracer() error {
|
||||
if c.Fraction < 1 {
|
||||
c.Fraction = 1
|
||||
}
|
||||
|
||||
tracing.T = &Tracer{trace.ProbabilitySampler(c.Fraction)}
|
||||
|
||||
return tracing.T.Start()
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// 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))
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
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,89 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type Span struct {
|
||||
span *trace.Span
|
||||
attributes []trace.Attribute
|
||||
}
|
||||
|
||||
func CreateSpan(span *trace.Span) *Span {
|
||||
return &Span{span: span, attributes: []trace.Attribute{}}
|
||||
}
|
||||
|
||||
func (s *Span) End() {
|
||||
if s.span == nil {
|
||||
return
|
||||
}
|
||||
s.span.AddAttributes(s.attributes...)
|
||||
s.span.End()
|
||||
}
|
||||
|
||||
func (s *Span) EndWithError(err error) {
|
||||
s.SetStatusByError(err)
|
||||
s.End()
|
||||
}
|
||||
|
||||
func (s *Span) SetStatusByError(err error) {
|
||||
if s.span == nil {
|
||||
return
|
||||
}
|
||||
s.span.SetStatus(statusFromError(err))
|
||||
}
|
||||
|
||||
func statusFromError(err error) trace.Status {
|
||||
if statusErr, ok := status.FromError(err); ok {
|
||||
return trace.Status{Code: int32(statusErr.Code()), Message: statusErr.Message()}
|
||||
}
|
||||
return trace.Status{Code: int32(codes.Unknown), Message: "Unknown"}
|
||||
}
|
||||
|
||||
// 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.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, status.Error(codes.InvalidArgument, "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)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package tracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"go.opencensus.io/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
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
NewTracer() error
|
||||
}
|
||||
|
||||
var T Tracer
|
||||
|
||||
func Sampler() trace.Sampler {
|
||||
if T == nil {
|
||||
return trace.NeverSample()
|
||||
}
|
||||
return T.Sampler()
|
||||
}
|
||||
|
||||
func NewSpan(ctx context.Context) (context.Context, *Span) {
|
||||
if T == nil {
|
||||
return ctx, CreateSpan(nil)
|
||||
}
|
||||
return T.NewSpan(ctx, GetCaller())
|
||||
}
|
||||
|
||||
func NewClientSpan(ctx context.Context) (context.Context, *Span) {
|
||||
if T == nil {
|
||||
return ctx, CreateSpan(nil)
|
||||
}
|
||||
return T.NewClientSpan(ctx, GetCaller())
|
||||
}
|
||||
|
||||
func NewServerSpan(ctx context.Context) (context.Context, *Span) {
|
||||
if T == nil {
|
||||
return ctx, CreateSpan(nil)
|
||||
}
|
||||
return T.NewServerSpan(ctx, GetCaller())
|
||||
}
|
||||
|
||||
func NewClientInterceptorSpan(ctx context.Context, name string) (context.Context, *Span) {
|
||||
if T == nil {
|
||||
return ctx, CreateSpan(nil)
|
||||
}
|
||||
return T.NewClientInterceptorSpan(ctx, name)
|
||||
}
|
||||
|
||||
func NewServerInterceptorSpan(ctx context.Context, name string) (context.Context, *Span) {
|
||||
if T == nil {
|
||||
return ctx, CreateSpan(nil)
|
||||
}
|
||||
return T.NewServerInterceptorSpan(ctx, name)
|
||||
}
|
||||
|
||||
func NewSpanHTTP(r *http.Request) (*http.Request, *Span) {
|
||||
if T == nil {
|
||||
return r, CreateSpan(nil)
|
||||
}
|
||||
return T.NewSpanHTTP(r, GetCaller())
|
||||
}
|
||||
Reference in New Issue
Block a user