-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtrace_exporter.go
165 lines (136 loc) · 4.25 KB
/
trace_exporter.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package loadbalancingexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter"
import (
"context"
"errors"
"fmt"
"sync"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/otlpexporter"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal"
)
var _ exporter.Traces = (*traceExporterImp)(nil)
type exporterTraces map[*wrappedExporter]ptrace.Traces
type traceExporterImp struct {
loadBalancer *loadBalancer
routingKey routingKey
stopped bool
shutdownWg sync.WaitGroup
}
// Create new traces exporter
func newTracesExporter(params exporter.Settings, cfg component.Config) (*traceExporterImp, error) {
exporterFactory := otlpexporter.NewFactory()
lb, err := newLoadBalancer(params, cfg, func(ctx context.Context, endpoint string) (component.Component, error) {
oCfg := buildExporterConfig(cfg.(*Config), endpoint)
return exporterFactory.CreateTracesExporter(ctx, params, &oCfg)
})
if err != nil {
return nil, err
}
traceExporter := traceExporterImp{loadBalancer: lb, routingKey: traceIDRouting}
switch cfg.(*Config).RoutingKey {
case svcRoutingStr:
traceExporter.routingKey = svcRouting
case traceIDRoutingStr, "":
default:
return nil, fmt.Errorf("unsupported routing_key: %s", cfg.(*Config).RoutingKey)
}
return &traceExporter, nil
}
func buildExporterConfig(cfg *Config, endpoint string) otlpexporter.Config {
oCfg := cfg.Protocol.OTLP
oCfg.Endpoint = endpoint
return oCfg
}
func (e *traceExporterImp) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
func (e *traceExporterImp) Start(ctx context.Context, host component.Host) error {
return e.loadBalancer.Start(ctx, host)
}
func (e *traceExporterImp) Shutdown(ctx context.Context) error {
err := e.loadBalancer.Shutdown(ctx)
e.stopped = true
e.shutdownWg.Wait()
return err
}
func (e *traceExporterImp) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
batches := batchpersignal.SplitTraces(td)
exporterSegregatedTraces := make(exporterTraces)
endpoints := make(map[*wrappedExporter]string)
for _, batch := range batches {
routingID, err := routingIdentifiersFromTraces(batch, e.routingKey)
if err != nil {
return err
}
for rid := range routingID {
exp, endpoint, err := e.loadBalancer.exporterAndEndpoint([]byte(rid))
if err != nil {
return err
}
_, ok := exporterSegregatedTraces[exp]
if !ok {
exp.consumeWG.Add(1)
exporterSegregatedTraces[exp] = ptrace.NewTraces()
}
exporterSegregatedTraces[exp] = mergeTraces(exporterSegregatedTraces[exp], batch)
endpoints[exp] = endpoint
}
}
var errs error
for exp, td := range exporterSegregatedTraces {
start := time.Now()
err := exp.ConsumeTraces(ctx, td)
exp.consumeWG.Done()
errs = multierr.Append(errs, err)
duration := time.Since(start)
if err == nil {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoints[exp]), successTrueMutator},
mBackendLatency.M(duration.Milliseconds()))
} else {
_ = stats.RecordWithTags(
ctx,
[]tag.Mutator{tag.Upsert(endpointTagKey, endpoints[exp]), successFalseMutator},
mBackendLatency.M(duration.Milliseconds()))
}
}
return errs
}
func routingIdentifiersFromTraces(td ptrace.Traces, key routingKey) (map[string]bool, error) {
ids := make(map[string]bool)
rs := td.ResourceSpans()
if rs.Len() == 0 {
return nil, errors.New("empty resource spans")
}
ils := rs.At(0).ScopeSpans()
if ils.Len() == 0 {
return nil, errors.New("empty scope spans")
}
spans := ils.At(0).Spans()
if spans.Len() == 0 {
return nil, errors.New("empty spans")
}
if key == svcRouting {
for i := 0; i < rs.Len(); i++ {
svc, ok := rs.At(i).Resource().Attributes().Get("service.name")
if !ok {
return nil, errors.New("unable to get service name")
}
ids[svc.Str()] = true
}
return ids, nil
}
tid := spans.At(0).TraceID()
ids[string(tid[:])] = true
return ids, nil
}