Skip to content

Commit

Permalink
chore: add support for json codec in jaeger (open-telemetry#28625)
Browse files Browse the repository at this point in the history
**Description:** Add support for JSON protocol for Jaeger codec. 

**Link to tracking Issue:**
[open-telemetry#6272](open-telemetry#28686)
  • Loading branch information
VihasMakwana authored and cparkins committed Jan 10, 2024
1 parent 4c9cbcb commit 2f01ea5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
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)
})
}
}

0 comments on commit 2f01ea5

Please sign in to comment.