| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xprocessorhelper // import "go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/component" |
| 11 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 12 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 13 |
"go.opentelemetry.io/collector/processor" |
| 14 |
"go.opentelemetry.io/collector/processor/processorhelper" |
| 15 |
"go.opentelemetry.io/collector/processor/xprocessor" |
| 16 |
) |
| 17 |
|
| 18 |
// ProcessProfilesFunc is a helper function that processes the incoming data and returns the data to be sent to the next component. |
| 19 |
// If error is returned then returned data are ignored. It MUST not call the next component. |
| 20 |
type ProcessProfilesFunc func(context.Context, pprofile.Profiles) (pprofile.Profiles, error) |
| 21 |
|
| 22 |
type profiles struct { |
| 23 |
component.StartFunc |
| 24 |
component.ShutdownFunc |
| 25 |
xconsumer.Profiles |
| 26 |
} |
| 27 |
|
| 28 |
// NewProfiles creates a xprocessor.Profiles that ensure context propagation. |
| 29 |
func NewProfiles( |
| 30 |
_ context.Context, |
| 31 |
_ processor.Settings, |
| 32 |
_ component.Config, |
| 33 |
nextConsumer xconsumer.Profiles, |
| 34 |
profilesFunc ProcessProfilesFunc, |
| 35 |
options ...Option, |
| 36 |
) (xprocessor.Profiles, error) { |
| 37 |
if profilesFunc == nil { |
| 38 |
return nil, errors.New("nil profilesFunc") |
| 39 |
} |
| 40 |
|
| 41 |
bs := fromOptions(options) |
| 42 |
profilesConsumer, err := xconsumer.NewProfiles(func(ctx context.Context, pd pprofile.Profiles) (err error) { |
| 43 |
pd, err = profilesFunc(ctx, pd) |
| 44 |
if err != nil { |
| 45 |
if errors.Is(err, processorhelper.ErrSkipProcessingData) { |
| 46 |
return nil |
| 47 |
} |
| 48 |
return err |
| 49 |
} |
| 50 |
return nextConsumer.ConsumeProfiles(ctx, pd) |
| 51 |
}, bs.consumerOptions...) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
return &profiles{ |
| 57 |
StartFunc: bs.StartFunc, |
| 58 |
ShutdownFunc: bs.ShutdownFunc, |
| 59 |
Profiles: profilesConsumer, |
| 60 |
}, nil |
| 61 |
} |