feat: enable tracing (#3528)

This commit is contained in:
Livio Amstutz
2022-04-28 14:44:13 +02:00
committed by GitHub
parent 08ae39ae19
commit 44a2b81bef
6 changed files with 76 additions and 70 deletions

View File

@@ -1,69 +1,34 @@
package config
import (
"encoding/json"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
"github.com/zitadel/zitadel/internal/telemetry/tracing/google"
"github.com/zitadel/zitadel/internal/telemetry/tracing/log"
"github.com/zitadel/zitadel/internal/telemetry/tracing/otel"
)
type TracingConfig struct {
type Config struct {
Type string
Config tracing.Config
Config map[string]interface{} `mapstructure:",remain"`
}
var tracer = map[string]func() tracing.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{} },
}
func (c *TracingConfig) UnmarshalJSON(data []byte) error {
var rc struct {
Type string
Config json.RawMessage
}
if err := json.Unmarshal(data, &rc); err != nil {
return errors.ThrowInternal(err, "TRACE-vmjS", "error parsing config")
}
c.Type = rc.Type
var err error
c.Config, err = newTracingConfig(c.Type, rc.Config)
if err != nil {
return err
}
return c.Config.NewTracer()
}
func newTracingConfig(tracerType string, configData []byte) (tracing.Config, error) {
t, ok := tracer[tracerType]
func (c *Config) NewTracer() error {
t, ok := tracer[c.Type]
if !ok {
return nil, errors.ThrowInternalf(nil, "TRACE-HMEJ", "config type %s not supported", tracerType)
return errors.ThrowInternalf(nil, "TRACE-dsbjh", "config type %s not supported", c.Type)
}
tracingConfig := t()
if len(configData) == 0 {
return tracingConfig, nil
}
if err := json.Unmarshal(configData, tracingConfig); err != nil {
return nil, errors.ThrowInternal(err, "TRACE-1tSS", "Could not read config: %v")
}
return tracingConfig, nil
return t(c.Config)
}
type NoTracing struct{}
var tracer = map[string]func(map[string]interface{}) error{
"otel": otel.NewTracerFromConfig,
"google": google.NewTracer,
"log": log.NewTracer,
"none": NoTracer,
"": NoTracer,
}
func (_ *NoTracing) NewTracer() error {
func NoTracer(_ map[string]interface{}) error {
return nil
}