Skip to content

Commit

Permalink
add WithHeaders option for zipkin exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
srijan-27 committed Jun 20, 2024
1 parent 773aec2 commit 6d74c4d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Store and provide the emitted `context.Context` in `ScopeRecords` of `go.opentelemetry.io/otel/sdk/log/logtest`. (#5468)
- `SimpleProcessor.OnEmit` in `go.opentelemetry.io/otel/sdk/log` no longer allocates a slice which makes it possible to have a zero-allocation log processing using `SimpleProcessor`. (#5493)
- The `AssertRecordEqual` method to `go.opentelemetry.io/otel/log/logtest` to allow comparison of two log records in tests. (#5499)
- The `WithHeaders` option to `go.opentelemetry.io/otel/exporters/zipkin` to allow configuring custom http headers while exporting spans. (#3474)

### Changed

Expand All @@ -40,7 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Prevent random number generation data-race for experimental rand exemplars in `go.opentelemetry.io/otel/sdk/metric`. (#5456)
- Fix counting number of dropped attributes of `Record` in `go.opentelemetry.io/otel/sdk/log`. (#5464)
- Fix panic in baggage creation when a member contains 0x80 char in key or value. (#5494)
- Correct comments for the priority of the `WithEndpoint` and `WithEndpointURL` options and their coresponding environment variables in in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`. (#5508)
- Correct comments for the priority of the `WithEndpoint` and `WithEndpointURL` options and their corresponding environment variables in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`. (#5508)
- Fix stale timestamps reported by the lastvalue aggregation. (#5517)

## [1.27.0/0.49.0/0.3.0] 2024-05-21
Expand Down
36 changes: 28 additions & 8 deletions exporters/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const (

// Exporter exports spans to the zipkin collector.
type Exporter struct {
url string
client *http.Client
logger logr.Logger
url string
client *http.Client
logger logr.Logger
headers map[string]string

stoppedMu sync.RWMutex
stopped bool
Expand All @@ -40,8 +41,9 @@ var emptyLogger = logr.Logger{}

// Options contains configuration for the exporter.
type config struct {
client *http.Client
logger logr.Logger
client *http.Client
logger logr.Logger
headers map[string]string
}

// Option defines a function that configures the exporter.
Expand Down Expand Up @@ -70,6 +72,14 @@ func WithLogr(logger logr.Logger) Option {
})
}

// WithHeaders configures the exporter to use the passed http request headers.
func WithHeaders(headers map[string]string) Option {
return optionFunc(func(cfg config) config {
cfg.headers = headers
return cfg
})
}

// WithClient configures the exporter to use the passed HTTP client.
func WithClient(client *http.Client) Option {
return optionFunc(func(cfg config) config {
Expand Down Expand Up @@ -101,9 +111,10 @@ func New(collectorURL string, opts ...Option) (*Exporter, error) {
cfg.client = http.DefaultClient
}
return &Exporter{
url: collectorURL,
client: cfg.client,
logger: cfg.logger,
url: collectorURL,
client: cfg.client,
logger: cfg.logger,
headers: cfg.headers,
}, nil
}

Expand Down Expand Up @@ -132,6 +143,15 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpa
return e.errf("failed to create request to %s: %v", e.url, err)
}
req.Header.Set("Content-Type", "application/json")

for k, v := range e.headers {
if k == "host" {
req.Host = v
} else {
req.Header.Set(k, v)

Check warning on line 151 in exporters/zipkin/zipkin.go

View check run for this annotation

Codecov / codecov/patch

exporters/zipkin/zipkin.go#L148-L151

Added lines #L148 - L151 were not covered by tests
}
}

resp, err := e.client.Do(req)
if err != nil {
return e.errf("request to %s failed: %v", e.url, err)
Expand Down
14 changes: 14 additions & 0 deletions exporters/zipkin/zipkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,17 @@ func TestLogrFormatting(t *testing.T) {
got := buf.String()
assert.Equal(t, want, got)
}

func TestWithHeaders(t *testing.T) {
headers := map[string]string{
"name1": "value1",
"name2": "value2",
}

exp, err := New("", WithHeaders(headers))
require.NoError(t, err)

want := headers
got := exp.headers
assert.Equal(t, want, got)
}

0 comments on commit 6d74c4d

Please sign in to comment.