forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/encodingextension] Introduce OTLP encoding extension (open…
…-telemetry#27875) Introduce OTLP encoding extension. For more context, see open-telemetry#27564
- Loading branch information
1 parent
4b91be0
commit 132912b
Showing
18 changed files
with
520 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: otlpencodingextension | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Introduce OTLP encoding extension | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [6272] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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: [] |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# OTLP encoding extension | ||
|
||
<!-- status autogenerated section --> | ||
| Status | | | ||
| ------------- |-----------| | ||
| Stability | [development] | | ||
| Distributions | [] | | ||
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aextension%2Fotlpencoding%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aextension%2Fotlpencoding) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aextension%2Fotlpencoding%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aextension%2Fotlpencoding) | | ||
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@dao-jun](https://www.github.com/dao-jun), [@VihasMakwana](https://www.github.com/VihasMakwana) | | ||
|
||
[development]: https://github.com/open-telemetry/opentelemetry-collector#development | ||
<!-- end autogenerated section --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/otlpencodingextension" | ||
import ( | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
var _ component.ConfigValidator = (*Config)(nil) | ||
|
||
type Config struct { | ||
Protocol string `mapstructure:"protocol"` | ||
} | ||
|
||
func (c *Config) Validate() error { | ||
if c.Protocol != otlpProto && c.Protocol != otlpJSON { | ||
return fmt.Errorf("unsupported protocol: %q", c.Protocol) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:generate mdatagen metadata.yaml | ||
package otlpencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/otlpencodingextension" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/otlpencodingextension" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
const ( | ||
otlpProto = "otlp_proto" | ||
otlpJSON = "otlp_json" | ||
) | ||
|
||
var ( | ||
_ component.Component = (*otlpExtension)(nil) | ||
_ ptrace.Marshaler = (*otlpExtension)(nil) | ||
_ ptrace.Unmarshaler = (*otlpExtension)(nil) | ||
_ plog.Marshaler = (*otlpExtension)(nil) | ||
_ plog.Unmarshaler = (*otlpExtension)(nil) | ||
_ pmetric.Marshaler = (*otlpExtension)(nil) | ||
_ pmetric.Unmarshaler = (*otlpExtension)(nil) | ||
) | ||
|
||
type otlpExtension struct { | ||
config *Config | ||
traceMarshaler ptrace.Marshaler | ||
traceUnmarshaler ptrace.Unmarshaler | ||
logMarshaler plog.Marshaler | ||
logUnmarshaler plog.Unmarshaler | ||
metricMarshaler pmetric.Marshaler | ||
metricUnmarshaler pmetric.Unmarshaler | ||
} | ||
|
||
func newExtension(config *Config) (*otlpExtension, error) { | ||
var ex *otlpExtension | ||
var err error | ||
protocol := config.Protocol | ||
switch protocol { | ||
case otlpProto: | ||
ex = &otlpExtension{ | ||
config: config, | ||
traceMarshaler: &ptrace.ProtoMarshaler{}, | ||
traceUnmarshaler: &ptrace.ProtoUnmarshaler{}, | ||
logMarshaler: &plog.ProtoMarshaler{}, | ||
logUnmarshaler: &plog.ProtoUnmarshaler{}, | ||
metricMarshaler: &pmetric.ProtoMarshaler{}, | ||
metricUnmarshaler: &pmetric.ProtoUnmarshaler{}, | ||
} | ||
case otlpJSON: | ||
ex = &otlpExtension{ | ||
config: config, | ||
traceMarshaler: &ptrace.JSONMarshaler{}, | ||
traceUnmarshaler: &ptrace.JSONUnmarshaler{}, | ||
logMarshaler: &plog.JSONMarshaler{}, | ||
logUnmarshaler: &plog.JSONUnmarshaler{}, | ||
metricMarshaler: &pmetric.JSONMarshaler{}, | ||
metricUnmarshaler: &pmetric.JSONUnmarshaler{}, | ||
} | ||
default: | ||
err = fmt.Errorf("unsupported protocol: %q", protocol) | ||
} | ||
|
||
return ex, err | ||
} | ||
|
||
func (ex *otlpExtension) UnmarshalTraces(buf []byte) (ptrace.Traces, error) { | ||
return ex.traceUnmarshaler.UnmarshalTraces(buf) | ||
} | ||
|
||
func (ex *otlpExtension) MarshalTraces(traces ptrace.Traces) ([]byte, error) { | ||
return ex.traceMarshaler.MarshalTraces(traces) | ||
} | ||
|
||
func (ex *otlpExtension) UnmarshalMetrics(buf []byte) (pmetric.Metrics, error) { | ||
return ex.metricUnmarshaler.UnmarshalMetrics(buf) | ||
} | ||
|
||
func (ex *otlpExtension) MarshalMetrics(metrics pmetric.Metrics) ([]byte, error) { | ||
return ex.metricMarshaler.MarshalMetrics(metrics) | ||
} | ||
|
||
func (ex *otlpExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) { | ||
return ex.logUnmarshaler.UnmarshalLogs(buf) | ||
} | ||
|
||
func (ex *otlpExtension) MarshalLogs(logs plog.Logs) ([]byte, error) { | ||
return ex.logMarshaler.MarshalLogs(logs) | ||
} | ||
|
||
func (ex *otlpExtension) Start(_ context.Context, _ component.Host) error { | ||
return nil | ||
} | ||
|
||
func (ex *otlpExtension) Shutdown(_ context.Context) error { | ||
return nil | ||
} |
136 changes: 136 additions & 0 deletions
136
extension/encoding/otlpencodingextension/extension_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/otlpencodingextension" | ||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
func testOTLPMarshal(ex *otlpExtension, t *testing.T) { | ||
traces := generateTraces() | ||
_, err := ex.MarshalTraces(traces) | ||
require.NoError(t, err) | ||
|
||
logs := generateLogs() | ||
_, err = ex.MarshalLogs(logs) | ||
require.NoError(t, err) | ||
|
||
metrics := generateMetrics() | ||
_, err = ex.MarshalMetrics(metrics) | ||
require.NoError(t, err) | ||
} | ||
|
||
func testOTLPUnmarshal(ex *otlpExtension, t *testing.T) { | ||
traces := generateTraces() | ||
logs := generateLogs() | ||
metrics := generateMetrics() | ||
|
||
traceBuf, err := ex.MarshalTraces(traces) | ||
require.NoError(t, err) | ||
logBuf, err := ex.MarshalLogs(logs) | ||
require.NoError(t, err) | ||
metricBuf, err := ex.MarshalMetrics(metrics) | ||
require.NoError(t, err) | ||
|
||
traces0, err := ex.UnmarshalTraces(traceBuf) | ||
require.NoError(t, err) | ||
logs0, err := ex.UnmarshalLogs(logBuf) | ||
require.NoError(t, err) | ||
metrics0, err := ex.UnmarshalMetrics(metricBuf) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, traces0.ResourceSpans().Len(), traces.ResourceSpans().Len()) | ||
require.Equal(t, logs0.ResourceLogs().Len(), logs.ResourceLogs().Len()) | ||
require.Equal(t, metrics0.ResourceMetrics().Len(), metrics.ResourceMetrics().Len()) | ||
} | ||
|
||
func TestOTLPJSONMarshal(t *testing.T) { | ||
conf := &Config{Protocol: otlpJSON} | ||
ex := createAndExtension0(conf, t) | ||
|
||
testOTLPMarshal(ex, t) | ||
} | ||
|
||
func TestOTLPProtoMarshal(t *testing.T) { | ||
conf := &Config{Protocol: otlpProto} | ||
ex := createAndExtension0(conf, t) | ||
|
||
testOTLPMarshal(ex, t) | ||
} | ||
|
||
func TestOTLPJSONUnmarshal(t *testing.T) { | ||
conf := &Config{Protocol: otlpJSON} | ||
ex := createAndExtension0(conf, t) | ||
testOTLPUnmarshal(ex, t) | ||
} | ||
|
||
func TestOTLPProtoUnmarshal(t *testing.T) { | ||
conf := &Config{Protocol: otlpProto} | ||
ex := createAndExtension0(conf, t) | ||
|
||
testOTLPUnmarshal(ex, t) | ||
} | ||
|
||
// createAndExtension0 Create extension | ||
func createAndExtension0(c *Config, t *testing.T) *otlpExtension { | ||
ex, err := newExtension(c) | ||
require.NoError(t, err) | ||
err = ex.Start(context.TODO(), nil) | ||
require.NoError(t, err) | ||
return ex | ||
} | ||
|
||
func generateTraces() ptrace.Traces { | ||
var num = 10 | ||
now := time.Now() | ||
md := ptrace.NewTraces() | ||
ilm := md.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty() | ||
ilm.Spans().EnsureCapacity(num) | ||
for i := 0; i < num; i++ { | ||
im := ilm.Spans().AppendEmpty() | ||
im.SetName("test_name") | ||
im.SetStartTimestamp(pcommon.NewTimestampFromTime(now)) | ||
im.SetEndTimestamp(pcommon.NewTimestampFromTime(now)) | ||
} | ||
return md | ||
} | ||
|
||
func generateLogs() plog.Logs { | ||
var num = 10 | ||
md := plog.NewLogs() | ||
ilm := md.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() | ||
ilm.LogRecords().EnsureCapacity(num) | ||
for i := 0; i < num; i++ { | ||
im := ilm.LogRecords().AppendEmpty() | ||
im.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) | ||
} | ||
return md | ||
} | ||
|
||
func generateMetrics() pmetric.Metrics { | ||
var num = 10 | ||
now := time.Now() | ||
startTime := pcommon.NewTimestampFromTime(now.Add(-10 * time.Second)) | ||
endTime := pcommon.NewTimestampFromTime(now) | ||
|
||
md := pmetric.NewMetrics() | ||
ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty() | ||
ilm.Metrics().EnsureCapacity(num) | ||
for i := 0; i < num; i++ { | ||
im := ilm.Metrics().AppendEmpty() | ||
im.SetName("test_name") | ||
idp := im.SetEmptySum().DataPoints().AppendEmpty() | ||
idp.SetStartTimestamp(startTime) | ||
idp.SetTimestamp(endTime) | ||
idp.SetIntValue(123) | ||
} | ||
return md | ||
} |
Oops, something went wrong.