| 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 |
} |