-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
exporter.go
163 lines (141 loc) · 4.6 KB
/
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
// 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://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 jaegerthrifthttpexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/jaegerthrifthttpexporter"
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/apache/thrift/lib/go/thrift"
"github.com/jaegertracing/jaeger/model"
jaegerThriftConverter "github.com/jaegertracing/jaeger/model/converter/thrift/jaeger"
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/ptrace"
jaegertranslator "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
)
func newTracesExporter(config *Config, params exporter.CreateSettings) (exporter.Traces, error) {
s := &jaegerThriftHTTPSender{
config: config,
settings: params.TelemetrySettings,
}
return exporterhelper.NewTracesExporter(
context.TODO(),
params,
config,
s.pushTraceData,
exporterhelper.WithStart(s.start),
)
}
// jaegerThriftHTTPSender forwards spans encoded in the jaeger thrift
// format to a http server.
type jaegerThriftHTTPSender struct {
config *Config
client *http.Client
settings component.TelemetrySettings
}
// start starts the exporter
func (s *jaegerThriftHTTPSender) start(_ context.Context, host component.Host) (err error) {
s.client, err = s.config.HTTPClientSettings.ToClient(host, s.settings)
if err != nil {
return consumererror.NewPermanent(err)
}
return nil
}
func (s *jaegerThriftHTTPSender) pushTraceData(
ctx context.Context,
td ptrace.Traces,
) error {
batches, err := jaegertranslator.ProtoFromTraces(td)
if err != nil {
return consumererror.NewPermanent(fmt.Errorf("failed to push trace data via Jaeger Thrift HTTP exporter: %w", err))
}
for i := 0; i < len(batches); i++ {
body, err := serializeThrift(ctx, batches[i])
if err != nil {
return consumererror.NewPermanent(err)
}
req, err := http.NewRequest("POST", s.config.HTTPClientSettings.Endpoint, body)
if err != nil {
return consumererror.NewPermanent(err)
}
req.Header.Set("Content-Type", "application/x-thrift")
resp, err := s.client.Do(req)
if err != nil {
return consumererror.NewPermanent(err)
}
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
err = fmt.Errorf(
"HTTP %d %q",
resp.StatusCode,
http.StatusText(resp.StatusCode))
return consumererror.NewPermanent(err)
}
}
return nil
}
func serializeThrift(ctx context.Context, batch *model.Batch) (*bytes.Buffer, error) {
thriftSpans := jaegerThriftConverter.FromDomain(batch.GetSpans())
thriftProcess := jaeger.Process{
ServiceName: batch.GetProcess().GetServiceName(),
Tags: convertTagsToThrift(batch.GetProcess().GetTags()),
}
thriftBatch := jaeger.Batch{
Spans: thriftSpans,
Process: &thriftProcess,
}
t := thrift.NewTMemoryBuffer()
p := thrift.NewTBinaryProtocolConf(t, nil)
if err := thriftBatch.Write(ctx, p); err != nil {
return nil, err
}
return t.Buffer, nil
}
func convertTagsToThrift(tags []model.KeyValue) []*jaeger.Tag {
thriftTags := make([]*jaeger.Tag, 0, len(tags))
for i := 0; i < len(tags); i++ {
tag := tags[i]
thriftTag := &jaeger.Tag{Key: tag.GetKey()}
switch tag.GetVType() {
case model.ValueType_STRING:
str := tag.GetVStr()
thriftTag.VStr = &str
thriftTag.VType = jaeger.TagType_STRING
case model.ValueType_INT64:
i := tag.GetVInt64()
thriftTag.VLong = &i
thriftTag.VType = jaeger.TagType_LONG
case model.ValueType_BOOL:
b := tag.GetVBool()
thriftTag.VBool = &b
thriftTag.VType = jaeger.TagType_BOOL
case model.ValueType_FLOAT64:
d := tag.GetVFloat64()
thriftTag.VDouble = &d
thriftTag.VType = jaeger.TagType_DOUBLE
default:
str := "<Unknown tag type for key \"" + tag.GetKey() + "\">"
thriftTag.VStr = &str
thriftTag.VType = jaeger.TagType_STRING
}
thriftTags = append(thriftTags, thriftTag)
}
return thriftTags
}