-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathconfig.go
More file actions
238 lines (213 loc) · 8.06 KB
/
Copy pathconfig.go
File metadata and controls
238 lines (213 loc) · 8.06 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package beholder
import (
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap/zapcore"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
)
type Config struct {
InsecureConnection bool
CACertFile string
OtelExporterGRPCEndpoint string
OtelExporterHTTPEndpoint string
// OTel Resource
ResourceAttributes []attribute.KeyValue
// Message Emitter
EmitterExportTimeout time.Duration
EmitterExportInterval time.Duration
EmitterExportMaxBatchSize int
EmitterMaxQueueSize int
// EmitterBatchProcessor controls custom-message export mode:
// true = batched async export; false = immediate per-record export.
EmitterBatchProcessor bool
// OTel Trace
TraceSampleRatio float64
TraceBatchTimeout time.Duration
TraceSpanExporter trace.SpanExporter // optional additional exporter
TraceRetryConfig *RetryConfig
// TraceCompressor sets the gRPC compressor for traces. Valid values: "gzip" (default), "none".
TraceCompressor string
// OTel Metric
MetricReaderInterval time.Duration
MetricRetryConfig *RetryConfig
MetricViews []metric.View
// MetricCompressor sets the gRPC compressor for metrics. Valid values: "gzip" (default), "none".
MetricCompressor string
MetricProducers []metric.Producer // For example, a prometheus bridge
// Custom Events via Chip Ingress Emitter
ChipIngressEmitterEnabled bool
ChipIngressEmitterGRPCEndpoint string
ChipIngressInsecureConnection bool // Disables TLS for Chip Ingress Emitter
// Chip Ingress Batch Emitter
ChipIngressBatchEmitterEnabled bool // When true, use batch emitter; when false (default), use legacy per-event emitter
ChipIngressBufferSize uint // Message buffer size (default 1000)
ChipIngressMaxBatchSize uint // Max events per PublishBatch call (default 500)
ChipIngressSendInterval time.Duration // Flush interval (default 100ms)
ChipIngressSendTimeout time.Duration // Timeout per PublishBatch call (default 3s)
ChipIngressDrainTimeout time.Duration // Max time to flush remaining events on shutdown (default 10s)
ChipIngressMaxConcurrentSends int // Max concurrent PublishBatch calls (default 10)
ChipIngressMaxGRPCRequestSize uint // Max serialized PublishBatch request size in bytes (default 10 MiB)
ChipIngressLogger logger.Logger // Required when ChipIngressBatchEmitterEnabled is true
// OTel Log
LogExportTimeout time.Duration
LogExportInterval time.Duration
LogExportMaxBatchSize int
LogMaxQueueSize int
LogBatchProcessor bool // Enabled by default. Disable only for testing.
// Retry config for shared log exporter, used by Emitter and Logger
LogRetryConfig *RetryConfig
LogStreamingEnabled bool // Enable logs streaming to the OTel log exporter
LogLevel zapcore.Level // Log level for telemetry streaming
// LogCompressor sets the gRPC compressor for logs. Valid values: "gzip" (default), "none".
LogCompressor string
// Auth
// AuthHeaders serves two purposes:
// 1. Static mode: When AuthKeySigner is nil, these headers are used as-is and never change
// 2. Rotating mode: When AuthKeySigner is set, these headers are used as initial headers
// until TTL expires, then the lazy signer generates new ones
AuthHeaders map[string]string
AuthHeadersTTL time.Duration
AuthKeySigner Signer
AuthPublicKeyHex string
}
type RetryConfig struct {
// InitialInterval the time to wait after the first failure before
// retrying.
InitialInterval time.Duration
// MaxInterval is the upper bound on backoff interval. Once this value is
// reached the delay between consecutive retries will always be
// `MaxInterval`.
MaxInterval time.Duration
// MaxElapsedTime is the maximum amount of time (including retries) spent
// trying to send a request/batch. Once this value is reached, the data
// is discarded.
// Set to zero to disable retry
MaxElapsedTime time.Duration
}
// Same defaults as used by the OTel SDK
var defaultRetryConfig = RetryConfig{
InitialInterval: 5 * time.Second,
MaxInterval: 30 * time.Second,
MaxElapsedTime: 1 * time.Minute, // Retry is enabled
}
const (
defaultPackageName = "beholder"
defaultMaxConcurrentSends = 10
)
var defaultOtelAttributes = []attribute.KeyValue{
attribute.String("package_name", "beholder"),
}
func DefaultConfig() Config {
return Config{
InsecureConnection: true,
CACertFile: "",
OtelExporterGRPCEndpoint: "localhost:4317",
// Resource
ResourceAttributes: defaultOtelAttributes,
// Message Emitter
EmitterExportTimeout: 30 * time.Second,
EmitterExportMaxBatchSize: 512,
EmitterExportInterval: 1 * time.Second,
EmitterMaxQueueSize: 2048,
// Keep batched export enabled by default for throughput.
EmitterBatchProcessor: true,
// OTel message log exporter retry config
LogRetryConfig: defaultRetryConfig.Copy(),
// Trace
TraceSampleRatio: 1,
TraceBatchTimeout: 1 * time.Second,
TraceCompressor: "gzip",
// OTel trace exporter retry config
TraceRetryConfig: defaultRetryConfig.Copy(),
// Metric
MetricReaderInterval: 1 * time.Second,
MetricCompressor: "gzip",
// OTel metric exporter retry config
MetricRetryConfig: defaultRetryConfig.Copy(),
// Log
LogExportTimeout: 30 * time.Second,
LogExportMaxBatchSize: 512,
LogExportInterval: 1 * time.Second,
LogMaxQueueSize: 2048,
LogBatchProcessor: true,
LogStreamingEnabled: true, // Enable logs streaming by default
LogLevel: zapcore.InfoLevel,
LogCompressor: "gzip",
// Chip Ingress Batch Emitter
ChipIngressBatchEmitterEnabled: false,
ChipIngressBufferSize: 1000,
ChipIngressMaxBatchSize: 500,
ChipIngressSendInterval: 100 * time.Millisecond,
ChipIngressSendTimeout: 3 * time.Second,
ChipIngressDrainTimeout: 10 * time.Second,
ChipIngressMaxConcurrentSends: defaultMaxConcurrentSends,
ChipIngressMaxGRPCRequestSize: 10 * 1024 * 1024, // 10 MiB
// Auth (defaults to static auth mode with TTL=0)
AuthHeadersTTL: 0,
}
}
func TestDefaultConfig() Config {
config := DefaultConfig()
// Should be only disabled for testing
// Use simple (non-batched) exporter in tests for faster, deterministic teardown.
config.EmitterBatchProcessor = false
config.LogBatchProcessor = false
// Retries are disabled for testing
config.LogRetryConfig.MaxElapsedTime = 0 // Retry is disabled
config.TraceRetryConfig.MaxElapsedTime = 0 // Retry is disabled
config.MetricRetryConfig.MaxElapsedTime = 0 // Retry is disabled
// Auth disabled for testing (TTL=0 means static auth mode)
config.AuthHeadersTTL = 0
return config
}
func TestDefaultConfigHTTPClient() Config {
config := DefaultConfig()
// Should be only disabled for testing
// Use simple (non-batched) exporter in tests for faster, deterministic teardown.
config.EmitterBatchProcessor = false
config.LogBatchProcessor = false
config.OtelExporterGRPCEndpoint = ""
config.OtelExporterHTTPEndpoint = "localhost:4318"
// Auth disabled for testing (TTL=0 means static auth mode)
config.AuthHeadersTTL = 0
return config
}
func (c *RetryConfig) Copy() *RetryConfig {
newConfig := *c
return &newConfig
}
// Calculate if retry is enabled
func (c *RetryConfig) Enabled() bool {
if c == nil {
return false
}
return c.InitialInterval > 0 && c.MaxInterval > 0 && c.MaxElapsedTime > 0
}
// Implement getters for fields to avoid nil pointer dereference in case the config is not set
func (c *RetryConfig) GetInitialInterval() time.Duration {
if c == nil {
return 0
}
return c.InitialInterval
}
func (c *RetryConfig) GetMaxInterval() time.Duration {
if c == nil {
return 0
}
return c.MaxInterval
}
func (c *RetryConfig) GetMaxElapsedTime() time.Duration {
if c == nil {
return 0
}
return c.MaxElapsedTime
}
type OtelAttributes map[string]string
func (a OtelAttributes) AsStringAttributes() (attributes []attribute.KeyValue) {
for k, v := range a {
attributes = append(attributes, attribute.String(k, v))
}
return attributes
}