Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup assignkeys operator files (open-telemetry…
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored and rimitchell committed May 8, 2024
1 parent 054f837 commit 7cd8ea0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package assignkeys // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/assignkeys"

import (
"context"
"fmt"

"go.opentelemetry.io/collector/featuregate"
Expand Down Expand Up @@ -85,49 +84,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {

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

// Transformer transforms a list in the entry field into a map. Each value is assigned a key from configuration keys
type Transformer struct {
helper.TransformerOperator
Field entry.Field
Keys []string
}

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

// Transform will apply AssignKeys to an entry
func (p *Transformer) Transform(entry *entry.Entry) error {
inputListInterface, ok := entry.Get(p.Field)
if !ok {
// The field doesn't exist, so ignore it
return fmt.Errorf("apply assign_keys: field %s does not exist on entry", p.Field)
}

inputList, ok := inputListInterface.([]any)
if !ok {
return fmt.Errorf("apply assign_keys: couldn't convert field %s to []any", p.Field)
}
if len(inputList) != len(p.Keys) {
return fmt.Errorf("apply assign_keys: field %s contains %d values while expected keys are %s contain %d keys", p.Field, len(inputList), p.Keys, len(p.Keys))
}

assignedMap := p.AssignKeys(p.Keys, inputList)

err := entry.Set(p.Field, assignedMap)
if err != nil {
return err
}
return nil
}

func (p *Transformer) AssignKeys(keys []string, values []any) map[string]any {
outputMap := make(map[string]any, len(keys))
for i, key := range keys {
outputMap[key] = values[i]
}

return outputMap
}
57 changes: 57 additions & 0 deletions pkg/stanza/operator/transformer/assignkeys/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 assignkeys // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/assignkeys"

import (
"context"
"fmt"

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

// Transformer transforms a list in the entry field into a map. Each value is assigned a key from configuration keys
type Transformer struct {
helper.TransformerOperator
Field entry.Field
Keys []string
}

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

// Transform will apply AssignKeys to an entry
func (t *Transformer) Transform(entry *entry.Entry) error {
inputListInterface, ok := entry.Get(t.Field)
if !ok {
// The field doesn't exist, so ignore it
return fmt.Errorf("apply assign_keys: field %s does not exist on entry", t.Field)
}

inputList, ok := inputListInterface.([]any)
if !ok {
return fmt.Errorf("apply assign_keys: couldn't convert field %s to []any", t.Field)
}
if len(inputList) != len(t.Keys) {
return fmt.Errorf("apply assign_keys: field %s contains %d values while expected keys are %s contain %d keys", t.Field, len(inputList), t.Keys, len(t.Keys))
}

assignedMap := t.AssignKeys(t.Keys, inputList)

err := entry.Set(t.Field, assignedMap)
if err != nil {
return err
}
return nil
}

func (t *Transformer) AssignKeys(keys []string, values []any) map[string]any {
outputMap := make(map[string]any, len(keys))
for i, key := range keys {
outputMap[key] = values[i]
}

return outputMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestBuildAndProcess(t *testing.T) {
op, err := cfg.Build(testutil.Logger(t))
if tc.expectErr && err != nil {
require.Error(t, err)
t.SkipNow()
return
}
require.NoError(t, err)

Expand Down

0 comments on commit 7cd8ea0

Please sign in to comment.