-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
146 lines (118 loc) · 4.48 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:generate mdatagen metadata.yaml
package azuremonitorexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter"
import (
"context"
"errors"
"time"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/azuremonitorexporter/internal/metadata"
)
const (
defaultEndpoint = "https://dc.services.visualstudio.com/v2/track"
)
var (
errUnexpectedConfigurationType = errors.New("failed to cast configuration to Azure Monitor Config")
)
// NewFactory returns a factory for Azure Monitor exporter.
func NewFactory() exporter.Factory {
f := &factory{}
return exporter.NewFactory(
metadata.Type,
createDefaultConfig,
exporter.WithTraces(f.createTracesExporter, metadata.TracesStability),
exporter.WithLogs(f.createLogsExporter, metadata.LogsStability),
exporter.WithMetrics(f.createMetricsExporter, metadata.MetricsStability))
}
// Implements the interface from go.opentelemetry.io/collector/exporter/factory.go
type factory struct {
tChannel transportChannel
}
func createDefaultConfig() component.Config {
return &Config{
Endpoint: defaultEndpoint,
MaxBatchSize: 1024,
MaxBatchInterval: 10 * time.Second,
SpanEventsEnabled: false,
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
}
}
func (f *factory) createTracesExporter(
_ context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Traces, error) {
exporterConfig, ok := cfg.(*Config)
if !ok {
return nil, errUnexpectedConfigurationType
}
tc, errInstrumentationKeyOrConnectionString := f.getTransportChannel(exporterConfig, set.Logger)
if errInstrumentationKeyOrConnectionString != nil {
return nil, errInstrumentationKeyOrConnectionString
}
return newTracesExporter(exporterConfig, tc, set)
}
func (f *factory) createLogsExporter(
_ context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Logs, error) {
exporterConfig, ok := cfg.(*Config)
if !ok {
return nil, errUnexpectedConfigurationType
}
tc, errInstrumentationKeyOrConnectionString := f.getTransportChannel(exporterConfig, set.Logger)
if errInstrumentationKeyOrConnectionString != nil {
return nil, errInstrumentationKeyOrConnectionString
}
return newLogsExporter(exporterConfig, tc, set)
}
func (f *factory) createMetricsExporter(
_ context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Metrics, error) {
exporterConfig, ok := cfg.(*Config)
if !ok {
return nil, errUnexpectedConfigurationType
}
tc, errInstrumentationKeyOrConnectionString := f.getTransportChannel(exporterConfig, set.Logger)
if errInstrumentationKeyOrConnectionString != nil {
return nil, errInstrumentationKeyOrConnectionString
}
return newMetricsExporter(exporterConfig, tc, set)
}
// Configures the transport channel.
// This method is not thread-safe
func (f *factory) getTransportChannel(exporterConfig *Config, logger *zap.Logger) (transportChannel, error) {
// The default transport channel uses the default send mechanism from the AppInsights telemetry client.
// This default channel handles batching, appropriate retries, and is backed by memory.
if f.tChannel == nil {
connectionVars, err := parseConnectionString(exporterConfig)
if err != nil {
return nil, err
}
exporterConfig.InstrumentationKey = configopaque.String(connectionVars.InstrumentationKey)
exporterConfig.Endpoint = connectionVars.IngestionURL
telemetryConfiguration := appinsights.NewTelemetryConfiguration(string(exporterConfig.InstrumentationKey))
telemetryConfiguration.EndpointUrl = exporterConfig.Endpoint
telemetryConfiguration.MaxBatchSize = exporterConfig.MaxBatchSize
telemetryConfiguration.MaxBatchInterval = exporterConfig.MaxBatchInterval
telemetryClient := appinsights.NewTelemetryClientFromConfig(telemetryConfiguration)
f.tChannel = telemetryClient.Channel()
// Don't even bother enabling the AppInsights diagnostics listener unless debug logging is enabled
if checkedEntry := logger.Check(zap.DebugLevel, ""); checkedEntry != nil {
appinsights.NewDiagnosticsMessageListener(func(msg string) error {
logger.Debug(msg)
return nil
})
}
}
return f.tChannel, nil
}