From 6d7a51a9db4c1a6c07352dad56580af904b25dfe Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Thu, 4 Apr 2024 12:21:41 +0300 Subject: [PATCH 1/3] feat: add query support --- .../otlptracegrpc/internal/otlpconfig/options.go | 8 ++++++++ exporters/otlp/otlptrace/otlptracehttp/client.go | 11 ++++++++++- .../otlptracehttp/internal/otlpconfig/options.go | 8 ++++++++ .../shared/otlp/otlptrace/otlpconfig/options.go.tmpl | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go index e3f7f431fe0..7f295bf2ccd 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go @@ -48,6 +48,7 @@ type ( Compression Compression Timeout time.Duration URLPath string + Query map[string][]string // gRPC configurations GRPCCredentials credentials.TransportCredentials @@ -346,3 +347,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption { return cfg }) } + +func WithQuery(query map[string][]string) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Query = query + return cfg + }) +} diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index 1c487a09630..96b48e1ee91 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -210,7 +210,7 @@ func (d *client) UploadTraces(ctx context.Context, protoSpans []*tracepb.Resourc } func (d *client) newRequest(body []byte) (request, error) { - u := url.URL{Scheme: d.getScheme(), Host: d.cfg.Endpoint, Path: d.cfg.URLPath} + u := url.URL{Scheme: d.getScheme(), Host: d.cfg.Endpoint, Path: d.cfg.URLPath, RawQuery: d.getRawQuery()} r, err := http.NewRequest(http.MethodPost, u.String(), nil) if err != nil { return request{Request: r}, err @@ -346,3 +346,12 @@ func (d *client) contextWithStop(ctx context.Context) (context.Context, context. }(ctx, cancel) return ctx, cancel } + +func (d *client) getRawQuery() string { + values := url.Values{} + for k, v := range d.cfg.Query { + values[k] = v + } + + return values.Encode() +} diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go index 45fefc4dd8c..483eda1cea0 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -48,6 +48,7 @@ type ( Compression Compression Timeout time.Duration URLPath string + Query map[string][]string // gRPC configurations GRPCCredentials credentials.TransportCredentials @@ -346,3 +347,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption { return cfg }) } + +func WithQuery(query map[string][]string) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Query = query + return cfg + }) +} diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl index 0a64afe3ce6..d98e09dc431 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl @@ -48,6 +48,7 @@ type ( Compression Compression Timeout time.Duration URLPath string + Query map[string][]string // gRPC configurations GRPCCredentials credentials.TransportCredentials @@ -346,3 +347,10 @@ func WithProxy(pf HTTPTransportProxyFunc) GenericOption { return cfg }) } + +func WithQuery(query map[string][]string) GenericOption { + return newGenericOption(func(cfg Config) Config { + cfg.Traces.Query = query + return cfg + }) +} From bbb7b401f2d48d4e64814ca01dbe9e287dd8a2c1 Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Thu, 4 Apr 2024 12:24:00 +0300 Subject: [PATCH 2/3] feat: add test --- .../otlptracehttp/internal/otlpconfig/options_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go index 1df421a34dc..0881217b121 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options_test.go @@ -433,6 +433,17 @@ func TestConfigs(t *testing.T) { assert.Nil(t, c.Traces.Proxy) }, }, + + // Query tests + { + name: "Test With Query", + opts: []GenericOption{ + WithQuery(map[string][]string{"queryKey": {"queryVal"}}), + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Equal(t, map[string][]string{"queryKey": {"queryVal"}}, c.Traces.Query) + }, + }, } for _, tt := range tests { From a14cc3a6428678ed8dd298f0fff9353c3a8e165b Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Thu, 4 Apr 2024 12:33:17 +0300 Subject: [PATCH 3/3] fix: add changelog --- CHANGELOG.md | 1 + exporters/otlp/otlptrace/otlptracegrpc/options.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a5af468a04..6a805c5fb3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `otel.scope.name` and `otel.scope.version` tags to spans exported by `go.opentelemetry.io/otel/exporters/zipkin`. (#5108) - Add support for `AddLink` to `go.opentelemetry.io/otel/bridge/opencensus`. (#5116) - Add `String` method to `Value` and `KeyValue` in `go.opentelemetry.io/otel/log`. (#5117) +- Add `Query` option in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5118) ### Changed diff --git a/exporters/otlp/otlptrace/otlptracegrpc/options.go b/exporters/otlp/otlptrace/otlptracegrpc/options.go index d7559bfb5a1..8e836b3f461 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/options.go @@ -200,3 +200,8 @@ func WithTimeout(duration time.Duration) Option { func WithRetry(settings RetryConfig) Option { return wrappedOption{otlpconfig.WithRetry(retry.Config(settings))} } + +// WithQuery will send the provided query values with each gRPC request. +func WithQuery(query map[string][]string) Option { + return wrappedOption{otlpconfig.WithQuery(query)} +}