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][pkg/stanza] Cleanup flatten operator files #32063

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package flatten // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/flatten"

import (
"context"
"fmt"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
Expand Down Expand Up @@ -69,47 +67,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {

return nil, fmt.Errorf("invalid field type: %T", c.Field.FieldInterface)
}

// Transformer flattens an object in the entry field
type Transformer[T interface {
entry.BodyField | entry.ResourceField | entry.AttributeField
entry.FieldInterface
Parent() T
Child(string) T
}] struct {
helper.TransformerOperator
Field T
}

// Process will process an entry with a flatten transformation.
func (p *Transformer[T]) Process(ctx context.Context, entry *entry.Entry) error {
return p.ProcessWith(ctx, entry, p.Transform)
}

// Transform will apply the flatten operation to an entry
func (p *Transformer[T]) Transform(entry *entry.Entry) error {
parent := p.Field.Parent()
val, ok := entry.Delete(p.Field)
if !ok {
// The field doesn't exist, so ignore it
return fmt.Errorf("apply flatten: field %s does not exist on entry", p.Field)
}

valMap, ok := val.(map[string]any)
if !ok {
// The field we were asked to flatten was not a map, so put it back
err := entry.Set(p.Field, val)
if err != nil {
return errors.Wrap(err, "reset non-map field")
}
return fmt.Errorf("apply flatten: field %s is not a map", p.Field)
}

for k, v := range valMap {
err := entry.Set(parent.Child(k), v)
if err != nil {
return err
}
}
return nil
}
57 changes: 57 additions & 0 deletions pkg/stanza/operator/transformer/flatten/transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package flatten // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/flatten"

import (
"context"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

// Transformer flattens an object in the entry field
type Transformer[T interface {
entry.BodyField | entry.ResourceField | entry.AttributeField
entry.FieldInterface
Parent() T
Child(string) T
}] struct {
helper.TransformerOperator
Field T
}

// Process will process an entry with a flatten transformation.
func (t *Transformer[T]) Process(ctx context.Context, entry *entry.Entry) error {
return t.ProcessWith(ctx, entry, t.Transform)
}

// Transform will apply the flatten operation to an entry
func (t *Transformer[T]) Transform(entry *entry.Entry) error {
parent := t.Field.Parent()
val, ok := entry.Delete(t.Field)
if !ok {
// The field doesn't exist, so ignore it
return fmt.Errorf("apply flatten: field %s does not exist on entry", t.Field)
}

valMap, ok := val.(map[string]any)
if !ok {
// The field we were asked to flatten was not a map, so put it back
err := entry.Set(t.Field, val)
if err != nil {
return errors.Wrap(err, "reset non-map field")
}
return fmt.Errorf("apply flatten: field %s is not a map", t.Field)
}

for k, v := range valMap {
err := entry.Set(parent.Child(k), v)
if err != nil {
return err
}
}
return nil
}