Skip to content

Commit

Permalink
feat: headers option for otlp
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgaOzen committed Apr 25, 2024
1 parent 68a08a9 commit 205d128
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
24 changes: 14 additions & 10 deletions internal/config/config.go
Expand Up @@ -89,20 +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
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"`
}

// 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
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"`
}

// Service contains configuration for various service-level features.
Expand Down Expand Up @@ -275,11 +277,13 @@ func DefaultConfig() *Config {
},
Tracer: Tracer{
Enabled: false,
Headers: map[string]string{},
},
Meter: Meter{
Enabled: true,
Exporter: "otlp",
Endpoint: "telemetry.permify.co",
Headers: map[string]string{},
},
Service: Service{
CircuitBreaker: false,
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/serve.go
Expand Up @@ -10,8 +10,6 @@ import (
"syscall"
"time"

"github.com/davecgh/go-spew/spew"

"github.com/sony/gobreaker"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -158,8 +156,6 @@ func serve() func(cmd *cobra.Command, args []string) error {
}
}

spew.Dump(cfg)

// Print banner and initialize logger
internal.PrintBanner()

Expand Down Expand Up @@ -232,6 +228,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Tracer.Endpoint,
cfg.Tracer.Insecure,
cfg.Tracer.URLPath,
cfg.Tracer.Headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down Expand Up @@ -274,6 +271,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Meter.Endpoint,
cfg.Meter.Insecure,
cfg.Meter.URLPath,
cfg.Meter.Headers,
)
if err != nil {
slog.Error(err.Error())
Expand Down
6 changes: 3 additions & 3 deletions pkg/telemetry/meterexporters/factory.go
Expand Up @@ -7,12 +7,12 @@ import (
)

// ExporterFactory - Create meter exporter according to given params
func ExporterFactory(name, endpoint string, insecure bool, urlpath string) (metric.Exporter, error) {
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
switch name {
case "otlp", "otlp-http":
return NewOTLP(endpoint, insecure, urlpath)
return NewOTLP(endpoint, insecure, urlpath, headers)
case "otlp-grpc":
return NewOTLPGRPC(endpoint, insecure)
return NewOTLPGrpc(endpoint, insecure, headers)
default:
return nil, fmt.Errorf("%s meter exporter is unsupported", name)
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/meterexporters/otlp.go
Expand Up @@ -8,12 +8,16 @@ import (
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string) (metric.Exporter, error) {
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
options := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(endpoint),
}

if len(headers) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(headers))
}

if urlpath != "" {
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/telemetry/meterexporters/otlp_grpc.go
Expand Up @@ -2,24 +2,29 @@ package meterexporters

import (
"context"

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/grpc/credentials"
)

// NewOTLPGrpc - Creates new OTLP exporter using GRPC protocol.
func NewOTLPGrpc(endpoint string, insecure bool) (metric.Exporter, error) {
func NewOTLPGrpc(endpoint string, insecure bool, headers map[string]string) (metric.Exporter, error) {
options := []otlpmetricgrpc.Option{
otlpmetricgrpc.WithEndpoint(endpoint),
otlpmetricgrpc.WithHeaders(headers),
}

if len(headers) > 0 {
options = append(options, otlpmetricgrpc.WithHeaders(headers))
}

if insecure {
options = append(options, otlpmetricgrpc.WithInsecure())
} else {
options = append(options, otlpmetricgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
}

exporter, err := otlpmetricgrpc.New(context.Background(), options...)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/telemetry/tracerexporters/factory.go
Expand Up @@ -7,16 +7,16 @@ import (
)

// ExporterFactory - Create tracer exporter according to given params
func ExporterFactory(name, url string, insecure bool, urlpath string) (trace.SpanExporter, error) {
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
switch name {
case "zipkin":
return NewZipkin(url)
case "jaeger":
return NewJaegar(url)
case "otlp", "otlp-http":
return NewOTLP(url, insecure, urlpath)
return NewOTLP(url, insecure, urlpath, headers)
case "otlp-grpc":
return NewOTLPGrpc(url, insecure)
return NewOTLPGrpc(url, insecure, headers)
case "signoz":
return NewSigNoz(url, insecure)
default:
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/tracerexporters/otlp.go
Expand Up @@ -8,14 +8,18 @@ import (
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string) (trace.SpanExporter, error) {
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
var exporter trace.SpanExporter
var err error

opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(endpoint),
}

if len(headers) > 0 {
opts = append(opts, otlptracehttp.WithHeaders(headers))
}

if urlpath != "" {
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/tracerexporters/otlp_grpc.go
Expand Up @@ -9,14 +9,18 @@ import (
)

// NewOTLPGrpc - Creates new OTLP exporter using GRPC protocol.
func NewOTLPGrpc(endpoint string, insecure bool) (trace.SpanExporter, error) {
func NewOTLPGrpc(endpoint string, insecure bool, headers map[string]string) (trace.SpanExporter, error) {
var exporter trace.SpanExporter
var err error

opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(endpoint),
}

if len(headers) > 0 {
opts = append(opts, otlptracegrpc.WithHeaders(headers))
}

if insecure {
opts = append(opts, otlptracegrpc.WithInsecure())
} else {
Expand Down

0 comments on commit 205d128

Please sign in to comment.