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

[extension/encodingextension] Introduce OTLP encoding extension #27875

Merged
merged 13 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions .chloggen/otlp_encoding_extension.yaml
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: []
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ extension/encoding/jaegerencodingextension/ @open-te
extension/encoding/jsonlogencodingextension/ @open-telemetry/collector-contrib-approvers @VihasMakwana @atoulme
extension/encoding/textencodingextension/ @open-telemetry/collector-contrib-approvers @MovieStoreGuy @atoulme
extension/encoding/zipkinencodingextension/ @open-telemetry/collector-contrib-approvers @MovieStoreGuy @dao-jun
extension/encoding/otlpencodingextension/ @open-telemetry/collector-contrib-approvers @MovieStoreGuy @dao-jun
extension/headerssetterextension/ @open-telemetry/collector-contrib-approvers @jpkrohling
extension/healthcheckextension/ @open-telemetry/collector-contrib-approvers @jpkrohling
extension/httpforwarder/ @open-telemetry/collector-contrib-approvers @atoulme @rmfitzpatrick
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ body:
- extension/bearertokenauth
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
- extension/encoding/textencoding
- extension/encoding/zipkinencoding
- extension/headerssetter
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ body:
- extension/bearertokenauth
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
- extension/encoding/textencoding
- extension/encoding/zipkinencoding
- extension/headerssetter
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ body:
- extension/bearertokenauth
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
- extension/encoding/textencoding
- extension/encoding/zipkinencoding
- extension/headerssetter
Expand Down
10 changes: 5 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ updates:
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/exporter/honeycombmarkerexporter"
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/exporter/influxdbexporter"
schedule:
Expand Down Expand Up @@ -362,6 +357,11 @@ updates:
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/extension/opampextension"
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/extension/pprofextension"
schedule:
Expand Down
1 change: 1 addition & 0 deletions extension/encoding/otlpencodingextension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../Makefile.Common
12 changes: 12 additions & 0 deletions extension/encoding/otlpencodingextension/README.md
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 -->
23 changes: 23 additions & 0 deletions extension/encoding/otlpencodingextension/config.go
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
}
5 changes: 5 additions & 0 deletions extension/encoding/otlpencodingextension/doc.go
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"
103 changes: 103 additions & 0 deletions extension/encoding/otlpencodingextension/extension.go
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 extension/encoding/otlpencodingextension/extension_test.go
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
}
Loading