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 retain operator files (open-telemetry#32068)
Contributes to open-telemetry#32058
- Loading branch information
1 parent
c5316c6
commit 92d3e8e
Showing
3 changed files
with
56 additions
and
46 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,56 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package retain // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/retain" | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" | ||
) | ||
|
||
// Transformer keeps the given fields and deletes the rest. | ||
type Transformer struct { | ||
helper.TransformerOperator | ||
Fields []entry.Field | ||
AllBodyFields bool | ||
AllAttributeFields bool | ||
AllResourceFields bool | ||
} | ||
|
||
// Process will process an entry with a retain transformation. | ||
func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error { | ||
return t.ProcessWith(ctx, entry, t.Transform) | ||
} | ||
|
||
// Transform will apply the retain operation to an entry | ||
func (t *Transformer) Transform(e *entry.Entry) error { | ||
newEntry := entry.New() | ||
newEntry.ObservedTimestamp = e.ObservedTimestamp | ||
newEntry.Timestamp = e.Timestamp | ||
|
||
if !t.AllResourceFields { | ||
newEntry.Resource = e.Resource | ||
} | ||
if !t.AllAttributeFields { | ||
newEntry.Attributes = e.Attributes | ||
} | ||
if !t.AllBodyFields { | ||
newEntry.Body = e.Body | ||
} | ||
|
||
for _, field := range t.Fields { | ||
val, ok := e.Get(field) | ||
if !ok { | ||
continue | ||
} | ||
err := newEntry.Set(field, val) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
*e = *newEntry | ||
return nil | ||
} |
File renamed without changes.