forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Cleanup assignkeys operator files (open-telemetry…
…#32060) Contributes to open-telemetry#32058
- Loading branch information
1 parent
44769e5
commit 9d6ef77
Showing
3 changed files
with
58 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters