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

chore: add support for json codec in jaeger #28625

Merged
merged 5 commits into from
Jan 8, 2024
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/add-json-jaeger.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: encoding/jaegerencodingextension

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

# 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: []
2 changes: 2 additions & 0 deletions extension/encoding/jaegerencodingextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type JaegerProtocol string

const (
JaegerProtocolProtobuf JaegerProtocol = "protobuf"
JaegerProtocolJSON JaegerProtocol = "json"
)

type Config struct {
Expand All @@ -19,6 +20,7 @@ type Config struct {
func (c *Config) Validate() error {
switch c.Protocol {
case JaegerProtocolProtobuf:
case JaegerProtocolJSON:
default:
return fmt.Errorf("invalid protocol %q", c.Protocol)
}
Expand Down
3 changes: 3 additions & 0 deletions extension/encoding/jaegerencodingextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

var _ encoding.TracesUnmarshalerExtension = &jaegerExtension{}
var _ ptrace.Unmarshaler = &jaegerExtension{}

type jaegerExtension struct {
config *Config
Expand All @@ -28,6 +29,8 @@ func (e *jaegerExtension) Start(_ context.Context, _ component.Host) error {
switch e.config.Protocol {
case JaegerProtocolProtobuf:
e.unmarshaler = jaegerProtobufTrace{}
case JaegerProtocolJSON:
e.unmarshaler = jaegerJSONTrace{}
default:
return fmt.Errorf("unsupported protocol: %q", e.config.Protocol)
}
Expand Down
2 changes: 1 addition & 1 deletion extension/encoding/jaegerencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/extension/encod
go 1.20

require (
github.com/gogo/protobuf v1.3.2
github.com/jaegertracing/jaeger v1.52.0
github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding v0.91.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger v0.91.0
Expand All @@ -17,7 +18,6 @@ require (
require (
github.com/apache/thrift v0.19.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
19 changes: 19 additions & 0 deletions extension/encoding/jaegerencodingextension/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package jaegerencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/jaegerencodingextension"

import (
"bytes"

"github.com/gogo/protobuf/jsonpb"
jaegerproto "github.com/jaegertracing/jaeger/model"
"go.opentelemetry.io/collector/pdata/ptrace"

Expand All @@ -19,6 +22,22 @@ func (j jaegerProtobufTrace) UnmarshalTraces(buf []byte) (ptrace.Traces, error)
if err != nil {
return ptrace.NewTraces(), err
}
return jaegerSpanToTraces(span)
}

type jaegerJSONTrace struct {
}

func (j jaegerJSONTrace) UnmarshalTraces(buf []byte) (ptrace.Traces, error) {
span := &jaegerproto.Span{}
err := jsonpb.Unmarshal(bytes.NewReader(buf), span)
if err != nil {
return ptrace.NewTraces(), err
}
return jaegerSpanToTraces(span)
}

func jaegerSpanToTraces(span *jaegerproto.Span) (ptrace.Traces, error) {
batch := jaegerproto.Batch{
Spans: []*jaegerproto.Span{span},
Process: span.Process,
Expand Down
60 changes: 60 additions & 0 deletions extension/encoding/jaegerencodingextension/jaeger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package jaegerencodingextension

import (
"bytes"
"testing"

"github.com/gogo/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)

func TestUnmarshalJaeger(t *testing.T) {
td := ptrace.NewTraces()
span := td.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.SetName("foo")
span.SetStartTimestamp(pcommon.Timestamp(10))
span.SetEndTimestamp(pcommon.Timestamp(20))
span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
batches, err := jaeger.ProtoFromTraces(td)
require.NoError(t, err)

protoBytes, err := batches[0].Spans[0].Marshal()
require.NoError(t, err)

jsonMarshaler := &jsonpb.Marshaler{}
jsonBytes := new(bytes.Buffer)
require.NoError(t, jsonMarshaler.Marshal(jsonBytes, batches[0].Spans[0]))

tests := []struct {
unmarshaler ptrace.Unmarshaler
encoding string
bytes []byte
}{
{
unmarshaler: jaegerProtobufTrace{},
encoding: "jaeger_proto",
bytes: protoBytes,
},
{
unmarshaler: jaegerJSONTrace{},
encoding: "jaeger_json",
bytes: jsonBytes.Bytes(),
},
}
for _, test := range tests {
t.Run(test.encoding, func(t *testing.T) {
got, err := test.unmarshaler.UnmarshalTraces(test.bytes)
require.NoError(t, err)
assert.Equal(t, td, got)
})
}
}