Skip to content

Commit ee2c784

Browse files
Add documentation example for exporter package (open-telemetry#5675) (open-telemetry#12958)
#### Description This PR introduces a testable example to the package [exporter](/receiver). it's a similar example to the documentation one on https://opentelemetry.io/docs/collector/building/receiver/#designing-and-validating-receiver-settings #### Issue: open-telemetry#5675 (comment) #### Testing the testable example was executed successfully ![image](https://github.com/user-attachments/assets/0843a710-767b-4746-81d3-81fe05dec472) #### Documentation this testable example follows https://go.dev/blog/examples#larger-examples format and should be automatically added to `exporter` package documentation Co-authored-by: Damien Mathieu <[email protected]>
1 parent 094e1fa commit ee2c784

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

exporter/example_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package exporter_test
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"go.uber.org/zap"
11+
12+
"go.opentelemetry.io/collector/component"
13+
"go.opentelemetry.io/collector/config/configretry"
14+
"go.opentelemetry.io/collector/consumer"
15+
"go.opentelemetry.io/collector/exporter"
16+
"go.opentelemetry.io/collector/exporter/exporterhelper"
17+
"go.opentelemetry.io/collector/pdata/pmetric"
18+
)
19+
20+
// typeStr defines the unique type identifier for the exporter.
21+
var typeStr = component.MustNewType("example")
22+
23+
// exampleConfig holds configuration settings for the exporter.
24+
type exampleConfig struct {
25+
QueueSettings exporterhelper.QueueBatchConfig
26+
BackOffConfig configretry.BackOffConfig
27+
}
28+
29+
// exampleExporter implements the OpenTelemetry exporter interface.
30+
type exampleExporter struct {
31+
cancel context.CancelFunc
32+
config exampleConfig
33+
client *loggerClient
34+
}
35+
36+
// loggerClient wraps a Zap logger to provide logging functionality.
37+
type loggerClient struct {
38+
logger *zap.Logger
39+
}
40+
41+
// Example demonstrates the usage of the exporter factory.
42+
func Example() {
43+
// Instantiate the exporter factory and print its type.
44+
exampleExporter := NewFactory()
45+
fmt.Println(exampleExporter.Type())
46+
47+
// Output:
48+
// example
49+
}
50+
51+
// NewFactory creates a new exporter factory.
52+
func NewFactory() exporter.Factory {
53+
return exporter.NewFactory(
54+
typeStr,
55+
createDefaultConfig,
56+
exporter.WithMetrics(createExampleExporter, component.StabilityLevelAlpha),
57+
)
58+
}
59+
60+
// createDefaultConfig returns the default configuration for the exporter.
61+
func createDefaultConfig() component.Config {
62+
return &exampleConfig{}
63+
}
64+
65+
// createExampleExporter initializes an instance of the example exporter.
66+
func createExampleExporter(ctx context.Context, params exporter.Settings, baseCfg component.Config) (exporter.Metrics, error) {
67+
// Convert baseCfg to the correct type.
68+
cfg := baseCfg.(*exampleConfig)
69+
70+
// Create a new exporter instance.
71+
xptr := newExampleExporter(ctx, cfg, params)
72+
73+
// Wrap the exporter with the helper utilities.
74+
return exporterhelper.NewMetrics(
75+
ctx,
76+
params,
77+
cfg,
78+
xptr.consumeMetrics,
79+
exporterhelper.WithQueue(cfg.QueueSettings),
80+
exporterhelper.WithRetry(cfg.BackOffConfig),
81+
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
82+
exporterhelper.WithShutdown(xptr.shutdown),
83+
)
84+
}
85+
86+
// newExampleExporter constructs a new instance of the example exporter.
87+
func newExampleExporter(ctx context.Context, cfg *exampleConfig, params exporter.Settings) *exampleExporter {
88+
xptr := &exampleExporter{
89+
config: *cfg,
90+
client: &loggerClient{logger: params.Logger},
91+
}
92+
93+
// Create a cancelable context.
94+
_, xptr.cancel = context.WithCancel(ctx)
95+
96+
return xptr
97+
}
98+
99+
// consumeMetrics processes incoming metric data and logs it.
100+
func (xptr *exampleExporter) consumeMetrics(_ context.Context, md pmetric.Metrics) error {
101+
xptr.client.Push(md)
102+
return nil
103+
}
104+
105+
// Shutdown properly stops the exporter and releases resources.
106+
func (xptr *exampleExporter) shutdown(_ context.Context) error {
107+
xptr.cancel()
108+
return nil
109+
}
110+
111+
// Push logs the received metric data.
112+
func (client *loggerClient) Push(md pmetric.Metrics) {
113+
client.logger.Info("Exporting metrics", zap.Any("metrics", md))
114+
}

exporter/xexporter/go.sum

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)