Skip to content

Commit

Permalink
Merge pull request #1210 from Permify/next
Browse files Browse the repository at this point in the history
feat: flag and env variables added for tracer and meter headers
  • Loading branch information
tolgaOzen committed Apr 26, 2024
2 parents e29be50 + 759d295 commit bace015
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
28 changes: 14 additions & 14 deletions internal/config/config.go
Expand Up @@ -89,22 +89,22 @@ type (

// Tracer contains configuration for distributed tracing.
Tracer struct {
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the tracing exporter, if not defined /v1/trace will be used
Headers map[string]string `mapstructure:"headers"`
Enabled bool `mapstructure:"enabled"` // Whether tracing collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for tracing data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the tracing exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the tracing exporter, if not defined /v1/trace will be used
Headers []string `mapstructure:"headers"`
}

// Meter contains configuration for metrics collection and reporting.
Meter struct {
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the metrics exporter, if not defined /v1/metrics will be used
Headers map[string]string `mapstructure:"headers"`
Enabled bool `mapstructure:"enabled"` // Whether metrics collection is enabled
Exporter string `mapstructure:"exporter"` // Exporter for metrics data
Endpoint string `mapstructure:"endpoint"` // Endpoint for the metrics exporter
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the metrics exporter, if not defined /v1/metrics will be used
Headers []string `mapstructure:"headers"`
}

// Service contains configuration for various service-level features.
Expand Down Expand Up @@ -277,13 +277,13 @@ func DefaultConfig() *Config {
},
Tracer: Tracer{
Enabled: false,
Headers: map[string]string{},
Headers: []string{},
},
Meter: Meter{
Enabled: true,
Exporter: "otlp",
Endpoint: "telemetry.permify.co",
Headers: map[string]string{},
Headers: []string{},
},
Service: Service{
CircuitBreaker: false,
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/config.go
Expand Up @@ -54,11 +54,13 @@ func NewConfigCommand() *cobra.Command {
f.String("tracer-endpoint", conf.Tracer.Endpoint, "export uri for tracing data")
f.Bool("tracer-insecure", conf.Tracer.Insecure, "use https or http for tracer data, only used for otlp exporter or signoz")
f.String("tracer-urlpath", conf.Tracer.URLPath, "allow to set url path for otlp exporter")
f.StringSlice("tracer-headers", conf.Tracer.Headers, "allows setting custom headers for the tracer exporter in key-value pairs")
f.Bool("meter-enabled", conf.Meter.Enabled, "switch option for metric")
f.String("meter-exporter", conf.Meter.Exporter, "can be; otlp. (integrated metric tools)")
f.String("meter-endpoint", conf.Meter.Endpoint, "export uri for metric data")
f.Bool("meter-insecure", conf.Meter.Insecure, "use https or http for metric data")
f.String("meter-urlpath", conf.Meter.URLPath, "allow to set url path for otlp exporter")
f.StringSlice("meter-headers", conf.Meter.Headers, "allows setting custom headers for the metric exporter in key-value pairs")
f.Bool("service-circuit-breaker", conf.Service.CircuitBreaker, "switch option for service circuit breaker")
f.Bool("service-watch-enabled", conf.Service.Watch.Enabled, "switch option for watch service")
f.Int64("service-schema-cache-number-of-counters", conf.Service.Schema.Cache.NumberOfCounters, "schema service cache number of counters")
Expand Down Expand Up @@ -153,12 +155,14 @@ func conf() func(cmd *cobra.Command, args []string) error {
[]string{"tracer.endpoint", HideSecret(cfg.Tracer.Exporter), getKeyOrigin(cmd, "tracer-endpoint", "PERMIFY_TRACER_ENDPOINT")},
[]string{"tracer.insecure", fmt.Sprintf("%v", cfg.Tracer.Insecure), getKeyOrigin(cmd, "tracer-insecure", "PERMIFY_TRACER_INSECURE")},
[]string{"tracer.urlpath", cfg.Tracer.URLPath, getKeyOrigin(cmd, "tracer-urlpath", "PERMIFY_TRACER_URL_PATH")},
[]string{"tracer.headers", fmt.Sprintf("%v", cfg.Tracer.Headers), getKeyOrigin(cmd, "tracer-headers", "PERMIFY_TRACER_HEADERS")},
// METER
[]string{"meter.enabled", fmt.Sprintf("%v", cfg.Meter.Enabled), getKeyOrigin(cmd, "meter-enabled", "PERMIFY_METER_ENABLED")},
[]string{"meter.exporter", cfg.Meter.Exporter, getKeyOrigin(cmd, "meter-exporter", "PERMIFY_METER_EXPORTER")},
[]string{"meter.endpoint", HideSecret(cfg.Meter.Exporter), getKeyOrigin(cmd, "meter-endpoint", "PERMIFY_METER_ENDPOINT")},
[]string{"meter.insecure", fmt.Sprintf("%v", cfg.Meter.Insecure), getKeyOrigin(cmd, "meter-insecure", "PERMIFY_METER_INSECURE")},
[]string{"meter.urlpath", cfg.Meter.URLPath, getKeyOrigin(cmd, "meter-urlpath", "PERMIFY_METER_URL_PATH")},
[]string{"meter.headers", fmt.Sprintf("%v", cfg.Meter.Headers), getKeyOrigin(cmd, "meter-headers", "PERMIFY_METER_HEADERS")},
// SERVICE
[]string{"service.circuit_breaker", fmt.Sprintf("%v", cfg.Service.CircuitBreaker), getKeyOrigin(cmd, "service-circuit-breaker", "PERMIFY_SERVICE_CIRCUIT_BREAKER")},
[]string{"service.schema.cache.number_of_counters", fmt.Sprintf("%v", cfg.Service.Schema.Cache.NumberOfCounters), getKeyOrigin(cmd, "service-schema-cache-number-of-counters", "PERMIFY_SERVICE_WATCH_ENABLED")},
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/flags/serve.go
Expand Up @@ -224,6 +224,13 @@ func RegisterServeFlags(flags *pflag.FlagSet) {
panic(err)
}

if err = viper.BindPFlag("tracer.headers", flags.Lookup("tracer-headers")); err != nil {
panic(err)
}
if err = viper.BindEnv("tracer.headers", "PERMIFY_TRACER_HEADERS"); err != nil {
panic(err)
}

// METER
if err = viper.BindPFlag("meter.enabled", flags.Lookup("meter-enabled")); err != nil {
panic(err)
Expand Down Expand Up @@ -260,6 +267,13 @@ func RegisterServeFlags(flags *pflag.FlagSet) {
panic(err)
}

if err = viper.BindPFlag("meter.headers", flags.Lookup("meter-headers")); err != nil {
panic(err)
}
if err = viper.BindEnv("meter.headers", "PERMIFY_METER_HEADERS"); err != nil {
panic(err)
}

// SERVICE
if err = viper.BindPFlag("service.circuit_breaker", flags.Lookup("service-circuit-breaker")); err != nil {
panic(err)
Expand Down
25 changes: 23 additions & 2 deletions pkg/cmd/serve.go
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -84,11 +85,13 @@ func NewServeCommand() *cobra.Command {
f.String("tracer-endpoint", conf.Tracer.Endpoint, "export uri for tracing data")
f.Bool("tracer-insecure", conf.Tracer.Insecure, "use https or http for tracer data, only used for otlp exporter or signoz")
f.String("tracer-urlpath", conf.Tracer.URLPath, "allow to set url path for otlp exporter")
f.StringSlice("tracer-headers", conf.Tracer.Headers, "allows setting custom headers for the tracer exporter in key-value pairs")
f.Bool("meter-enabled", conf.Meter.Enabled, "switch option for metric")
f.String("meter-exporter", conf.Meter.Exporter, "can be; otlp. (integrated metric tools)")
f.String("meter-endpoint", conf.Meter.Endpoint, "export uri for metric data")
f.Bool("meter-insecure", conf.Meter.Insecure, "use https or http for metric data")
f.String("meter-urlpath", conf.Meter.URLPath, "allow to set url path for otlp exporter")
f.StringSlice("meter-headers", conf.Meter.Headers, "allows setting custom headers for the metric exporter in key-value pairs")
f.Bool("service-circuit-breaker", conf.Service.CircuitBreaker, "switch option for service circuit breaker")
f.Bool("service-watch-enabled", conf.Service.Watch.Enabled, "switch option for watch service")
f.Int64("service-schema-cache-number-of-counters", conf.Service.Schema.Cache.NumberOfCounters, "schema service cache number of counters")
Expand Down Expand Up @@ -222,13 +225,22 @@ func serve() func(cmd *cobra.Command, args []string) error {

// Tracing
if cfg.Tracer.Enabled {
headers := map[string]string{}
for _, header := range cfg.Tracer.Headers {
h := strings.Split(header, ":")
if len(h) != 2 {
return errors.New("invalid header format; expected 'key:value'")
}
headers[h[0]] = h[1]
}

var exporter trace.SpanExporter
exporter, err = tracerexporters.ExporterFactory(
cfg.Tracer.Exporter,
cfg.Tracer.Endpoint,
cfg.Tracer.Insecure,
cfg.Tracer.URLPath,
cfg.Tracer.Headers,
headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down Expand Up @@ -265,13 +277,22 @@ func serve() func(cmd *cobra.Command, args []string) error {
// Meter
meter := telemetry.NewNoopMeter()
if cfg.Meter.Enabled {
headers := map[string]string{}
for _, header := range cfg.Meter.Headers {
h := strings.Split(header, ":")
if len(h) != 2 {
return errors.New("invalid header format; expected 'key:value'")
}
headers[h[0]] = h[1]
}

var exporter metric.Exporter
exporter, err = meterexporters.ExporterFactory(
cfg.Meter.Exporter,
cfg.Meter.Endpoint,
cfg.Meter.Insecure,
cfg.Meter.URLPath,
cfg.Meter.Headers,
headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down

0 comments on commit bace015

Please sign in to comment.