Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/otlphttp] added support for configurable telemetry encoding #9276

Merged
merged 18 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/otlphttp-json-encoding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: otlphttpexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for json content encoding when exporting telemetry

# One or more tracking issues or pull requests related to the change
issues: [6945]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
10 changes: 10 additions & 0 deletions exporter/otlphttpexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following settings can be optionally configured:
- `timeout` (default = 30s): HTTP request time limit. For details see https://golang.org/pkg/net/http/#Client
- `read_buffer_size` (default = 0): ReadBufferSize for HTTP client.
- `write_buffer_size` (default = 512 * 1024): WriteBufferSize for HTTP client.
- `encoding` (default = proto): The encoding to use for the messages (valid options: `proto`, `json`)

Example:

Expand All @@ -55,5 +56,14 @@ exporters:
compression: none
```

By default `proto` encoding is used, to change the content encoding of the message configure it as follows:

```yaml
exporters:
otlphttp:
...
encoding: json
```

The full list of settings exposed for this exporter are documented [here](./config.go)
with detailed sample configurations [here](./testdata/config.yaml).
34 changes: 34 additions & 0 deletions exporter/otlphttpexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,45 @@
package otlphttpexporter // import "go.opentelemetry.io/collector/exporter/otlphttpexporter"

import (
"encoding"
"errors"
"fmt"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

// EncodingType defines the type for content encoding
type EncodingType string
tvaintrob marked this conversation as resolved.
Show resolved Hide resolved
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

const (
EncodingProto EncodingType = "proto"
EncodingJSON EncodingType = "json"
)

var _ encoding.TextUnmarshaler = (*EncodingType)(nil)

// UnmarshalText unmarshalls text to a Level.
tvaintrob marked this conversation as resolved.
Show resolved Hide resolved
func (e *EncodingType) UnmarshalText(text []byte) error {
if e == nil {
return errors.New("cannot unmarshal to a nil *EncodingType")
}

Check warning on line 31 in exporter/otlphttpexporter/config.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/config.go#L30-L31

Added lines #L30 - L31 were not covered by tests

str := string(text)
switch str {
case string(EncodingProto):
*e = EncodingProto
case string(EncodingJSON):
*e = EncodingJSON

Check warning on line 38 in exporter/otlphttpexporter/config.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/config.go#L35-L38

Added lines #L35 - L38 were not covered by tests
default:
return fmt.Errorf("invalid encoding type: %s", str)
}

return nil

Check warning on line 43 in exporter/otlphttpexporter/config.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/config.go#L43

Added line #L43 was not covered by tests
}

// Config defines configuration for OTLP/HTTP exporter.
type Config struct {
confighttp.HTTPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
Expand All @@ -26,6 +57,9 @@

// The URL to send logs to. If omitted the Endpoint + "/v1/logs" will be used.
LogsEndpoint string `mapstructure:"logs_endpoint"`

// The encoding to export telemetry (default: "proto")
Encoding EncodingType `mapstructure:"encoding"`
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

var _ component.Config = (*Config)(nil)
Expand Down
9 changes: 9 additions & 0 deletions exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestUnmarshalConfig(t *testing.T) {
NumConsumers: 2,
QueueSize: 10,
},
Encoding: EncodingProto,
HTTPClientSettings: confighttp.HTTPClientSettings{
Headers: map[string]configopaque.String{
"can you have a . here?": "F0000000-0000-0000-0000-000000000000",
Expand All @@ -73,3 +74,11 @@ func TestUnmarshalConfig(t *testing.T) {
},
}, cfg)
}

func TestUnmarshalConfigInvalidEncoding(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "bad_invalid_encoding.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.Error(t, component.UnmarshalConfig(cm, cfg))
}
1 change: 1 addition & 0 deletions exporter/otlphttpexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func createDefaultConfig() component.Config {
return &Config{
RetryConfig: configretry.NewDefaultBackOffConfig(),
QueueConfig: exporterhelper.NewDefaultQueueSettings(),
Encoding: EncodingProto,
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "",
Timeout: 30 * time.Second,
Expand Down
15 changes: 15 additions & 0 deletions exporter/otlphttpexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestCreateDefaultConfig(t *testing.T) {
assert.Equal(t, ocfg.RetryConfig.MaxInterval, 30*time.Second, "default retry MaxInterval")
assert.Equal(t, ocfg.QueueConfig.Enabled, true, "default sending queue is enabled")
assert.Equal(t, ocfg.Compression, configcompression.Gzip)
assert.Equal(t, ocfg.Encoding, EncodingProto)
}

func TestCreateMetricsExporter(t *testing.T) {
Expand Down Expand Up @@ -154,6 +155,20 @@ func TestCreateTracesExporter(t *testing.T) {
},
},
},
{
name: "ProtoEncoding",
config: &Config{
Encoding: EncodingProto,
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: endpoint},
},
},
{
name: "JSONEncoding",
config: &Config{
Encoding: EncodingJSON,
HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: endpoint},
},
},
}

for _, tt := range tests {
Expand Down
119 changes: 92 additions & 27 deletions exporter/otlphttpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
headerRetryAfter = "Retry-After"
maxHTTPResponseReadBytes = 64 * 1024

jsonContentType = "application/json"
protobufContentType = "application/x-protobuf"
)

Expand Down Expand Up @@ -87,7 +88,18 @@

func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) error {
tr := ptraceotlp.NewExportRequestFromTraces(td)
request, err := tr.MarshalProto()

var err error
var request []byte
switch e.config.Encoding {
case EncodingJSON:
request, err = tr.MarshalJSON()
case EncodingProto:
request, err = tr.MarshalProto()
default:
tvaintrob marked this conversation as resolved.
Show resolved Hide resolved
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("invalid encoding: %s", e.config.Encoding)

Check warning on line 100 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}

if err != nil {
return consumererror.NewPermanent(err)
}
Expand All @@ -97,7 +109,18 @@

func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error {
tr := pmetricotlp.NewExportRequestFromMetrics(md)
request, err := tr.MarshalProto()

var err error
var request []byte
switch e.config.Encoding {
case EncodingJSON:
request, err = tr.MarshalJSON()
case EncodingProto:
request, err = tr.MarshalProto()
default:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("invalid encoding: %s", e.config.Encoding)

Check warning on line 121 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L120-L121

Added lines #L120 - L121 were not covered by tests
}

if err != nil {
return consumererror.NewPermanent(err)
}
Expand All @@ -106,7 +129,18 @@

func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) error {
tr := plogotlp.NewExportRequestFromLogs(ld)
request, err := tr.MarshalProto()

var err error
var request []byte
switch e.config.Encoding {
case EncodingJSON:
request, err = tr.MarshalJSON()
case EncodingProto:
request, err = tr.MarshalProto()
default:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("invalid encoding: %s", e.config.Encoding)

Check warning on line 141 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}

if err != nil {
return consumererror.NewPermanent(err)
}
Expand All @@ -120,7 +154,16 @@
if err != nil {
return consumererror.NewPermanent(err)
}
req.Header.Set("Content-Type", protobufContentType)

switch e.config.Encoding {
case EncodingJSON:
req.Header.Set("Content-Type", jsonContentType)
case EncodingProto:
req.Header.Set("Content-Type", protobufContentType)
default:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("invalid encoding: %s", e.config.Encoding)

Check warning on line 164 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L163-L164

Added lines #L163 - L164 were not covered by tests
}

req.Header.Set("User-Agent", e.userAgent)

resp, err := e.client.Do(req)
Expand Down Expand Up @@ -231,7 +274,6 @@
// "Response body for all HTTP 4xx and HTTP 5xx responses MUST be a
// Protobuf-encoded Status message that describes the problem."
respBytes, err := readResponseBody(resp)

if err != nil {
return nil
}
Expand All @@ -249,7 +291,6 @@

func handlePartialSuccessResponse(resp *http.Response, partialSuccessHandler partialSuccessHandler) error {
bodyBytes, err := readResponseBody(resp)

if err != nil {
return err
}
Expand All @@ -259,47 +300,71 @@

type partialSuccessHandler func(bytes []byte, contentType string) error

func tracesPartialSuccessHandler(protoBytes []byte, contentType string) error {
if contentType != protobufContentType {
return nil
}
func tracesPartialSuccessHandler(responseBytes []byte, contentType string) error {
exportResponse := ptraceotlp.NewExportResponse()
err := exportResponse.UnmarshalProto(protoBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
switch contentType {
case protobufContentType:
err := exportResponse.UnmarshalProto(responseBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
}
case jsonContentType:
err := exportResponse.UnmarshalJSON(responseBytes)
if err != nil {
return fmt.Errorf("error parsing json response: %w", err)
}

Check warning on line 315 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L311-L315

Added lines #L311 - L315 were not covered by tests
default:
return nil
tvaintrob marked this conversation as resolved.
Show resolved Hide resolved
}

partialSuccess := exportResponse.PartialSuccess()
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedSpans() == 0) {
return consumererror.NewPermanent(fmt.Errorf("OTLP partial success: %s (%d rejected)", partialSuccess.ErrorMessage(), partialSuccess.RejectedSpans()))
}
return nil
}

func metricsPartialSuccessHandler(protoBytes []byte, contentType string) error {
if contentType != protobufContentType {
return nil
}
func metricsPartialSuccessHandler(responseBytes []byte, contentType string) error {
exportResponse := pmetricotlp.NewExportResponse()
err := exportResponse.UnmarshalProto(protoBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
switch contentType {
case protobufContentType:
err := exportResponse.UnmarshalProto(responseBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
}
case jsonContentType:
err := exportResponse.UnmarshalJSON(responseBytes)
if err != nil {
return fmt.Errorf("error parsing json response: %w", err)
}

Check warning on line 339 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L335-L339

Added lines #L335 - L339 were not covered by tests
default:
return nil
songy23 marked this conversation as resolved.
Show resolved Hide resolved
}

partialSuccess := exportResponse.PartialSuccess()
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedDataPoints() == 0) {
return consumererror.NewPermanent(fmt.Errorf("OTLP partial success: %s (%d rejected)", partialSuccess.ErrorMessage(), partialSuccess.RejectedDataPoints()))
}
return nil
}

func logsPartialSuccessHandler(protoBytes []byte, contentType string) error {
if contentType != protobufContentType {
return nil
}
func logsPartialSuccessHandler(responseBytes []byte, contentType string) error {
exportResponse := plogotlp.NewExportResponse()
err := exportResponse.UnmarshalProto(protoBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
switch contentType {
case protobufContentType:
err := exportResponse.UnmarshalProto(responseBytes)
if err != nil {
return fmt.Errorf("error parsing protobuf response: %w", err)
}
case jsonContentType:
err := exportResponse.UnmarshalJSON(responseBytes)
if err != nil {
return fmt.Errorf("error parsing json response: %w", err)
}

Check warning on line 363 in exporter/otlphttpexporter/otlp.go

View check run for this annotation

Codecov / codecov/patch

exporter/otlphttpexporter/otlp.go#L359-L363

Added lines #L359 - L363 were not covered by tests
default:
return nil
songy23 marked this conversation as resolved.
Show resolved Hide resolved
}

partialSuccess := exportResponse.PartialSuccess()
if !(partialSuccess.ErrorMessage() == "" && partialSuccess.RejectedLogRecords() == 0) {
return consumererror.NewPermanent(fmt.Errorf("OTLP partial success: %s (%d rejected)", partialSuccess.ErrorMessage(), partialSuccess.RejectedLogRecords()))
Expand Down
Loading
Loading