Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup flatten operator files (open-telemetry#32063
Browse files Browse the repository at this point in the history
)

Contributes to open-telemetry#32058
  • Loading branch information
djaglowski authored and ycombinator committed Apr 9, 2024
1 parent 2051221 commit 81e4362
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
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
}

0 comments on commit 81e4362

Please sign in to comment.