Skip to content

[chore] receiver/kafkareceiver: tidy up encodings #38984

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

Merged
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
32 changes: 0 additions & 32 deletions receiver/kafkareceiver/azureresourcelogs_unmarshaler.go

This file was deleted.

16 changes: 0 additions & 16 deletions receiver/kafkareceiver/azureresourcelogs_unmarshaler_test.go

This file was deleted.

135 changes: 135 additions & 0 deletions receiver/kafkareceiver/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kafkareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver"

import (
"errors"
"fmt"
"strings"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/receiver"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv1"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv2"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver/internal/unmarshaler"
)

var errUnknownEncodingExtension = errors.New("unknown encoding extension")

func newTracesUnmarshaler(encoding string, _ receiver.Settings, host component.Host) (ptrace.Unmarshaler, error) {
// Extensions take precedence.
if unmarshaler, err := loadEncodingExtension[ptrace.Unmarshaler](host, encoding, "traces"); err != nil {
if !errors.Is(err, errUnknownEncodingExtension) {
return nil, err
}
} else {
return unmarshaler, nil
}
switch encoding {
case "otlp_proto":
return &ptrace.ProtoUnmarshaler{}, nil
case "otlp_json":
return &ptrace.JSONUnmarshaler{}, nil
case "jaeger_proto":
return unmarshaler.JaegerProtoSpanUnmarshaler{}, nil
case "jaeger_json":
return unmarshaler.JaegerJSONSpanUnmarshaler{}, nil
case "zipkin_proto":
return zipkinv2.NewProtobufTracesUnmarshaler(false, false), nil
case "zipkin_json":
return zipkinv2.NewJSONTracesUnmarshaler(false), nil
case "zipkin_thrift":
return zipkinv1.NewThriftTracesUnmarshaler(), nil
}
Comment on lines +34 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if this should be loaded in a map somewhere instead of an exhaustive switch statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem better to me, since that implies constructing unmarshalers ahead of time that may never be used. What benefit do you see of using a map over this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is the same, one is a map unrolled effectively.

return nil, fmt.Errorf("unrecognized traces encoding %q", encoding)
}

func newLogsUnmarshaler(encoding string, set receiver.Settings, host component.Host) (plog.Unmarshaler, error) {
// Extensions take precedence.
if unmarshaler, err := loadEncodingExtension[plog.Unmarshaler](host, encoding, "logs"); err != nil {
if !errors.Is(err, errUnknownEncodingExtension) {
return nil, err
}
} else {
return unmarshaler, nil
}
switch encoding {
case "otlp_proto":
return &plog.ProtoUnmarshaler{}, nil
case "otlp_json":
return &plog.JSONUnmarshaler{}, nil
case "raw":
return unmarshaler.RawLogsUnmarshaler{}, nil
case "json":
return unmarshaler.JSONLogsUnmarshaler{}, nil
case "azure_resource_logs":
return &azure.ResourceLogsUnmarshaler{
Version: set.BuildInfo.Version,
Logger: set.Logger,
}, nil
case "text":
return unmarshaler.NewTextLogsUnmarshaler("utf-8")
}
// There is a special case for text-based encodings, where you can specify
// the text encoding (e.g. utf8, utf16) as a suffix in the encoding name.
if textEncodingName, ok := strings.CutPrefix(encoding, "text_"); ok {
u, err := unmarshaler.NewTextLogsUnmarshaler(textEncodingName)
if err != nil {
return nil, fmt.Errorf("invalid text encoding: %w", err)
}
return u, nil
}
Comment on lines +81 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind explaining what is section used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is handling the logic that's currently here:

func getLogsUnmarshaler(encoding string, unmarshalers map[string]LogsUnmarshaler) (LogsUnmarshaler, error) {
var enc string
unmarshaler, ok := unmarshalers[encoding]
if !ok {
split := strings.SplitN(encoding, "_", 2)
prefix := split[0]
if len(split) > 1 {
enc = split[1]
}
unmarshaler, ok = unmarshalers[prefix].(LogsUnmarshalerWithEnc)
if !ok {
return nil, errUnrecognizedEncoding
}
}
if unmarshalerWithEnc, ok := unmarshaler.(LogsUnmarshalerWithEnc); ok {
// This should be called even when enc is an empty string to initialize the encoding.
unmarshaler, err := unmarshalerWithEnc.WithEnc(enc)
if err != nil {
return nil, err
}
return unmarshaler, nil
}
return unmarshaler, nil
}

I'll add a comment

return nil, fmt.Errorf("unrecognized logs encoding %q", encoding)
}

func newMetricsUnmarshaler(encoding string, _ receiver.Settings, host component.Host) (pmetric.Unmarshaler, error) {
// Extensions take precedence.
if unmarshaler, err := loadEncodingExtension[pmetric.Unmarshaler](host, encoding, "metrics"); err != nil {
if !errors.Is(err, errUnknownEncodingExtension) {
return nil, err
}
} else {
return unmarshaler, nil
}
switch encoding {
case "otlp_proto":
return &pmetric.ProtoUnmarshaler{}, nil
case "otlp_json":
return &pmetric.JSONUnmarshaler{}, nil
}
return nil, fmt.Errorf("unrecognized metrics encoding %q", encoding)
}

// loadEncodingExtension tries to load an available extension for the given encoding.
func loadEncodingExtension[T any](host component.Host, encoding, signalType string) (T, error) {
var zero T
extensionID, err := encodingToComponentID(encoding)
if err != nil {
return zero, err
}
encodingExtension, ok := host.GetExtensions()[*extensionID]
if !ok {
return zero, fmt.Errorf("invalid encoding %q: %w", encoding, errUnknownEncodingExtension)
}
unmarshaler, ok := encodingExtension.(T)
if !ok {
return zero, fmt.Errorf("extension %q is not a %s unmarshaler", encoding, signalType)
}
return unmarshaler, nil
}

// encodingToComponentID converts an encoding string to a component ID using the given encoding as type.
func encodingToComponentID(encoding string) (*component.ID, error) {
componentType, err := component.NewType(encoding)
if err != nil {
return nil, fmt.Errorf("invalid component type: %w", err)
}
id := component.NewID(componentType)
return &id, nil
}
Loading