Skip to content

Commit

Permalink
[extension/encoding][otlpencodingextension] Add support for profiles …
Browse files Browse the repository at this point in the history
…signal (#36008)

#### Description
Add support for profiles signal to `encodingextension`.
  • Loading branch information
haoqixu authored Nov 14, 2024
1 parent 6db5d1a commit c06be6d
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 27 deletions.
27 changes: 27 additions & 0 deletions .chloggen/f-profiles-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: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for profiles signal to encodingextension

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36008]

# (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 extension/encoding/avrologencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/avrologencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions extension/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
)

Expand Down Expand Up @@ -45,3 +46,15 @@ type TracesUnmarshalerExtension interface {
extension.Extension
ptrace.Unmarshaler
}

// ProfilesMarshalerExtension is an extension that marshals profiles.
type ProfilesMarshalerExtension interface {
extension.Extension
pprofile.Marshaler
}

// ProfilesUnmarshalerExtension is an extension that unmarshals Profiles.
type ProfilesUnmarshalerExtension interface {
extension.Extension
pprofile.Unmarshaler
}
1 change: 1 addition & 0 deletions extension/encoding/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
go.opentelemetry.io/collector/extension v0.113.0
go.opentelemetry.io/collector/pdata v1.19.0
go.opentelemetry.io/collector/pdata/pprofile v0.113.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extension/encoding/jaegerencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.113.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/collector/semconv v0.113.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/jaegerencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extension/encoding/jsonlogencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/jsonlogencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 46 additions & 27 deletions extension/encoding/otlpencodingextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding"
Expand All @@ -21,22 +22,26 @@ const (
)

var (
_ encoding.TracesMarshalerExtension = (*otlpExtension)(nil)
_ encoding.TracesUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.LogsMarshalerExtension = (*otlpExtension)(nil)
_ encoding.LogsUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.MetricsMarshalerExtension = (*otlpExtension)(nil)
_ encoding.MetricsUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.TracesMarshalerExtension = (*otlpExtension)(nil)
_ encoding.TracesUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.LogsMarshalerExtension = (*otlpExtension)(nil)
_ encoding.LogsUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.MetricsMarshalerExtension = (*otlpExtension)(nil)
_ encoding.MetricsUnmarshalerExtension = (*otlpExtension)(nil)
_ encoding.ProfilesMarshalerExtension = (*otlpExtension)(nil)
_ encoding.ProfilesUnmarshalerExtension = (*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
config *Config
traceMarshaler ptrace.Marshaler
traceUnmarshaler ptrace.Unmarshaler
logMarshaler plog.Marshaler
logUnmarshaler plog.Unmarshaler
metricMarshaler pmetric.Marshaler
metricUnmarshaler pmetric.Unmarshaler
profileMarshaler pprofile.Marshaler
profileUnmarshaler pprofile.Unmarshaler
}

func newExtension(config *Config) (*otlpExtension, error) {
Expand All @@ -46,23 +51,27 @@ func newExtension(config *Config) (*otlpExtension, error) {
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{},
config: config,
traceMarshaler: &ptrace.ProtoMarshaler{},
traceUnmarshaler: &ptrace.ProtoUnmarshaler{},
logMarshaler: &plog.ProtoMarshaler{},
logUnmarshaler: &plog.ProtoUnmarshaler{},
metricMarshaler: &pmetric.ProtoMarshaler{},
metricUnmarshaler: &pmetric.ProtoUnmarshaler{},
profileMarshaler: &pprofile.ProtoMarshaler{},
profileUnmarshaler: &pprofile.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{},
config: config,
traceMarshaler: &ptrace.JSONMarshaler{},
traceUnmarshaler: &ptrace.JSONUnmarshaler{},
logMarshaler: &plog.JSONMarshaler{},
logUnmarshaler: &plog.JSONUnmarshaler{},
metricMarshaler: &pmetric.JSONMarshaler{},
metricUnmarshaler: &pmetric.JSONUnmarshaler{},
profileMarshaler: &pprofile.JSONMarshaler{},
profileUnmarshaler: &pprofile.JSONUnmarshaler{},
}
default:
err = fmt.Errorf("unsupported protocol: %q", protocol)
Expand Down Expand Up @@ -95,6 +104,16 @@ func (ex *otlpExtension) MarshalLogs(logs plog.Logs) ([]byte, error) {
return ex.logMarshaler.MarshalLogs(logs)
}

// UnmarshalProfiles implements encoding.ProfilesUnmarshalerExtension.
func (ex *otlpExtension) UnmarshalProfiles(buf []byte) (pprofile.Profiles, error) {
return ex.profileUnmarshaler.UnmarshalProfiles(buf)
}

// MarshalProfiles implements encoding.ProfilesMarshalerExtension.
func (ex *otlpExtension) MarshalProfiles(profiles pprofile.Profiles) ([]byte, error) {
return ex.profileMarshaler.MarshalProfiles(profiles)
}

func (ex *otlpExtension) Start(_ context.Context, _ component.Host) error {
return nil
}
Expand Down
26 changes: 26 additions & 0 deletions extension/encoding/otlpencodingextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
)

Expand Down Expand Up @@ -73,30 +74,40 @@ func testOTLPMarshal(ex *otlpExtension, t *testing.T) {
metrics := generateMetrics()
_, err = ex.MarshalMetrics(metrics)
require.NoError(t, err)

profiles := generateProfiles()
_, err = ex.MarshalProfiles(profiles)
require.NoError(t, err)
}

func testOTLPUnmarshal(ex *otlpExtension, t *testing.T) {
traces := generateTraces()
logs := generateLogs()
metrics := generateMetrics()
profiles := generateProfiles()

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)
profileBuf, err := ex.MarshalProfiles(profiles)
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)
profiles0, err := ex.UnmarshalProfiles(profileBuf)
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())
require.Equal(t, profiles0.ResourceProfiles().Len(), profiles.ResourceProfiles().Len())
}

func TestOTLPJSONMarshal(t *testing.T) {
Expand Down Expand Up @@ -181,3 +192,18 @@ func generateMetrics() pmetric.Metrics {
}
return md
}

func generateProfiles() pprofile.Profiles {
var num = 10
now := time.Now()
pd := pprofile.NewProfiles()
ilm := pd.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty()
ilm.Profiles().EnsureCapacity(num)
for i := 0; i < num; i++ {
im := ilm.Profiles().AppendEmpty()
im.SetProfileID(pprofile.ProfileID([16]byte{0x01, 0x02, 0x03, 0x04}))
im.SetStartTime(pcommon.NewTimestampFromTime(now))
im.SetEndTime(pcommon.NewTimestampFromTime(now))
}
return pd
}
1 change: 1 addition & 0 deletions extension/encoding/otlpencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
go.opentelemetry.io/collector/confmap v1.19.0
go.opentelemetry.io/collector/extension v0.113.0
go.opentelemetry.io/collector/pdata v1.19.0
go.opentelemetry.io/collector/pdata/pprofile v0.113.0
go.uber.org/goleak v1.3.0
)

Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/otlpencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extension/encoding/textencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/textencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extension/encoding/zipkinencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/collector/semconv v0.113.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/zipkinencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c06be6d

Please sign in to comment.