Debian Codegraph @

processor/processorhelper/example_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package processorhelper_test
5
6 import (
7 "context"
8 "fmt"
9
10 "go.opentelemetry.io/collector/component"
11 "go.opentelemetry.io/collector/consumer"
12 "go.opentelemetry.io/collector/pdata/pmetric"
13 "go.opentelemetry.io/collector/processor"
14 "go.opentelemetry.io/collector/processor/processorhelper"
15 )
16
17 // typeStr defines the unique type identifier for the processor.
18 var typeStr = component.MustNewType("example")
19
20 // exampleConfig holds configuration settings for the processor.
21 type exampleConfig struct{}
22
23 // exampleProcessor implements the OpenTelemetry processor interface.
24 type exampleProcessor struct {
25 cancel context.CancelFunc
26 config exampleConfig
27 }
28
29 // Example demonstrates the usage of the processor factory.
30 func Example() {
31 // Instantiate the processor factory and print its type.
32 exampleProcessor := NewFactory()
33 fmt.Println(exampleProcessor.Type())
34
35 // Output:
36 // example
37 }
38
39 // NewFactory creates a new processor factory.
40 func NewFactory() processor.Factory {
41 return processor.NewFactory(
42 typeStr,
43 createDefaultConfig,
44 processor.WithMetrics(createExampleProcessor, component.StabilityLevelAlpha),
45 )
46 }
47
48 // createDefaultConfig returns the default configuration for the processor.
49 func createDefaultConfig() component.Config {
50 return &exampleConfig{}
51 }
52
53 // createExampleProcessor initializes an instance of the example processor.
54 func createExampleProcessor(ctx context.Context, params processor.Settings, baseCfg component.Config, next consumer.Metrics) (processor.Metrics, error) {
55 // Convert baseCfg to the correct type.
56 cfg := baseCfg.(*exampleConfig)
57
58 // Create a new processor instance.
59 pcsr := newExampleProcessor(ctx, cfg)
60
61 // Wrap the processor with the helper utilities.
62 return processorhelper.NewMetrics(
63 ctx,
64 params,
65 cfg,
66 next,
67 pcsr.consumeMetrics,
68 processorhelper.WithCapabilities(consumer.Capabilities{MutatesData: true}),
69 processorhelper.WithShutdown(pcsr.shutdown),
70 )
71 }
72
73 // newExampleProcessor constructs a new instance of the example processor.
74 func newExampleProcessor(ctx context.Context, cfg *exampleConfig) *exampleProcessor {
75 pcsr := &exampleProcessor{
76 config: *cfg,
77 }
78
79 // Create a cancelable context.
80 _, pcsr.cancel = context.WithCancel(ctx)
81
82 return pcsr
83 }
84
85 // ConsumeMetrics modify metrics adding one attribute to resource.
86 func (pcsr *exampleProcessor) consumeMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
87 rm := md.ResourceMetrics()
88 for i := 0; i < rm.Len(); i++ {
89 rm.At(i).Resource().Attributes().PutStr("processed_by", "exampleProcessor")
90 }
91
92 return md, nil
93 }
94
95 // Shutdown properly stops the processor and releases resources.
96 func (pcsr *exampleProcessor) shutdown(_ context.Context) error {
97 pcsr.cancel()
98 return nil
99 }