Skip to content

Commit 01c6651

Browse files
joeyyy09fatsheep9146yurishkuro
authored
[receiver/kafkareceiver] Add otlp_json support in kafka receiver (#34840)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.-->The current features dont support otlp_json in receivers/kafkareceivers. Add support for otlp_json which accepts json formated for Otel Collector kafka receiver **Link to tracking Issue:** <Issue number if applicable> #33627 **Testing:** <Describe what testing was performed and which tests were added.> Added test files for the same. **Documentation:** <Describe the documentation added.> --------- Signed-off-by: joeyyy09 <[email protected]> Co-authored-by: Ziqi Zhao <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
1 parent c52a9a7 commit 01c6651

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

.chloggen/otlp_logs.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: kafkareceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add support for `otlp_json` encoding to Kafka receiver. The payload is deserialized into OpenTelemetry traces using JSON format."
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33627]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
This encoding allows the Kafka receiver to handle trace data in JSON format,
20+
enabling integration with systems that export traces as JSON-encoded data.
21+
22+
# If your change doesn't affect end users or the exported elements of any package,
23+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
24+
# Optional: The change log or logs in which this entry should be included.
25+
# e.g. '[user]' or '[user, api]'
26+
# Include 'user' if the change is relevant to end users.
27+
# Include 'api' if there is a change to a library API.
28+
# Default: '[user]'
29+
change_logs: [user]

receiver/kafkareceiver/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following settings can be optionally configured:
3131
Only one telemetry type may be used for a given topic.
3232
- `encoding` (default = otlp_proto): The encoding of the payload received from kafka. Supports encoding extensions. Tries to load an encoding extension and falls back to internal encodings if no extension was loaded. Available internal encodings:
3333
- `otlp_proto`: the payload is deserialized to `ExportTraceServiceRequest`, `ExportLogsServiceRequest` or `ExportMetricsServiceRequest` respectively.
34+
- `otlp_json`: the payload is deserialized to `ExportTraceServiceRequest` `ExportLogsServiceRequest` or `ExportMetricsServiceRequest` respectively using JSON encoding.
3435
- `jaeger_proto`: the payload is deserialized to a single Jaeger proto `Span`.
3536
- `jaeger_json`: the payload is deserialized to a single Jaeger JSON Span using `jsonpb`.
3637
- `zipkin_proto`: the payload is deserialized into a list of Zipkin proto spans.

receiver/kafkareceiver/unmarshaler.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
type TracesUnmarshaler interface {
1818
// Unmarshal deserializes the message body into traces.
1919
Unmarshal([]byte) (ptrace.Traces, error)
20-
2120
// Encoding of the serialized messages.
2221
Encoding() string
2322
}
@@ -26,7 +25,6 @@ type TracesUnmarshaler interface {
2625
type MetricsUnmarshaler interface {
2726
// Unmarshal deserializes the message body into traces
2827
Unmarshal([]byte) (pmetric.Metrics, error)
29-
3028
// Encoding of the serialized messages
3129
Encoding() string
3230
}
@@ -35,28 +33,28 @@ type MetricsUnmarshaler interface {
3533
type LogsUnmarshaler interface {
3634
// Unmarshal deserializes the message body into traces.
3735
Unmarshal([]byte) (plog.Logs, error)
38-
3936
// Encoding of the serialized messages.
4037
Encoding() string
4138
}
4239

4340
type LogsUnmarshalerWithEnc interface {
4441
LogsUnmarshaler
45-
4642
// WithEnc sets the character encoding (UTF-8, GBK, etc.) of the unmarshaler.
4743
WithEnc(string) (LogsUnmarshalerWithEnc, error)
4844
}
4945

5046
// defaultTracesUnmarshalers returns map of supported encodings with TracesUnmarshaler.
5147
func defaultTracesUnmarshalers() map[string]TracesUnmarshaler {
5248
otlpPb := newPdataTracesUnmarshaler(&ptrace.ProtoUnmarshaler{}, defaultEncoding)
49+
otlpJSON := newPdataTracesUnmarshaler(&ptrace.JSONUnmarshaler{}, "otlp_json")
5350
jaegerProto := jaegerProtoSpanUnmarshaler{}
5451
jaegerJSON := jaegerJSONSpanUnmarshaler{}
5552
zipkinProto := newPdataTracesUnmarshaler(zipkinv2.NewProtobufTracesUnmarshaler(false, false), "zipkin_proto")
5653
zipkinJSON := newPdataTracesUnmarshaler(zipkinv2.NewJSONTracesUnmarshaler(false), "zipkin_json")
5754
zipkinThrift := newPdataTracesUnmarshaler(zipkinv1.NewThriftTracesUnmarshaler(), "zipkin_thrift")
5855
return map[string]TracesUnmarshaler{
5956
otlpPb.Encoding(): otlpPb,
57+
otlpJSON.Encoding(): otlpJSON,
6058
jaegerProto.Encoding(): jaegerProto,
6159
jaegerJSON.Encoding(): jaegerJSON,
6260
zipkinProto.Encoding(): zipkinProto,
@@ -67,20 +65,24 @@ func defaultTracesUnmarshalers() map[string]TracesUnmarshaler {
6765

6866
func defaultMetricsUnmarshalers() map[string]MetricsUnmarshaler {
6967
otlpPb := newPdataMetricsUnmarshaler(&pmetric.ProtoUnmarshaler{}, defaultEncoding)
68+
otlpJSON := newPdataMetricsUnmarshaler(&pmetric.JSONUnmarshaler{}, "otlp_json")
7069
return map[string]MetricsUnmarshaler{
71-
otlpPb.Encoding(): otlpPb,
70+
otlpPb.Encoding(): otlpPb,
71+
otlpJSON.Encoding(): otlpJSON,
7272
}
7373
}
7474

7575
func defaultLogsUnmarshalers(version string, logger *zap.Logger) map[string]LogsUnmarshaler {
7676
azureResourceLogs := newAzureResourceLogsUnmarshaler(version, logger)
7777
otlpPb := newPdataLogsUnmarshaler(&plog.ProtoUnmarshaler{}, defaultEncoding)
78+
otlpJSON := newPdataLogsUnmarshaler(&plog.JSONUnmarshaler{}, "otlp_json")
7879
raw := newRawLogsUnmarshaler()
7980
text := newTextLogsUnmarshaler()
8081
json := newJSONLogsUnmarshaler()
8182
return map[string]LogsUnmarshaler{
8283
azureResourceLogs.Encoding(): azureResourceLogs,
8384
otlpPb.Encoding(): otlpPb,
85+
otlpJSON.Encoding(): otlpJSON,
8486
raw.Encoding(): raw,
8587
text.Encoding(): text,
8688
json.Encoding(): json,

receiver/kafkareceiver/unmarshaler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
func TestDefaultTracesUnMarshaler(t *testing.T) {
1515
expectedEncodings := []string{
1616
"otlp_proto",
17+
"otlp_json",
1718
"jaeger_proto",
1819
"jaeger_json",
1920
"zipkin_proto",
@@ -34,6 +35,7 @@ func TestDefaultTracesUnMarshaler(t *testing.T) {
3435
func TestDefaultMetricsUnMarshaler(t *testing.T) {
3536
expectedEncodings := []string{
3637
"otlp_proto",
38+
"otlp_json",
3739
}
3840
marshalers := defaultMetricsUnmarshalers()
3941
assert.Equal(t, len(expectedEncodings), len(marshalers))
@@ -49,6 +51,7 @@ func TestDefaultMetricsUnMarshaler(t *testing.T) {
4951
func TestDefaultLogsUnMarshaler(t *testing.T) {
5052
expectedEncodings := []string{
5153
"otlp_proto",
54+
"otlp_json",
5255
"raw",
5356
"text",
5457
"json",

0 commit comments

Comments
 (0)