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 11 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 an EncodingType.
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
default:
return fmt.Errorf("invalid encoding type: %s", str)
}

return nil
}

// 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
55 changes: 55 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,57 @@ 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))
}

func TestUnmarshalEncoding(t *testing.T) {
tests := []struct {
name string
encodingBytes []byte
expected EncodingType
shouldError bool
}{
{
name: "UnmarshalEncodingProto",
encodingBytes: []byte("proto"),
expected: EncodingProto,
shouldError: false,
},
{
name: "UnmarshalEncodingJson",
encodingBytes: []byte("json"),
expected: EncodingJSON,
shouldError: false,
},
{
name: "UnmarshalEmptyEncoding",
encodingBytes: []byte(""),
shouldError: true,
},
{
name: "UnmarshalInvalidEncoding",
encodingBytes: []byte("invalid"),
shouldError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var encoding EncodingType
err := encoding.UnmarshalText(tt.encodingBytes)

if tt.shouldError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, encoding)
}
})
}
}
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
Loading
Loading