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

Improve OpenTelemetry Collector JSON Marshalling #3286

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cb6600e
Improve OpenTelemetry Collector JSON Marshalling
iblancasa Sep 12, 2024
f242919
Add clarification
iblancasa Sep 19, 2024
daaab63
Merge branch 'main' into bug/3281
iblancasa Sep 19, 2024
4a08965
Merge branch 'main' into bug/3281
iblancasa Sep 23, 2024
3812d6e
Merge branch 'main' into bug/3281
iblancasa Sep 23, 2024
dfc89b2
Merge branch 'main' into bug/3281
iblancasa Sep 24, 2024
b9bce9a
Merge branch 'bug/3281' of github.com:iblancasa/opentelemetry-operato…
iblancasa Sep 26, 2024
bd41d8c
Apply changes requested in code review
iblancasa Sep 26, 2024
32264b6
Improve changelog
iblancasa Sep 26, 2024
9d2ed17
Merge branch 'main' into bug/3281
iblancasa Sep 26, 2024
4c05ee6
Merge branch 'main' into bug/3281
iblancasa Sep 30, 2024
8d63442
Merge branch 'main' into bug/3281
iblancasa Sep 30, 2024
bff339f
Merge branch 'main' into bug/3281
iblancasa Oct 1, 2024
11a2258
Merge branch 'main' into bug/3281
iblancasa Oct 1, 2024
6049ced
Merge branch 'bug/3281' of github.com:iblancasa/opentelemetry-operato…
iblancasa Oct 2, 2024
f3e8de0
Remove not needed copy
iblancasa Oct 2, 2024
5f67714
Merge branch 'main' into bug/3281
iblancasa Oct 2, 2024
a9131d3
Merge branch 'main' into bug/3281
iblancasa Oct 3, 2024
f86f7d5
Merge branch 'main' into bug/3281
iblancasa Oct 7, 2024
2e6916f
Merge branch 'bug/3281' of github.com:iblancasa/opentelemetry-operato…
iblancasa Oct 8, 2024
8820e14
Merge branch 'main' of github.com:open-telemetry/opentelemetry-operat…
iblancasa Oct 8, 2024
905b5d7
Merge branch 'main' into bug/3281
iblancasa Oct 9, 2024
7505d5a
Merge branch 'main' into bug/3281
iblancasa Oct 10, 2024
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
38 changes: 38 additions & 0 deletions .chloggen/3281-marshal-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix Configuration marshalling for v1beta1 OpenTelemetryCollector instances.

# One or more tracking issues related to the change
issues: [3281]

# (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: |
Previously, the v1beta1 OpenTelemetryCollector CR's config field was incorrectly
marshalled, potentially causing issues when storing in etcd or retrieving via kubectl.
This fix ensures that the config is properly marshalled, maintaining the correct
structure and data types. For example, a config with nested maps and arrays will now
be correctly preserved when stored and retrieved, instead of being flattened or
losing structure.

As an example, configuration like this:

receivers:
otlp:
protocols:
grpc:
http:

Would be retrieved from the Kubernetes cluster as:

receivers:
otlp:
protocols:
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved

The specified protocols were not there anymore before this fix.
26 changes: 24 additions & 2 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ func (c *AnyConfig) UnmarshalJSON(b []byte) error {
}

// MarshalJSON specifies how to convert this object into JSON.
func (c *AnyConfig) MarshalJSON() ([]byte, error) {
if c == nil {
func (c AnyConfig) MarshalJSON() ([]byte, error) {
if c.Object == nil {
return []byte("{}"), nil
}

return json.Marshal(c.Object)
}

Expand Down Expand Up @@ -334,6 +335,27 @@ func (c *Config) Yaml() (string, error) {
return buf.String(), nil
}

// MarshalJSON marshalls the Config field.
func (c Config) MarshalJSON() ([]byte, error) {
// When you marshal a struct that embeds itself or a type that directly or indirectly refers back to itself,
// Go's JSON marshaling can lead to infinite recursion. To avoid this, we create an alias of the struct
// (Config), so Go no longer considers it the same type.
// This allows you to embed the struct safely within itself without triggering that infinite loop.
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
type Alias Config
// Ensure we call the custom marshaller for AnyConfig for the Receivers
receiversJSON, err := json.Marshal(c.Receivers)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we only need to do this for receivers?

Copy link
Contributor

Choose a reason for hiding this comment

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

@iblancasa bump on this

if err != nil {
return nil, err
}
return json.Marshal(&struct {
*Alias
Receivers json.RawMessage `json:"receivers,omitempty"`
}{
Alias: (*Alias)(&c),
Receivers: receiversJSON,
})
}

// Returns null objects in the config.
func (c *Config) nullObjects() []string {
var nullKeys []string
Expand Down
287 changes: 287 additions & 0 deletions apis/v1beta1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package v1beta1

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand All @@ -26,11 +30,227 @@ import (
"github.com/stretchr/testify/require"
go_yaml "gopkg.in/yaml.v3"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/yaml"
)

func TestCreateConfigInKubernetesEmptyValues(t *testing.T) {
testScheme := scheme.Scheme
err := AddToScheme(testScheme)
require.NoError(t, err)

testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
}

cfg, err := testEnv.Start()
require.NoError(t, err)
defer func() {
errStop := testEnv.Stop()
require.NoError(t, errStop)
}()

k8sClient, err := client.New(cfg, client.Options{Scheme: testScheme})
if err != nil {
fmt.Printf("failed to setup a Kubernetes client: %v", err)
os.Exit(1)
Comment on lines +44 to +62
Copy link
Contributor

Choose a reason for hiding this comment

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

ive found this fake client package a lot more usable

}

newCollector := &OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-collector",
Namespace: "default",
},
Spec: OpenTelemetryCollectorSpec{
UpgradeStrategy: UpgradeStrategyNone,
Config: Config{
Exporters: AnyConfig{
Object: map[string]interface{}{
"logging": map[string]interface{}{},
},
},
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": map[string]interface{}{
"protocols": map[string]interface{}{
"grpc": map[string]interface{}{},
"http": map[string]interface{}{},
},
},
},
},
Processors: &AnyConfig{
Object: map[string]interface{}{
"resourcedetection": map[string]interface{}{},
},
},
Service: Service{
Pipelines: map[string]*Pipeline{
"traces": {
Receivers: []string{"otlp"},
Exporters: []string{"logging"},
},
},
},
},
},
}

err = k8sClient.Create(context.TODO(), newCollector)
if err != nil {
log.Fatal(err)
}

// Fetch the created OpenTelemetryCollector
otelCollector := &OpenTelemetryCollector{}
err = k8sClient.Get(context.TODO(), types.NamespacedName{
Name: "my-collector",
Namespace: "default",
}, otelCollector)

if err != nil {
log.Fatal(err)
}

jsonData, err := json.Marshal(otelCollector.Spec)
if err != nil {
log.Fatal(err)
}

require.Contains(t, string(jsonData), "{\"grpc\":{},\"http\":{}}")
require.Contains(t, string(jsonData), "\"resourcedetection\":{}")

unmarshalledCollector := &OpenTelemetryCollector{}
err = json.Unmarshal(jsonData, &unmarshalledCollector.Spec)
require.NoError(t, err)

require.NotNil(t, unmarshalledCollector.Spec.Config.Receivers.Object["otlp"])

otlpReceiver, ok := unmarshalledCollector.Spec.Config.Receivers.Object["otlp"].(map[string]interface{})
require.True(t, ok, "otlp receiver should be a map")
protocols, ok := otlpReceiver["protocols"].(map[string]interface{})
require.True(t, ok, "protocols should be a map")

grpc, ok := protocols["grpc"]
require.True(t, ok, "grpc protocol should exist")
require.NotNil(t, grpc, "grpc protocol should be nil")

http, ok := protocols["http"]
require.True(t, ok, "http protocol should exist")
require.NotNil(t, http, "http protocol should be nil")

require.NotNil(t, unmarshalledCollector.Spec.Config.Receivers.Object["otlp"])

_, ok = unmarshalledCollector.Spec.Config.Processors.Object["resourcedetection"].(map[string]interface{})
require.True(t, ok, "resourcedetector processor should be a map")
}

func TestCreateConfigInKubernetesNullValues(t *testing.T) {
testScheme := scheme.Scheme
err := AddToScheme(testScheme)
require.NoError(t, err)

testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
}

cfg, err := testEnv.Start()
require.NoError(t, err)
defer func() {
errStop := testEnv.Stop()
require.NoError(t, errStop)
}()

k8sClient, err := client.New(cfg, client.Options{Scheme: testScheme})
if err != nil {
fmt.Printf("failed to setup a Kubernetes client: %v", err)
os.Exit(1)
}

newCollector := &OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-collector",
Namespace: "default",
},
Spec: OpenTelemetryCollectorSpec{
UpgradeStrategy: UpgradeStrategyNone,
Config: Config{
Exporters: AnyConfig{
Object: map[string]interface{}{
"logging": map[string]interface{}{},
},
},
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": map[string]interface{}{
"protocols": map[string]interface{}{
"grpc": nil,
"http": nil,
},
},
},
},
Service: Service{
Pipelines: map[string]*Pipeline{
"traces": {
Receivers: []string{"otlp"},
Exporters: []string{"logging"},
},
},
},
},
},
}

err = k8sClient.Create(context.TODO(), newCollector)
if err != nil {
log.Fatal(err)
}

// Fetch the created OpenTelemetryCollector
otelCollector := &OpenTelemetryCollector{}
err = k8sClient.Get(context.TODO(), types.NamespacedName{
Name: "my-collector",
Namespace: "default",
}, otelCollector)

if err != nil {
log.Fatal(err)
}

jsonData, err := json.Marshal(otelCollector.Spec)
if err != nil {
log.Fatal(err)
}

require.Contains(t, string(jsonData), "{\"grpc\":null,\"http\":null")

unmarshalledCollector := &OpenTelemetryCollector{}
err = json.Unmarshal(jsonData, &unmarshalledCollector.Spec)
require.NoError(t, err)

require.NotNil(t, unmarshalledCollector.Spec.Config.Receivers.Object["otlp"])

otlpReceiver, ok := unmarshalledCollector.Spec.Config.Receivers.Object["otlp"].(map[string]interface{})
require.True(t, ok, "otlp receiver should be a map")
protocols, ok := otlpReceiver["protocols"].(map[string]interface{})
require.True(t, ok, "protocols should be a map")

grpc, ok := protocols["grpc"]
require.True(t, ok, "grpc protocol should exist")
require.Nil(t, grpc, "grpc protocol should be nil")

http, ok := protocols["http"]
require.True(t, ok, "http protocol should exist")
require.Nil(t, http, "http protocol should be nil")
}

func TestConfigFiles(t *testing.T) {
files, err := os.ReadDir("./testdata")
require.NoError(t, err)
Expand Down Expand Up @@ -558,3 +778,70 @@ func TestConfig_GetExporterPorts(t *testing.T) {
})
}
}

func TestAnyConfig_MarshalJSON(t *testing.T) {
tests := []struct {
name string
config AnyConfig
want string
wantErr bool
}{
{
name: "nil Object",
config: AnyConfig{},
want: "{}",
},
{
name: "empty Object",
config: AnyConfig{
Object: map[string]interface{}{},
},
want: "{}",
},
{
name: "Object with nil value",
config: AnyConfig{
Object: map[string]interface{}{
"key": nil,
},
},
want: `{"key":null}`,
},
{
name: "Object with empty map value",
config: AnyConfig{
Object: map[string]interface{}{
"key": map[string]interface{}{},
},
},
want: `{"key":{}}`,
},
{
name: "Object with non-empty values",
config: AnyConfig{
Object: map[string]interface{}{
"string": "value",
"number": 42,
"bool": true,
"map": map[string]interface{}{
"nested": "data",
},
},
},
want: `{"bool":true,"map":{"nested":"data"},"number":42,"string":"value"}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.config.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("AnyConfig.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != tt.want {
t.Errorf("AnyConfig.MarshalJSON() = %v, want %v", string(got), tt.want)
}
})
}
}
Loading
Loading