Skip to content

Commit

Permalink
feat: add propagators to tracing config struct
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Aguilar <pablo.aguilar@outlook.com.br>
  • Loading branch information
thepabloaguilar committed Apr 14, 2024
1 parent 10a02d5 commit 408ac24
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ func Default() *Config {
Enabled: false,
Exporter: TracingJaeger,
SamplingRatio: 1,
Propagators: []TracingPropagator{
TracingPropagatorTraceContext,
TracingPropagatorBaggage,
},
Jaeger: JaegerTracingConfig{
Host: "localhost",
Port: 6831,
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func TestLoad(t *testing.T) {
path: "./testdata/tracing/wrong_sampling_ratio.yml",
wantErr: errors.New("sampling ratio should be a number between 0 and 1"),
},
{
name: "tracing with wrong propagator",
path: "./testdata/tracing/wrong_propagator.yml",
wantErr: errors.New("invalid propagator option: wrong_propagator"),
},
{
name: "tracing otlp",
path: "./testdata/tracing/otlp.yml",
Expand Down Expand Up @@ -586,6 +591,10 @@ func TestLoad(t *testing.T) {
Enabled: true,
Exporter: TracingOTLP,
SamplingRatio: 1,
Propagators: []TracingPropagator{
TracingPropagatorTraceContext,
TracingPropagatorBaggage,
},
Jaeger: JaegerTracingConfig{
Host: "localhost",
Port: 6831,
Expand Down
4 changes: 4 additions & 0 deletions internal/config/testdata/tracing/wrong_propagator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tracing:
enabled: true
propagators:
- wrong_propagator
40 changes: 40 additions & 0 deletions internal/config/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"errors"
"fmt"

"github.com/spf13/viper"
)
Expand All @@ -15,6 +16,7 @@ var _ defaulter = (*TracingConfig)(nil)
type TracingConfig struct {
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Exporter TracingExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"`
Propagators []TracingPropagator `json:"propagators,omitempty" mapstructure:"propagators" yaml:"propagators,omitempty"`
SamplingRatio float64 `json:"samplingRatio,omitempty" mapstructure:"samplingRatio" yaml:"samplingRatio,omitempty"`
Jaeger JaegerTracingConfig `json:"jaeger,omitempty" mapstructure:"jaeger" yaml:"jaeger,omitempty"`
Zipkin ZipkinTracingConfig `json:"zipkin,omitempty" mapstructure:"zipkin" yaml:"zipkin,omitempty"`
Expand All @@ -26,6 +28,10 @@ func (c *TracingConfig) setDefaults(v *viper.Viper) error {
"enabled": false,
"exporter": TracingJaeger,
"samplingRatio": 1,
"propagators": []TracingPropagator{
TracingPropagatorTraceContext,
TracingPropagatorBaggage,
},
"jaeger": map[string]any{
"host": "localhost",
"port": 6831,
Expand All @@ -46,6 +52,12 @@ func (c *TracingConfig) validate() error {
return errors.New("sampling ratio should be a number between 0 and 1")
}

for _, propagator := range c.Propagators {
if !propagator.isValid() {
return fmt.Errorf("invalid propagator option: %s", propagator)
}
}

return nil
}

Expand Down Expand Up @@ -105,6 +117,34 @@ var (
}
)

type TracingPropagator string

const (
TracingPropagatorTraceContext TracingPropagator = "tracecontext"
TracingPropagatorBaggage TracingPropagator = "baggage"
TracingPropagatorB3 TracingPropagator = "b3"
TracingPropagatorB3Multi TracingPropagator = "b3multi"
TracingPropagatorJaeger TracingPropagator = "jaeger"
TracingPropagatorXRay TracingPropagator = "xray"
TracingPropagatorOtTrace TracingPropagator = "ottrace"
TracingPropagatorNone TracingPropagator = "none"
)

func (t TracingPropagator) isValid() bool {
validOptions := map[TracingPropagator]bool{
TracingPropagatorTraceContext: true,
TracingPropagatorBaggage: true,
TracingPropagatorB3: true,
TracingPropagatorB3Multi: true,
TracingPropagatorJaeger: true,
TracingPropagatorXRay: true,
TracingPropagatorOtTrace: true,
TracingPropagatorNone: true,
}

return validOptions[t]
}

// JaegerTracingConfig contains fields, which configure
// Jaeger span and tracing output destination.
type JaegerTracingConfig struct {
Expand Down

0 comments on commit 408ac24

Please sign in to comment.