-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathrouting.go
183 lines (151 loc) · 5.57 KB
/
routing.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package routingprocessor
import (
"context"
"errors"
"fmt"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/model/pdata"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
)
var (
errNoExporters = errors.New("no exporters defined for the route")
errNoTableItems = errors.New("the routing table is empty")
errNoMissingFromAttribute = errors.New("the FromAttribute property is empty")
errExporterNotFound = errors.New("exporter not found")
)
var _ component.TracesProcessor = (*processorImp)(nil)
type processorImp struct {
logger *zap.Logger
config Config
defaultTracesExporters []component.TracesExporter
traceExporters map[string][]component.TracesExporter
}
// Crete new processor
func newProcessor(logger *zap.Logger, cfg config.Processor) (*processorImp, error) {
logger.Info("building processor")
oCfg := cfg.(*Config)
// validate that every route has at least one exporter
for _, item := range oCfg.Table {
if len(item.Exporters) == 0 {
return nil, fmt.Errorf("invalid route %s: %w", item.Value, errNoExporters)
}
}
// validate that there's at least one item in the table
if len(oCfg.Table) == 0 {
return nil, fmt.Errorf("invalid routing table: %w", errNoTableItems)
}
// we also need a "FromAttribute" value
if len(oCfg.FromAttribute) == 0 {
return nil, fmt.Errorf("invalid attribute to read the route's value from: %w", errNoMissingFromAttribute)
}
return &processorImp{
logger: logger,
config: *oCfg,
traceExporters: make(map[string][]component.TracesExporter),
}, nil
}
func (e *processorImp) Start(_ context.Context, host component.Host) error {
// first, let's build a map of exporter names with the exporter instances
source := host.GetExporters()
availableExporters := map[string]component.TracesExporter{}
for k, exp := range source[config.TracesDataType] {
traceExp, ok := exp.(component.TracesExporter)
if !ok {
return fmt.Errorf("the exporter %q isn't a trace exporter", k.Name())
}
availableExporters[k.String()] = traceExp
}
// default exporters
if err := e.registerExportersForDefaultRoute(availableExporters, e.config.DefaultExporters); err != nil {
return err
}
// exporters for each defined value
for _, item := range e.config.Table {
if err := e.registerExportersForRoute(item.Value, availableExporters, item.Exporters); err != nil {
return err
}
}
return nil
}
func (e *processorImp) registerExportersForDefaultRoute(available map[string]component.TracesExporter, requested []string) error {
for _, exp := range requested {
v, ok := available[exp]
if !ok {
return fmt.Errorf("error registering default exporter %q: %w", exp, errExporterNotFound)
}
e.defaultTracesExporters = append(e.defaultTracesExporters, v)
}
return nil
}
func (e *processorImp) registerExportersForRoute(route string, available map[string]component.TracesExporter, requested []string) error {
for _, exp := range requested {
v, ok := available[exp]
if !ok {
return fmt.Errorf("error registering route %q for exporter %q: %w", route, exp, errExporterNotFound)
}
e.traceExporters[route] = append(e.traceExporters[route], v)
}
return nil
}
func (e *processorImp) Shutdown(context.Context) error {
return nil
}
func (e *processorImp) ConsumeTraces(ctx context.Context, td pdata.Traces) error {
value := e.extractValueFromContext(ctx)
if len(value) == 0 {
// the attribute's value hasn't been found, send data to the default exporter
return e.pushDataToExporters(ctx, td, e.defaultTracesExporters)
}
if _, ok := e.traceExporters[value]; !ok {
// the value has been found, but there are no exporters for the value
return e.pushDataToExporters(ctx, td, e.defaultTracesExporters)
}
// found the appropriate router, using it
return e.pushDataToExporters(ctx, td, e.traceExporters[value])
}
func (e *processorImp) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
func (e *processorImp) pushDataToExporters(ctx context.Context, td pdata.Traces, exporters []component.TracesExporter) error {
// TODO: determine the proper action when errors happen
for _, exp := range exporters {
if err := exp.ConsumeTraces(ctx, td); err != nil {
return err
}
}
return nil
}
func (e *processorImp) extractValueFromContext(ctx context.Context) string {
// right now, we only support looking up attributes from requests that have gone through the gRPC server
// in that case, it will add the HTTP headers as context metadata
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ""
}
// we have gRPC metadata in the context but does it have our key?
values, ok := md[strings.ToLower(e.config.FromAttribute)]
if !ok {
return ""
}
if len(values) > 1 {
e.logger.Debug("more than one value found for the attribute, using only the first", zap.Strings("values", values), zap.String("attribute", e.config.FromAttribute))
}
return values[0]
}