2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-09 23:59:39 +02:00
|
|
|
//
|
|
|
|
// 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 jaeger
|
|
|
|
|
|
|
|
import (
|
2019-10-08 20:56:58 +02:00
|
|
|
"context"
|
2019-10-23 08:01:33 +02:00
|
|
|
"encoding/binary"
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
"google.golang.org/api/support/bundler"
|
2019-09-25 22:22:33 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2020-03-10 22:58:45 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-06-05 04:05:53 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv/value"
|
2020-03-02 23:54:57 +02:00
|
|
|
gen "go.opentelemetry.io/otel/exporters/trace/jaeger/internal/gen-go/jaeger"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2020-03-10 22:58:45 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-09-09 23:59:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultServiceName = "OpenTelemetry"
|
|
|
|
|
2019-10-04 21:07:42 +02:00
|
|
|
type Option func(*options)
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
// options are the options to be used when initializing a Jaeger export.
|
2019-10-04 21:07:42 +02:00
|
|
|
type options struct {
|
2019-09-09 23:59:39 +02:00
|
|
|
// Process contains the information about the exporting process.
|
|
|
|
Process Process
|
|
|
|
|
|
|
|
//BufferMaxCount defines the total number of traces that can be buffered in memory
|
|
|
|
BufferMaxCount int
|
2020-03-10 22:58:45 +02:00
|
|
|
|
|
|
|
Config *sdktrace.Config
|
|
|
|
|
|
|
|
// RegisterGlobal is set to true if the trace provider of the new pipeline should be
|
|
|
|
// registered as Global Trace Provider
|
|
|
|
RegisterGlobal bool
|
2020-06-05 04:05:53 +02:00
|
|
|
|
|
|
|
Disabled bool
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 21:07:42 +02:00
|
|
|
// WithProcess sets the process with the information about the exporting process.
|
2020-04-22 05:30:57 +02:00
|
|
|
func WithProcess(process Process) Option {
|
2019-10-04 21:07:42 +02:00
|
|
|
return func(o *options) {
|
|
|
|
o.Process = process
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//WithBufferMaxCount defines the total number of traces that can be buffered in memory
|
2020-04-22 05:30:57 +02:00
|
|
|
func WithBufferMaxCount(bufferMaxCount int) Option {
|
2019-10-04 21:07:42 +02:00
|
|
|
return func(o *options) {
|
|
|
|
o.BufferMaxCount = bufferMaxCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:58:45 +02:00
|
|
|
// WithSDK sets the SDK config for the exporter pipeline.
|
2020-04-22 05:30:57 +02:00
|
|
|
func WithSDK(config *sdktrace.Config) Option {
|
2020-03-10 22:58:45 +02:00
|
|
|
return func(o *options) {
|
|
|
|
o.Config = config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterAsGlobal enables the registration of the trace provider of the new pipeline
|
|
|
|
// as Global Trace Provider.
|
2020-04-22 05:30:57 +02:00
|
|
|
func RegisterAsGlobal() Option {
|
2020-03-10 22:58:45 +02:00
|
|
|
return func(o *options) {
|
|
|
|
o.RegisterGlobal = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 04:05:53 +02:00
|
|
|
func WithDisabled(disabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.Disabled = disabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:58:45 +02:00
|
|
|
// NewRawExporter returns a trace.Exporter implementation that exports
|
2019-09-09 23:59:39 +02:00
|
|
|
// the collected spans to Jaeger.
|
2020-03-10 22:58:45 +02:00
|
|
|
func NewRawExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, error) {
|
2019-10-04 21:07:42 +02:00
|
|
|
o := options{}
|
2020-06-06 10:42:24 +02:00
|
|
|
opts = append(opts, WithDisabledFromEnv(), WithProcessFromEnv())
|
2019-10-04 21:07:42 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&o)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
2020-06-05 04:05:53 +02:00
|
|
|
if o.Disabled {
|
|
|
|
return &Exporter{
|
|
|
|
o: o,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
uploader, err := endpointOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-04 21:07:42 +02:00
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
service := o.Process.ServiceName
|
|
|
|
if service == "" {
|
|
|
|
service = defaultServiceName
|
|
|
|
}
|
2019-10-17 18:59:30 +02:00
|
|
|
tags := make([]*gen.Tag, 0, len(o.Process.Tags))
|
|
|
|
for _, tag := range o.Process.Tags {
|
|
|
|
t := keyValueToTag(tag)
|
|
|
|
if t != nil {
|
|
|
|
tags = append(tags, t)
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
e := &Exporter{
|
2019-10-04 21:07:42 +02:00
|
|
|
uploader: uploader,
|
2019-09-09 23:59:39 +02:00
|
|
|
process: &gen.Process{
|
|
|
|
ServiceName: service,
|
|
|
|
Tags: tags,
|
|
|
|
},
|
2020-03-10 22:58:45 +02:00
|
|
|
o: o,
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
bundler := bundler.NewBundler((*gen.Span)(nil), func(bundle interface{}) {
|
|
|
|
if err := e.upload(bundle.([]*gen.Span)); err != nil {
|
2020-06-02 21:30:55 +02:00
|
|
|
global.Handle(err)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set BufferedByteLimit with the total number of spans that are permissible to be held in memory.
|
|
|
|
// This needs to be done since the size of messages is always set to 1. Failing to set this would allow
|
|
|
|
// 1G messages to be held in memory since that is the default value of BufferedByteLimit.
|
|
|
|
if o.BufferMaxCount != 0 {
|
|
|
|
bundler.BufferedByteLimit = o.BufferMaxCount
|
|
|
|
}
|
|
|
|
|
|
|
|
e.bundler = bundler
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 22:58:45 +02:00
|
|
|
// NewExportPipeline sets up a complete export pipeline
|
|
|
|
// with the recommended setup for trace provider
|
|
|
|
func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (*sdktrace.Provider, func(), error) {
|
|
|
|
exporter, err := NewRawExporter(endpointOption, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
syncer := sdktrace.WithSyncer(exporter)
|
|
|
|
tp, err := sdktrace.NewProvider(syncer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if exporter.o.Config != nil {
|
|
|
|
tp.ApplyConfig(*exporter.o.Config)
|
|
|
|
}
|
|
|
|
if exporter.o.RegisterGlobal {
|
|
|
|
global.SetTraceProvider(tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tp, exporter.Flush, nil
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
// Process contains the information exported to jaeger about the source
|
|
|
|
// of the trace data.
|
|
|
|
type Process struct {
|
|
|
|
// ServiceName is the Jaeger service name.
|
|
|
|
ServiceName string
|
|
|
|
|
|
|
|
// Tags are added to Jaeger Process exports
|
2020-05-14 01:06:03 +02:00
|
|
|
Tags []kv.KeyValue
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 05:34:53 +02:00
|
|
|
// Exporter is an implementation of trace.SpanSyncer that uploads spans to Jaeger.
|
2019-09-09 23:59:39 +02:00
|
|
|
type Exporter struct {
|
2019-10-04 21:07:42 +02:00
|
|
|
process *gen.Process
|
|
|
|
bundler *bundler.Bundler
|
|
|
|
uploader batchUploader
|
2020-03-10 22:58:45 +02:00
|
|
|
o options
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
var _ export.SpanSyncer = (*Exporter)(nil)
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
// ExportSpan exports a SpanData to Jaeger.
|
2019-10-08 20:56:58 +02:00
|
|
|
func (e *Exporter) ExportSpan(ctx context.Context, d *export.SpanData) {
|
2020-06-05 04:05:53 +02:00
|
|
|
if e.o.Disabled {
|
|
|
|
return
|
|
|
|
}
|
2019-10-08 20:56:58 +02:00
|
|
|
_ = e.bundler.Add(spanDataToThrift(d), 1)
|
2019-09-09 23:59:39 +02:00
|
|
|
// TODO(jbd): Handle oversized bundlers.
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
func spanDataToThrift(data *export.SpanData) *gen.Span {
|
2019-09-09 23:59:39 +02:00
|
|
|
tags := make([]*gen.Tag, 0, len(data.Attributes))
|
2019-09-25 22:22:33 +02:00
|
|
|
for _, kv := range data.Attributes {
|
2019-10-17 18:59:30 +02:00
|
|
|
tag := keyValueToTag(kv)
|
|
|
|
if tag != nil {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
// TODO (jmacd): OTel has a broad "last value wins"
|
|
|
|
// semantic. Should resources be appended before span
|
|
|
|
// attributes, above, to allow span attributes to
|
|
|
|
// overwrite resource attributes?
|
2020-03-17 01:38:21 +02:00
|
|
|
if data.Resource != nil {
|
2020-04-23 21:10:58 +02:00
|
|
|
for iter := data.Resource.Iter(); iter.Next(); {
|
|
|
|
if tag := keyValueToTag(iter.Attribute()); tag != nil {
|
2020-03-17 01:38:21 +02:00
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:38:59 +02:00
|
|
|
tags = append(tags,
|
|
|
|
getInt64Tag("status.code", int64(data.StatusCode)),
|
|
|
|
getStringTag("status.message", data.StatusMessage),
|
2020-02-01 00:03:04 +02:00
|
|
|
getStringTag("span.kind", data.SpanKind.String()),
|
2019-09-09 23:59:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure that if Status.Code is not OK, that we set the "error" tag on the Jaeger span.
|
|
|
|
// See Issue https://github.com/census-instrumentation/opencensus-go/issues/1041
|
2020-03-07 20:38:59 +02:00
|
|
|
if data.StatusCode != codes.OK {
|
2019-09-25 22:22:33 +02:00
|
|
|
tags = append(tags, getBoolTag("error", true))
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var logs []*gen.Log
|
|
|
|
for _, a := range data.MessageEvents {
|
|
|
|
fields := make([]*gen.Tag, 0, len(a.Attributes))
|
|
|
|
for _, kv := range a.Attributes {
|
2019-10-17 18:59:30 +02:00
|
|
|
tag := keyValueToTag(kv)
|
2019-09-09 23:59:39 +02:00
|
|
|
if tag != nil {
|
|
|
|
fields = append(fields, tag)
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 20:13:05 +02:00
|
|
|
fields = append(fields, getStringTag("name", a.Name))
|
2019-09-09 23:59:39 +02:00
|
|
|
logs = append(logs, &gen.Log{
|
2019-09-25 22:22:33 +02:00
|
|
|
Timestamp: a.Time.UnixNano() / 1000,
|
2019-09-09 23:59:39 +02:00
|
|
|
Fields: fields,
|
|
|
|
})
|
|
|
|
}
|
2019-10-07 18:40:36 +02:00
|
|
|
|
|
|
|
var refs []*gen.SpanRef
|
|
|
|
for _, link := range data.Links {
|
|
|
|
refs = append(refs, &gen.SpanRef{
|
2019-10-23 08:01:33 +02:00
|
|
|
TraceIdHigh: int64(binary.BigEndian.Uint64(link.TraceID[0:8])),
|
|
|
|
TraceIdLow: int64(binary.BigEndian.Uint64(link.TraceID[8:16])),
|
2019-10-28 19:05:06 +02:00
|
|
|
SpanId: int64(binary.BigEndian.Uint64(link.SpanID[:])),
|
2019-10-07 18:40:36 +02:00
|
|
|
// TODO(paivagustavo): properly set the reference type when specs are defined
|
|
|
|
// see https://github.com/open-telemetry/opentelemetry-specification/issues/65
|
|
|
|
RefType: gen.SpanRefType_CHILD_OF,
|
|
|
|
})
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
return &gen.Span{
|
2019-10-23 08:01:33 +02:00
|
|
|
TraceIdHigh: int64(binary.BigEndian.Uint64(data.SpanContext.TraceID[0:8])),
|
|
|
|
TraceIdLow: int64(binary.BigEndian.Uint64(data.SpanContext.TraceID[8:16])),
|
2019-10-28 19:05:06 +02:00
|
|
|
SpanId: int64(binary.BigEndian.Uint64(data.SpanContext.SpanID[:])),
|
|
|
|
ParentSpanId: int64(binary.BigEndian.Uint64(data.ParentSpanID[:])),
|
2019-09-09 23:59:39 +02:00
|
|
|
OperationName: data.Name, // TODO: if span kind is added then add prefix "Sent"/"Recv"
|
2019-09-25 23:37:36 +02:00
|
|
|
Flags: int32(data.SpanContext.TraceFlags),
|
2019-09-09 23:59:39 +02:00
|
|
|
StartTime: data.StartTime.UnixNano() / 1000,
|
|
|
|
Duration: data.EndTime.Sub(data.StartTime).Nanoseconds() / 1000,
|
|
|
|
Tags: tags,
|
|
|
|
Logs: logs,
|
2019-10-07 18:40:36 +02:00
|
|
|
References: refs,
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func keyValueToTag(keyValue kv.KeyValue) *gen.Tag {
|
2019-09-25 22:22:33 +02:00
|
|
|
var tag *gen.Tag
|
2020-05-14 01:06:03 +02:00
|
|
|
switch keyValue.Value.Type() {
|
|
|
|
case value.STRING:
|
|
|
|
s := keyValue.Value.AsString()
|
2019-09-25 22:22:33 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VStr: &s,
|
2019-09-25 22:22:33 +02:00
|
|
|
VType: gen.TagType_STRING,
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
case value.BOOL:
|
|
|
|
b := keyValue.Value.AsBool()
|
2019-09-25 22:22:33 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VBool: &b,
|
2019-09-25 22:22:33 +02:00
|
|
|
VType: gen.TagType_BOOL,
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
case value.INT32:
|
|
|
|
i := int64(keyValue.Value.AsInt32())
|
2019-09-25 22:22:33 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VLong: &i,
|
2019-09-25 22:22:33 +02:00
|
|
|
VType: gen.TagType_LONG,
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
case value.INT64:
|
|
|
|
i := keyValue.Value.AsInt64()
|
2019-10-31 00:01:19 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VLong: &i,
|
|
|
|
VType: gen.TagType_LONG,
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
case value.FLOAT32:
|
|
|
|
f := float64(keyValue.Value.AsFloat32())
|
2019-10-31 00:01:19 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VDouble: &f,
|
|
|
|
VType: gen.TagType_DOUBLE,
|
|
|
|
}
|
2020-05-14 01:06:03 +02:00
|
|
|
case value.FLOAT64:
|
|
|
|
f := keyValue.Value.AsFloat64()
|
2019-09-25 22:22:33 +02:00
|
|
|
tag = &gen.Tag{
|
2020-05-14 01:06:03 +02:00
|
|
|
Key: string(keyValue.Key),
|
2019-10-31 00:01:19 +02:00
|
|
|
VDouble: &f,
|
2019-09-25 22:22:33 +02:00
|
|
|
VType: gen.TagType_DOUBLE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInt64Tag(k string, i int64) *gen.Tag {
|
|
|
|
return &gen.Tag{
|
|
|
|
Key: k,
|
|
|
|
VLong: &i,
|
|
|
|
VType: gen.TagType_LONG,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStringTag(k, s string) *gen.Tag {
|
|
|
|
return &gen.Tag{
|
|
|
|
Key: k,
|
|
|
|
VStr: &s,
|
|
|
|
VType: gen.TagType_STRING,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBoolTag(k string, b bool) *gen.Tag {
|
|
|
|
return &gen.Tag{
|
|
|
|
Key: k,
|
|
|
|
VBool: &b,
|
|
|
|
VType: gen.TagType_BOOL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
// Flush waits for exported trace spans to be uploaded.
|
|
|
|
//
|
|
|
|
// This is useful if your program is ending and you do not want to lose recent spans.
|
|
|
|
func (e *Exporter) Flush() {
|
2020-06-05 04:05:53 +02:00
|
|
|
if e.o.Disabled {
|
|
|
|
return
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
e.bundler.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) upload(spans []*gen.Span) error {
|
2020-06-05 04:05:53 +02:00
|
|
|
if e.o.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
batch := &gen.Batch{
|
|
|
|
Spans: spans,
|
|
|
|
Process: e.process,
|
|
|
|
}
|
|
|
|
|
2019-10-04 21:07:42 +02:00
|
|
|
return e.uploader.upload(batch)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|