Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup retain operator files (#32068)
Browse files Browse the repository at this point in the history
Contributes to #32058
  • Loading branch information
djaglowski committed Apr 1, 2024
1 parent c5316c6 commit 92d3e8e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package retain // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/retain"

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -68,48 +67,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
}
return retainOp, nil
}

// 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 (p *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
return p.ProcessWith(ctx, entry, p.Transform)
}

// Transform will apply the retain operation to an entry
func (p *Transformer) Transform(e *entry.Entry) error {
newEntry := entry.New()
newEntry.ObservedTimestamp = e.ObservedTimestamp
newEntry.Timestamp = e.Timestamp

if !p.AllResourceFields {
newEntry.Resource = e.Resource
}
if !p.AllAttributeFields {
newEntry.Attributes = e.Attributes
}
if !p.AllBodyFields {
newEntry.Body = e.Body
}

for _, field := range p.Fields {
val, ok := e.Get(field)
if !ok {
continue
}
err := newEntry.Set(field, val)
if err != nil {
return err
}
}

*e = *newEntry
return nil
}
56 changes: 56 additions & 0 deletions pkg/stanza/operator/transformer/retain/transformer.go
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
}

0 comments on commit 92d3e8e

Please sign in to comment.