Skip to content

Commit

Permalink
[chore] add processorhelperprofiles package
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu committed Oct 29, 2024
1 parent 90243f3 commit bf063fc
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 0 deletions.
1 change: 1 addition & 0 deletions processor/processorhelper/processorhelperprofiles/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../Makefile.Common
70 changes: 70 additions & 0 deletions processor/processorhelper/processorhelperprofiles/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module go.opentelemetry.io/collector/processor/processorhelper/processorhelperprofiles

go 1.22.0

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.112.0
go.opentelemetry.io/collector/consumer v0.112.0
go.opentelemetry.io/collector/consumer/consumerprofiles v0.112.0
go.opentelemetry.io/collector/consumer/consumertest v0.112.0
go.opentelemetry.io/collector/pdata/pprofile v0.112.0
go.opentelemetry.io/collector/processor v0.112.0
go.opentelemetry.io/collector/processor/processorprofiles v0.112.0
go.opentelemetry.io/collector/processor/processortest v0.112.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/component/componentstatus v0.112.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.112.0 // indirect
go.opentelemetry.io/collector/pdata v1.18.0 // indirect
go.opentelemetry.io/collector/pdata/testdata v0.112.0 // indirect
go.opentelemetry.io/collector/pipeline v0.112.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/collector/consumer/consumertest => ../../../consumer/consumertest

replace go.opentelemetry.io/collector/pdata/pprofile => ../../../pdata/pprofile

replace go.opentelemetry.io/collector/pdata/testdata => ../../../pdata/testdata

replace go.opentelemetry.io/collector/processor => ../../../processor

replace go.opentelemetry.io/collector/processor/processorhelper => ../../processorhelper

replace go.opentelemetry.io/collector/consumer => ../../../consumer

replace go.opentelemetry.io/collector/consumer/consumerprofiles => ../../../consumer/consumerprofiles

replace go.opentelemetry.io/collector/component => ../../../component

replace go.opentelemetry.io/collector/extension => ../../../extension

replace go.opentelemetry.io/collector/pdata => ../../../pdata

replace go.opentelemetry.io/collector/config/configtelemetry => ../../../config/configtelemetry

replace go.opentelemetry.io/collector/pipeline => ../../../pipeline
102 changes: 102 additions & 0 deletions processor/processorhelper/processorhelperprofiles/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions processor/processorhelper/processorhelperprofiles/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package processorhelperprofiles // import "go.opentelemetry.io/collector/processor/processorhelper/processorhelperprofiles"

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
)

// Option apply changes to internalOptions.
type Option interface {
apply(*baseSettings)
}

type optionFunc func(*baseSettings)

func (of optionFunc) apply(e *baseSettings) {
of(e)
}

// WithStart overrides the default Start function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithStart(start component.StartFunc) Option {
return optionFunc(func(o *baseSettings) {
o.StartFunc = start
})
}

// WithShutdown overrides the default Shutdown function for an processor.
// The default shutdown function does nothing and always returns nil.
func WithShutdown(shutdown component.ShutdownFunc) Option {
return optionFunc(func(o *baseSettings) {
o.ShutdownFunc = shutdown
})
}

// WithCapabilities overrides the default GetCapabilities function for an processor.
// The default GetCapabilities function returns mutable capabilities.
func WithCapabilities(capabilities consumer.Capabilities) Option {
return optionFunc(func(o *baseSettings) {
o.consumerOptions = append(o.consumerOptions, consumer.WithCapabilities(capabilities))
})
}

type baseSettings struct {
component.StartFunc
component.ShutdownFunc
consumerOptions []consumer.Option
}

// fromOptions returns the internal settings starting from the default and applying all options.
func fromOptions(options []Option) *baseSettings {
// Start from the default options:
opts := &baseSettings{
consumerOptions: []consumer.Option{consumer.WithCapabilities(consumer.Capabilities{MutatesData: true})},
}

for _, op := range options {
op.apply(opts)
}

return opts
}
61 changes: 61 additions & 0 deletions processor/processorhelper/processorhelperprofiles/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package processorhelperprofiles // import "go.opentelemetry.io/collector/processor/processorhelper/processorhelperprofiles"

import (
"context"
"errors"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumerprofiles"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"
"go.opentelemetry.io/collector/processor/processorprofiles"
)

// ProcessProfilesFunc is a helper function that processes the incoming data and returns the data to be sent to the next component.
// If error is returned then returned data are ignored. It MUST not call the next component.
type ProcessProfilesFunc func(context.Context, pprofile.Profiles) (pprofile.Profiles, error)

type profiles struct {
component.StartFunc
component.ShutdownFunc
consumerprofiles.Profiles
}

// NewProfiles creates a processorprofiles.Profiles that ensure context propagation and the right tags are set.
func NewProfiles(
_ context.Context,
set processor.Settings,
_ component.Config,
nextConsumer consumerprofiles.Profiles,
profilesFunc ProcessProfilesFunc,
options ...Option,
) (processorprofiles.Profiles, error) {
if profilesFunc == nil {
return nil, errors.New("nil profilesFunc")
}

bs := fromOptions(options)
profilessConsumer, err := consumerprofiles.NewProfiles(func(ctx context.Context, pd pprofile.Profiles) (err error) {
pd, err = profilesFunc(ctx, pd)
if err != nil {
if errors.Is(err, processorhelper.ErrSkipProcessingData) {
return nil
}
return err
}
return nextConsumer.ConsumeProfiles(ctx, pd)
}, bs.consumerOptions...)
if err != nil {
return nil, err
}

Check warning on line 54 in processor/processorhelper/processorhelperprofiles/profiles.go

View check run for this annotation

Codecov / codecov/patch

processor/processorhelper/processorhelperprofiles/profiles.go#L53-L54

Added lines #L53 - L54 were not covered by tests

return &profiles{
StartFunc: bs.StartFunc,
ShutdownFunc: bs.ShutdownFunc,
Profiles: profilessConsumer,
}, nil
}
Loading

0 comments on commit bf063fc

Please sign in to comment.