forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsxray.go
119 lines (109 loc) · 3.89 KB
/
awsxray.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
// Copyright 2019, 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:https://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 awsxrayexporter
import (
"context"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/xray"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/translator"
)
const (
maxSegmentsPerPut = int(50) // limit imposed by PutTraceSegments API
)
// NewTraceExporter creates an component.TraceExporterOld that converts to an X-Ray PutTraceSegments
// request and then posts the request to the configured region's X-Ray endpoint.
func NewTraceExporter(config configmodels.Exporter, logger *zap.Logger, cn connAttr) (component.TraceExporter, error) {
typeLog := zap.String("type", string(config.Type()))
nameLog := zap.String("name", config.Name())
awsConfig, session, err := GetAWSConfigSession(logger, cn, config.(*Config))
if err != nil {
return nil, err
}
xrayClient := NewXRay(logger, awsConfig, session)
return exporterhelper.NewTraceExporter(
config,
logger,
func(ctx context.Context, td pdata.Traces) (totalDroppedSpans int, err error) {
logger.Debug("TraceExporter", typeLog, nameLog, zap.Int("#spans", td.SpanCount()))
totalDroppedSpans = 0
documents := make([]*string, 0, td.SpanCount())
for i := 0; i < td.ResourceSpans().Len(); i++ {
rspans := td.ResourceSpans().At(i)
if rspans.IsNil() {
continue
}
resource := rspans.Resource()
for j := 0; j < rspans.InstrumentationLibrarySpans().Len(); j++ {
ispans := rspans.InstrumentationLibrarySpans().At(j)
if ispans.IsNil() {
continue
}
spans := ispans.Spans()
for k := 0; k < spans.Len(); k++ {
span := spans.At(k)
if span.IsNil() {
continue
}
document, localErr := translator.MakeSegmentDocumentString(span, resource,
config.(*Config).IndexedAttributes, config.(*Config).IndexAllAttributes)
if localErr != nil {
totalDroppedSpans++
continue
}
documents = append(documents, &document)
}
}
}
for offset := 0; offset < len(documents); offset += maxSegmentsPerPut {
nextOffset := offset + maxSegmentsPerPut
if nextOffset > td.SpanCount() {
nextOffset = td.SpanCount()
}
input := xray.PutTraceSegmentsInput{TraceSegmentDocuments: documents[offset:nextOffset]}
logger.Debug("request: " + input.String())
output, localErr := xrayClient.PutTraceSegments(&input)
if localErr != nil {
logger.Debug("response error", zap.Error(localErr))
err = wrapErrorIfBadRequest(&localErr) // record error
}
if output != nil {
logger.Debug("response: " + output.String())
if output.UnprocessedTraceSegments != nil {
totalDroppedSpans += len(output.UnprocessedTraceSegments)
}
}
if err != nil {
break
}
}
return totalDroppedSpans, err
},
exporterhelper.WithShutdown(func(context.Context) error {
return logger.Sync()
}),
)
}
func wrapErrorIfBadRequest(err *error) error {
_, ok := (*err).(awserr.RequestFailure)
if ok && (*err).(awserr.RequestFailure).StatusCode() < 500 {
return consumererror.Permanent(*err)
}
return *err
}