|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package loadbalancingexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | + |
| 15 | + "go.opencensus.io/stats" |
| 16 | + "go.opencensus.io/tag" |
| 17 | + "go.opentelemetry.io/collector/component" |
| 18 | + "go.opentelemetry.io/collector/consumer" |
| 19 | + "go.opentelemetry.io/collector/exporter" |
| 20 | + "go.opentelemetry.io/collector/exporter/otlpexporter" |
| 21 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 22 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 23 | + conventions "go.opentelemetry.io/collector/semconv/v1.6.1" |
| 24 | + "go.uber.org/multierr" |
| 25 | + |
| 26 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal" |
| 27 | +) |
| 28 | + |
| 29 | +var _ exporter.Metrics = (*metricExporterImp)(nil) |
| 30 | + |
| 31 | +type metricExporterImp struct { |
| 32 | + loadBalancer loadBalancer |
| 33 | + routingKey routingKey |
| 34 | + |
| 35 | + stopped bool |
| 36 | + shutdownWg sync.WaitGroup |
| 37 | +} |
| 38 | + |
| 39 | +func newMetricsExporter(params exporter.CreateSettings, cfg component.Config) (*metricExporterImp, error) { |
| 40 | + exporterFactory := otlpexporter.NewFactory() |
| 41 | + |
| 42 | + lb, err := newLoadBalancer(params, cfg, func(ctx context.Context, endpoint string) (component.Component, error) { |
| 43 | + oCfg := buildExporterConfig(cfg.(*Config), endpoint) |
| 44 | + return exporterFactory.CreateMetricsExporter(ctx, params, &oCfg) |
| 45 | + }) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + metricExporter := metricExporterImp{loadBalancer: lb, routingKey: svcRouting} |
| 51 | + |
| 52 | + switch cfg.(*Config).RoutingKey { |
| 53 | + case "service", "": |
| 54 | + // default case for empty routing key |
| 55 | + metricExporter.routingKey = svcRouting |
| 56 | + case "resource": |
| 57 | + metricExporter.routingKey = resourceRouting |
| 58 | + case "metric": |
| 59 | + metricExporter.routingKey = metricNameRouting |
| 60 | + default: |
| 61 | + return nil, fmt.Errorf("unsupported routing_key: %q", cfg.(*Config).RoutingKey) |
| 62 | + } |
| 63 | + return &metricExporter, nil |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +func (e *metricExporterImp) Capabilities() consumer.Capabilities { |
| 68 | + return consumer.Capabilities{MutatesData: false} |
| 69 | +} |
| 70 | + |
| 71 | +func (e *metricExporterImp) Start(ctx context.Context, host component.Host) error { |
| 72 | + return e.loadBalancer.Start(ctx, host) |
| 73 | +} |
| 74 | + |
| 75 | +func (e *metricExporterImp) Shutdown(context.Context) error { |
| 76 | + e.stopped = true |
| 77 | + e.shutdownWg.Wait() |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (e *metricExporterImp) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error { |
| 82 | + var errs error |
| 83 | + batches := batchpersignal.SplitMetrics(md) |
| 84 | + for _, batch := range batches { |
| 85 | + errs = multierr.Append(errs, e.consumeMetric(ctx, batch)) |
| 86 | + } |
| 87 | + |
| 88 | + return errs |
| 89 | +} |
| 90 | + |
| 91 | +func (e *metricExporterImp) consumeMetric(ctx context.Context, md pmetric.Metrics) error { |
| 92 | + var exp component.Component |
| 93 | + routingIds, err := routingIdentifiersFromMetrics(md, e.routingKey) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + for rid := range routingIds { |
| 98 | + endpoint := e.loadBalancer.Endpoint([]byte(rid)) |
| 99 | + exp, err = e.loadBalancer.Exporter(endpoint) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + te, ok := exp.(exporter.Metrics) |
| 105 | + if !ok { |
| 106 | + return fmt.Errorf("unable to export metrics, unexpected exporter type: expected exporter.Metrics but got %T", exp) |
| 107 | + } |
| 108 | + |
| 109 | + start := time.Now() |
| 110 | + err = te.ConsumeMetrics(ctx, md) |
| 111 | + duration := time.Since(start) |
| 112 | + |
| 113 | + if err == nil { |
| 114 | + _ = stats.RecordWithTags( |
| 115 | + ctx, |
| 116 | + []tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successTrueMutator}, |
| 117 | + mBackendLatency.M(duration.Milliseconds())) |
| 118 | + } else { |
| 119 | + _ = stats.RecordWithTags( |
| 120 | + ctx, |
| 121 | + []tag.Mutator{tag.Upsert(endpointTagKey, endpoint), successFalseMutator}, |
| 122 | + mBackendLatency.M(duration.Milliseconds())) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return err |
| 127 | +} |
| 128 | + |
| 129 | +func routingIdentifiersFromMetrics(mds pmetric.Metrics, key routingKey) (map[string]bool, error) { |
| 130 | + ids := make(map[string]bool) |
| 131 | + |
| 132 | + // no need to test "empty labels" |
| 133 | + // no need to test "empty resources" |
| 134 | + |
| 135 | + rs := mds.ResourceMetrics() |
| 136 | + if rs.Len() == 0 { |
| 137 | + return nil, errors.New("empty resource metrics") |
| 138 | + } |
| 139 | + |
| 140 | + ils := rs.At(0).ScopeMetrics() |
| 141 | + if ils.Len() == 0 { |
| 142 | + return nil, errors.New("empty scope metrics") |
| 143 | + } |
| 144 | + |
| 145 | + metrics := ils.At(0).Metrics() |
| 146 | + if metrics.Len() == 0 { |
| 147 | + return nil, errors.New("empty metrics") |
| 148 | + } |
| 149 | + |
| 150 | + for i := 0; i < rs.Len(); i++ { |
| 151 | + resource := rs.At(i).Resource() |
| 152 | + switch key { |
| 153 | + default: |
| 154 | + case svcRouting, traceIDRouting: |
| 155 | + svc, ok := resource.Attributes().Get(conventions.AttributeServiceName) |
| 156 | + if !ok { |
| 157 | + return nil, errors.New("unable to get service name") |
| 158 | + } |
| 159 | + ids[svc.Str()] = true |
| 160 | + case metricNameRouting: |
| 161 | + sm := rs.At(i).ScopeMetrics() |
| 162 | + for j := 0; j < sm.Len(); j++ { |
| 163 | + metrics := sm.At(j).Metrics() |
| 164 | + for k := 0; k < metrics.Len(); k++ { |
| 165 | + md := metrics.At(k) |
| 166 | + rKey := metricRoutingKey(md) |
| 167 | + ids[rKey] = true |
| 168 | + } |
| 169 | + } |
| 170 | + case resourceRouting: |
| 171 | + sm := rs.At(i).ScopeMetrics() |
| 172 | + for j := 0; j < sm.Len(); j++ { |
| 173 | + metrics := sm.At(j).Metrics() |
| 174 | + for k := 0; k < metrics.Len(); k++ { |
| 175 | + md := metrics.At(k) |
| 176 | + rKey := resourceRoutingKey(md, resource.Attributes()) |
| 177 | + ids[rKey] = true |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return ids, nil |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +// maintain |
| 188 | +func sortedMapAttrs(attrs pcommon.Map) []string { |
| 189 | + keys := make([]string, 0) |
| 190 | + for k := range attrs.AsRaw() { |
| 191 | + keys = append(keys, k) |
| 192 | + } |
| 193 | + sort.Strings(keys) |
| 194 | + |
| 195 | + attrsHash := make([]string, 0) |
| 196 | + for _, k := range keys { |
| 197 | + attrsHash = append(attrsHash, k) |
| 198 | + if v, ok := attrs.Get(k); ok { |
| 199 | + attrsHash = append(attrsHash, v.AsString()) |
| 200 | + } |
| 201 | + } |
| 202 | + return attrsHash |
| 203 | +} |
| 204 | + |
| 205 | +func resourceRoutingKey(md pmetric.Metric, attrs pcommon.Map) string { |
| 206 | + attrsHash := sortedMapAttrs(attrs) |
| 207 | + attrsHash = append(attrsHash, md.Name()) |
| 208 | + routingRef := strings.Join(attrsHash, "") |
| 209 | + |
| 210 | + return routingRef |
| 211 | +} |
| 212 | + |
| 213 | +func metricRoutingKey(md pmetric.Metric) string { |
| 214 | + return md.Name() |
| 215 | +} |
0 commit comments