-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
381 lines (334 loc) · 9.52 KB
/
processor.go
File metadata and controls
381 lines (334 loc) · 9.52 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package processor contains the Processor struct and related types for processing incoming telemetry data and forwarding it to the appropriate backend.
package processor
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"slices"
"strconv"
"sync"
"time"
"github.com/matt-gp/otel-lgtm-proxy/internal/config"
"github.com/matt-gp/otel-lgtm-proxy/internal/logger"
"github.com/matt-gp/otel-lgtm-proxy/internal/util/cert"
"github.com/matt-gp/otel-lgtm-proxy/internal/util/request"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
logpb "go.opentelemetry.io/proto/otlp/logs/v1"
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"
resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
)
// Client is an interface for making HTTP requests.
//
//go:generate mockgen -package processor -source processor.go -destination processor_mock.go
type Client interface {
Do(req *http.Request) (*http.Response, error)
}
// ResourceData is an interface for OTLP resource types.
type ResourceData interface {
*logpb.ResourceLogs | *metricpb.ResourceMetrics | *tracepb.ResourceSpans
}
// Processor is a generic struct that processes incoming telemetry resource data and forwards it to the appropriate backend.
type Processor[T ResourceData] struct {
config *config.Config
endpoint *config.Endpoint
signalType string
client Client
logger log.Logger
meter metric.Meter
tracer trace.Tracer
proxyRecordsMetric metric.Int64Counter
proxyRequestsMetric metric.Int64Counter
proxyLatencyMetric metric.Int64Histogram
getResource func(T) *resourcepb.Resource
marshalResources func([]T) ([]byte, error)
}
// New creates a new generic Processor for any resource type.
func New[T ResourceData](
config *config.Config,
endpoint *config.Endpoint,
signalType string,
client Client,
logger log.Logger,
meter metric.Meter,
tracer trace.Tracer,
getResource func(T) *resourcepb.Resource,
marshalResources func([]T) ([]byte, error),
) (*Processor[T], error) {
// Create a counter for the total number of records processed by the proxy
proxyRecordsMetric, err := meter.Int64Counter(
"otel_lgtm_proxy_records_total",
metric.WithDescription("Total number of otel lgtm proxy records processed"),
)
if err != nil {
return nil, fmt.Errorf("failed to create otel lgtm proxy records counter: %w", err)
}
// Create a counter for the total number of requests processed by the proxy
proxyRequestsMetric, err := meter.Int64Counter(
"otel_lgtm_proxy_requests_total",
metric.WithDescription("Total number of otel lgtm proxy requests processed"),
)
if err != nil {
return nil, fmt.Errorf("failed to create otel lgtm proxy requests counter: %w", err)
}
// Create a histogram for the latency of requests processed by the proxy
proxyLatencyMetric, err := meter.Int64Histogram(
"otel_lgtm_proxy_request_duration_seconds",
metric.WithDescription("Latency of otel lgtm proxy requests"),
metric.WithUnit("ms"),
)
if err != nil {
return nil, fmt.Errorf("failed to create otel lgtm proxy latency histogram: %w", err)
}
// Configure TLS if enabled
if cert.TLSEnabled(&endpoint.TLS) {
tlsConfig, err := cert.CreateTLSConfig(endpoint)
if err != nil {
return nil, fmt.Errorf("failed to create TLS config: %w", err)
}
client.(*http.Client).Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
}
return &Processor[T]{
config: config,
endpoint: endpoint,
signalType: signalType,
client: client,
logger: logger,
meter: meter,
tracer: tracer,
proxyRecordsMetric: proxyRecordsMetric,
proxyRequestsMetric: proxyRequestsMetric,
proxyLatencyMetric: proxyLatencyMetric,
getResource: getResource,
marshalResources: marshalResources,
}, nil
}
func (p *Processor[T]) signalTypeAttr() attribute.KeyValue {
return attribute.String("signal.type", p.signalType)
}
func (p *Processor[T]) signalTypeLogAttr() log.KeyValue {
return log.String("signal.type", p.signalType)
}
// Partition partitions the resources by tenant.
func (p *Processor[T]) Partition(ctx context.Context, resources []T) map[string][]T {
ctx, span := p.tracer.Start(
ctx,
fmt.Sprintf("%s.partition", p.signalType),
trace.WithAttributes(
p.signalTypeAttr(),
),
)
defer span.End()
tenantMap := make(map[string][]T)
for _, resourceData := range resources {
resource := p.getResource(resourceData)
logger.Trace(
ctx,
p.logger,
fmt.Sprintf("%+v", resource),
p.signalTypeLogAttr(),
)
tenant := ""
// First, check for the dedicated tenant label
if p.config.Tenant.Label != "" {
for _, attr := range resource.GetAttributes() {
if attr.GetKey() == p.config.Tenant.Label {
tenant = attr.GetValue().GetStringValue()
break
}
}
}
// If not found and we have additional labels, check those
if tenant == "" && len(p.config.Tenant.Labels) > 0 {
for _, attr := range resource.GetAttributes() {
if slices.Contains(p.config.Tenant.Labels, attr.GetKey()) {
tenant = attr.GetValue().GetStringValue()
break
}
}
}
if tenant == "" {
if p.config.Tenant.Default == "" {
logger.Warn(
ctx,
p.logger,
"No tenant found in attributes and no default tenant configured",
p.signalTypeLogAttr(),
)
continue
}
tenant = p.config.Tenant.Default
resource.Attributes = append(resource.Attributes, &commonpb.KeyValue{
Key: p.config.Tenant.Label,
Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_StringValue{StringValue: tenant}},
})
}
tenantMap[tenant] = append(tenantMap[tenant], resourceData)
}
span.SetStatus(codes.Ok, "data partitioned")
return tenantMap
}
// Dispatch sends all the requests to the target.
func (p *Processor[T]) Dispatch(ctx context.Context, tenantMap map[string][]T) error {
waitGroup := sync.WaitGroup{}
for tenant, resources := range tenantMap {
ctx, span := p.tracer.Start(
ctx,
fmt.Sprintf("%s.dispatch", p.signalType),
trace.WithAttributes(
p.signalTypeAttr(),
attribute.String("signal.tenant", tenant),
),
)
defer span.End()
waitGroup.Add(1)
go func(tenant string, resources []T) {
defer waitGroup.Done()
tenantAttribute := attribute.String("signal.tenant", tenant)
resp, err := p.send(ctx, tenant, resources)
if err != nil {
p.proxyRecordsMetric.Add(
ctx,
int64(len(resources)),
metric.WithAttributes(
tenantAttribute,
p.signalTypeAttr(),
),
)
logger.Error(
ctx,
p.logger,
err.Error(),
p.signalTypeLogAttr(),
)
span.RecordError(err)
span.SetStatus(codes.Error, "failed to send")
return
}
p.proxyRecordsMetric.Add(
ctx,
int64(len(resources)),
metric.WithAttributes(
p.signalTypeAttr(),
tenantAttribute,
attribute.String(
"signal.response.status.code",
strconv.Itoa(resp.StatusCode),
),
),
)
p.proxyRequestsMetric.Add(
ctx,
1,
metric.WithAttributes(
p.signalTypeAttr(),
tenantAttribute,
attribute.String(
"signal.response.status.code",
strconv.Itoa(resp.StatusCode),
),
),
)
logger.Debug(
ctx,
p.logger,
fmt.Sprintf(
"sent %d records status %d for tenant %s",
len(resources),
resp.StatusCode,
tenant,
),
p.signalTypeLogAttr(),
)
logger.Trace(
ctx,
p.logger,
fmt.Sprintf("%+v", resources),
p.signalTypeLogAttr(),
)
span.SetStatus(codes.Ok, "sent successfully")
}(tenant, resources)
}
waitGroup.Wait()
return nil
}
// send sends an individual request to the target.
func (p *Processor[T]) send(
ctx context.Context,
tenant string,
resources []T,
) (http.Response, error) {
start := time.Now()
ctx, span := p.tracer.Start(ctx,
fmt.Sprintf("%s.send", p.signalType),
trace.WithAttributes(
p.signalTypeAttr(),
attribute.String("signal.tenant", tenant),
attribute.Int("signal.tenant.records", len(resources)),
),
)
defer span.End()
// Marshal resources to bytes
body, err := p.marshalResources(resources)
if err != nil {
return http.Response{}, fmt.Errorf("failed to marshal data: %w", err)
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
p.endpoint.Address,
io.NopCloser(bytes.NewReader(body)),
)
if err != nil {
return http.Response{}, fmt.Errorf("failed to create request: %w", err)
}
request.AddHeaders(
tenant,
req,
p.config,
p.endpoint.Headers,
)
resp, err := p.client.Do(req)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "failed to send")
return http.Response{}, fmt.Errorf("failed to send request: %w", err)
}
defer func() {
closeErr := resp.Body.Close()
if closeErr != nil {
logger.Error(
ctx,
p.logger,
fmt.Sprintf("failed to close response body: %v", closeErr),
p.signalTypeLogAttr(),
)
span.RecordError(closeErr)
span.SetStatus(codes.Error, "failed to close response body")
}
}()
respAttr := attribute.String(
"signal.response.status.code",
strconv.Itoa(resp.StatusCode),
)
span.SetAttributes(respAttr)
span.SetStatus(codes.Ok, "sent successfully")
p.proxyLatencyMetric.Record(ctx,
time.Since(start).Milliseconds(),
metric.WithAttributes(
respAttr,
p.signalTypeAttr(),
attribute.String("signal.tenant", tenant),
),
)
return *resp, nil
}