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.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package jaeger // import "go.opentelemetry.io/otel/exporters/trace/jaeger"
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
import (
|
2019-10-08 20:56:58 +02:00
|
|
|
"context"
|
2019-10-23 08:01:33 +02:00
|
|
|
"encoding/binary"
|
2021-03-08 19:32:02 +02:00
|
|
|
"encoding/json"
|
2020-09-09 19:19:03 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
"google.golang.org/api/support/bundler"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-10-05 20:36:03 +02:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
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"
|
2021-03-16 18:04:46 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2020-03-10 22:58:45 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2021-03-16 18:04:46 +02:00
|
|
|
"go.opentelemetry.io/otel/semconv"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-09-09 23:59:39 +02:00
|
|
|
)
|
|
|
|
|
2020-09-03 16:22:05 +02:00
|
|
|
const (
|
2021-03-08 19:32:02 +02:00
|
|
|
keyInstrumentationLibraryName = "otel.library.name"
|
|
|
|
keyInstrumentationLibraryVersion = "otel.library.version"
|
2020-09-03 16:22:05 +02:00
|
|
|
)
|
2019-09-09 23:59:39 +02:00
|
|
|
|
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
|
|
|
|
|
2020-06-09 02:05:16 +02:00
|
|
|
// BufferMaxCount defines the total number of traces that can be buffered in memory
|
2019-09-09 23:59:39 +02:00
|
|
|
BufferMaxCount int
|
2020-03-10 22:58:45 +02:00
|
|
|
|
2020-07-10 16:28:45 +02:00
|
|
|
// BatchMaxCount defines the maximum number of spans sent in one batch
|
|
|
|
BatchMaxCount int
|
|
|
|
|
2021-03-18 19:48:13 +02:00
|
|
|
// TracerProviderOptions defines the options for tracer provider of sdk.
|
|
|
|
TracerProviderOptions []sdktrace.TracerProviderOption
|
2020-03-10 22:58:45 +02:00
|
|
|
|
2020-06-05 04:05:53 +02:00
|
|
|
Disabled bool
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 02:05:16 +02:00
|
|
|
// 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-07-10 16:28:45 +02:00
|
|
|
// WithBatchMaxCount defines the maximum number of spans in one batch
|
|
|
|
func WithBatchMaxCount(batchMaxCount int) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.BatchMaxCount = batchMaxCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:48:13 +02:00
|
|
|
// WithSDKOptions configures options for tracer provider of sdk.
|
|
|
|
func WithSDKOptions(opts ...sdktrace.TracerProviderOption) Option {
|
2020-03-10 22:58:45 +02:00
|
|
|
return func(o *options) {
|
2021-03-18 19:48:13 +02:00
|
|
|
o.TracerProviderOptions = opts
|
2020-03-10 22:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:57:48 +02:00
|
|
|
// WithDisabled option will cause pipeline methods to use
|
|
|
|
// a no-op provider
|
2020-06-05 04:05:53 +02:00
|
|
|
func WithDisabled(disabled bool) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.Disabled = disabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// NewRawExporter returns an OTel Exporter implementation that exports the
|
|
|
|
// collected spans to Jaeger.
|
2020-06-09 02:05:16 +02:00
|
|
|
//
|
|
|
|
// It will IGNORE Disabled option.
|
2020-03-10 22:58:45 +02:00
|
|
|
func NewRawExporter(endpointOption EndpointOption, opts ...Option) (*Exporter, error) {
|
2020-06-09 02:05:16 +02:00
|
|
|
uploader, err := endpointOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-04 21:07:42 +02:00
|
|
|
o := options{}
|
2020-06-10 05:36:25 +02:00
|
|
|
opts = append(opts, WithProcessFromEnv())
|
2019-10-04 21:07:42 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&o)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
2019-10-04 21:07:42 +02:00
|
|
|
|
2021-03-16 18:04:46 +02:00
|
|
|
// Fetch default service.name from default resource for backup
|
|
|
|
var defaultServiceName string
|
|
|
|
defaultResource := resource.Default()
|
|
|
|
if value, exists := defaultResource.Set().Value(semconv.ServiceNameKey); exists {
|
|
|
|
defaultServiceName = value.AsString()
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
2021-03-16 18:04:46 +02:00
|
|
|
if defaultServiceName == "" {
|
|
|
|
return nil, fmt.Errorf("failed to get service name from default resource")
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
2021-03-16 18:04:46 +02:00
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
e := &Exporter{
|
2021-03-16 18:04:46 +02:00
|
|
|
uploader: uploader,
|
|
|
|
o: o,
|
|
|
|
defaultServiceName: defaultServiceName,
|
|
|
|
resourceFromProcess: processToResource(o.Process),
|
|
|
|
}
|
|
|
|
bundler := bundler.NewBundler((*export.SpanSnapshot)(nil), func(bundle interface{}) {
|
|
|
|
if err := e.upload(bundle.([]*export.SpanSnapshot)); err != nil {
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.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
|
|
|
|
}
|
|
|
|
|
2020-07-10 16:28:45 +02:00
|
|
|
// The default value bundler uses is 10, increase to send larger batches
|
|
|
|
if o.BatchMaxCount != 0 {
|
|
|
|
bundler.BundleCountThreshold = o.BatchMaxCount
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
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
|
2020-11-07 00:13:31 +02:00
|
|
|
func NewExportPipeline(endpointOption EndpointOption, opts ...Option) (trace.TracerProvider, func(), error) {
|
2020-06-09 02:05:16 +02:00
|
|
|
o := options{}
|
2020-06-10 05:36:25 +02:00
|
|
|
opts = append(opts, WithDisabledFromEnv())
|
2020-06-09 02:05:16 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&o)
|
|
|
|
}
|
|
|
|
if o.Disabled {
|
2020-11-07 00:13:31 +02:00
|
|
|
return trace.NewNoopTracerProvider(), func() {}, nil
|
2020-06-09 02:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 22:58:45 +02:00
|
|
|
exporter, err := NewRawExporter(endpointOption, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-09-09 19:19:03 +02:00
|
|
|
|
2021-03-18 19:48:13 +02:00
|
|
|
pOpts := append(exporter.o.TracerProviderOptions, sdktrace.WithSyncer(exporter))
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := sdktrace.NewTracerProvider(pOpts...)
|
2020-03-10 22:58:45 +02:00
|
|
|
return tp, exporter.Flush, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:57:48 +02:00
|
|
|
// InstallNewPipeline instantiates a NewExportPipeline with the
|
|
|
|
// recommended configuration and registers it globally.
|
|
|
|
func InstallNewPipeline(endpointOption EndpointOption, opts ...Option) (func(), error) {
|
|
|
|
tp, flushFn, err := NewExportPipeline(endpointOption, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.SetTracerProvider(tp)
|
2020-07-22 20:57:48 +02:00
|
|
|
return flushFn, 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
|
2021-02-18 19:59:37 +02:00
|
|
|
Tags []attribute.KeyValue
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
// Exporter is an implementation of an OTel 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
|
|
|
bundler *bundler.Bundler
|
|
|
|
uploader batchUploader
|
2020-03-10 22:58:45 +02:00
|
|
|
o options
|
2020-09-09 19:19:03 +02:00
|
|
|
|
|
|
|
stoppedMu sync.RWMutex
|
|
|
|
stopped bool
|
2021-03-16 18:04:46 +02:00
|
|
|
|
|
|
|
defaultServiceName string
|
|
|
|
resourceFromProcess *resource.Resource
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
var _ export.SpanExporter = (*Exporter)(nil)
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
// ExportSpans exports SpanSnapshots to Jaeger.
|
|
|
|
func (e *Exporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapshot) error {
|
2020-09-09 19:19:03 +02:00
|
|
|
e.stoppedMu.RLock()
|
|
|
|
stopped := e.stopped
|
|
|
|
e.stoppedMu.RUnlock()
|
|
|
|
if stopped {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
for _, span := range ss {
|
2020-09-09 19:19:03 +02:00
|
|
|
// TODO(jbd): Handle oversized bundlers.
|
2021-03-16 18:04:46 +02:00
|
|
|
err := e.bundler.Add(span, 1)
|
2020-09-09 19:19:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to bundle %q: %w", span.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush is used to wrap the bundler's Flush method for testing.
|
|
|
|
var flush = func(e *Exporter) {
|
|
|
|
e.bundler.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the exporter flushing any pending exports.
|
|
|
|
func (e *Exporter) Shutdown(ctx context.Context) error {
|
|
|
|
e.stoppedMu.Lock()
|
|
|
|
e.stopped = true
|
|
|
|
e.stoppedMu.Unlock()
|
|
|
|
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
// Shadow so if the goroutine is leaked in testing it doesn't cause a race
|
|
|
|
// condition when the file level var is reset.
|
|
|
|
go func(FlushFunc func(*Exporter)) {
|
|
|
|
// The OpenTelemetry specification is explicit in not having this
|
|
|
|
// method block so the preference here is to orphan this goroutine if
|
|
|
|
// the context is canceled or times out while this flushing is
|
|
|
|
// occurring. This is a consequence of the bundler Flush method not
|
|
|
|
// supporting a context.
|
|
|
|
FlushFunc(e)
|
|
|
|
done <- struct{}{}
|
|
|
|
}(flush)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
func spanSnapshotToThrift(ss *export.SpanSnapshot) *gen.Span {
|
|
|
|
tags := make([]*gen.Tag, 0, len(ss.Attributes))
|
|
|
|
for _, kv := range ss.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-12-11 07:15:44 +02:00
|
|
|
if il := ss.InstrumentationLibrary; il.Name != "" {
|
2020-09-03 16:22:05 +02:00
|
|
|
tags = append(tags, getStringTag(keyInstrumentationLibraryName, il.Name))
|
2020-06-10 07:15:53 +02:00
|
|
|
if il.Version != "" {
|
2020-09-03 16:22:05 +02:00
|
|
|
tags = append(tags, getStringTag(keyInstrumentationLibraryVersion, il.Version))
|
2020-06-10 07:15:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:32:02 +02:00
|
|
|
if ss.SpanKind != trace.SpanKindInternal {
|
|
|
|
tags = append(tags,
|
|
|
|
getStringTag("span.kind", ss.SpanKind.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ss.StatusCode != codes.Unset {
|
|
|
|
tags = append(tags,
|
|
|
|
getInt64Tag("status.code", int64(ss.StatusCode)),
|
|
|
|
getStringTag("status.message", ss.StatusMessage),
|
|
|
|
)
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2021-03-08 19:32:02 +02:00
|
|
|
if ss.StatusCode == codes.Error {
|
|
|
|
tags = append(tags, getBoolTag("error", true))
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var logs []*gen.Log
|
2020-12-11 07:15:44 +02:00
|
|
|
for _, a := range ss.MessageEvents {
|
2019-09-09 23:59:39 +02:00
|
|
|
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
|
2020-12-11 07:15:44 +02:00
|
|
|
for _, link := range ss.Links {
|
2021-03-09 18:17:29 +02:00
|
|
|
tid := link.TraceID()
|
|
|
|
sid := link.SpanID()
|
2019-10-07 18:40:36 +02:00
|
|
|
refs = append(refs, &gen.SpanRef{
|
2021-03-09 18:17:29 +02:00
|
|
|
TraceIdHigh: int64(binary.BigEndian.Uint64(tid[0:8])),
|
|
|
|
TraceIdLow: int64(binary.BigEndian.Uint64(tid[8:16])),
|
|
|
|
SpanId: int64(binary.BigEndian.Uint64(sid[:])),
|
2021-03-08 19:32:02 +02:00
|
|
|
RefType: gen.SpanRefType_FOLLOWS_FROM,
|
2019-10-07 18:40:36 +02:00
|
|
|
})
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
tid := ss.SpanContext.TraceID()
|
|
|
|
sid := ss.SpanContext.SpanID()
|
2021-03-30 17:59:54 +02:00
|
|
|
psid := ss.Parent.SpanID()
|
2019-09-09 23:59:39 +02:00
|
|
|
return &gen.Span{
|
2021-03-09 18:17:29 +02:00
|
|
|
TraceIdHigh: int64(binary.BigEndian.Uint64(tid[0:8])),
|
|
|
|
TraceIdLow: int64(binary.BigEndian.Uint64(tid[8:16])),
|
|
|
|
SpanId: int64(binary.BigEndian.Uint64(sid[:])),
|
2021-03-30 17:59:54 +02:00
|
|
|
ParentSpanId: int64(binary.BigEndian.Uint64(psid[:])),
|
2020-12-11 07:15:44 +02:00
|
|
|
OperationName: ss.Name, // TODO: if span kind is added then add prefix "Sent"/"Recv"
|
2021-03-09 18:17:29 +02:00
|
|
|
Flags: int32(ss.SpanContext.TraceFlags()),
|
2020-12-11 07:15:44 +02:00
|
|
|
StartTime: ss.StartTime.UnixNano() / 1000,
|
|
|
|
Duration: ss.EndTime.Sub(ss.StartTime).Nanoseconds() / 1000,
|
2019-09-09 23:59:39 +02:00
|
|
|
Tags: tags,
|
|
|
|
Logs: logs,
|
2019-10-07 18:40:36 +02:00
|
|
|
References: refs,
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func keyValueToTag(keyValue attribute.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() {
|
2021-02-18 19:59:37 +02:00
|
|
|
case attribute.STRING:
|
2020-05-14 01:06:03 +02:00
|
|
|
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,
|
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
case attribute.BOOL:
|
2020-05-14 01:06:03 +02:00
|
|
|
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,
|
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
case attribute.INT64:
|
2020-05-14 01:06:03 +02:00
|
|
|
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,
|
|
|
|
}
|
2021-02-18 19:59:37 +02:00
|
|
|
case attribute.FLOAT64:
|
2020-05-14 01:06:03 +02:00
|
|
|
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,
|
|
|
|
}
|
2021-03-08 19:32:02 +02:00
|
|
|
case attribute.ARRAY:
|
|
|
|
json, _ := json.Marshal(keyValue.Value.AsArray())
|
|
|
|
a := (string)(json)
|
|
|
|
tag = &gen.Tag{
|
|
|
|
Key: string(keyValue.Key),
|
|
|
|
VStr: &a,
|
|
|
|
VType: gen.TagType_STRING,
|
|
|
|
}
|
2019-09-25 22:22:33 +02:00
|
|
|
}
|
|
|
|
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-09-09 19:19:03 +02:00
|
|
|
flush(e)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:04:46 +02:00
|
|
|
func (e *Exporter) upload(spans []*export.SpanSnapshot) error {
|
|
|
|
batchList := jaegerBatchList(spans, e.defaultServiceName, e.resourceFromProcess)
|
|
|
|
for _, batch := range batchList {
|
|
|
|
err := e.uploader.upload(batch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// jaegerBatchList transforms a slice of SpanSnapshot into a slice of jaeger
|
|
|
|
// Batch.
|
|
|
|
func jaegerBatchList(ssl []*export.SpanSnapshot, defaultServiceName string, resourceFromProcess *resource.Resource) []*gen.Batch {
|
|
|
|
if len(ssl) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
batchDict := make(map[attribute.Distinct]*gen.Batch)
|
|
|
|
|
|
|
|
for _, ss := range ssl {
|
|
|
|
if ss == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newResource := ss.Resource
|
|
|
|
if resourceFromProcess != nil {
|
|
|
|
// The value from process will overwrite the value from span's resources
|
|
|
|
newResource = resource.Merge(ss.Resource, resourceFromProcess)
|
|
|
|
}
|
|
|
|
resourceKey := newResource.Equivalent()
|
|
|
|
batch, bOK := batchDict[resourceKey]
|
|
|
|
if !bOK {
|
|
|
|
batch = &gen.Batch{
|
|
|
|
Process: process(newResource, defaultServiceName),
|
|
|
|
Spans: []*gen.Span{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batch.Spans = append(batch.Spans, spanSnapshotToThrift(ss))
|
|
|
|
batchDict[resourceKey] = batch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the categorized map into a slice
|
|
|
|
batchList := make([]*gen.Batch, 0, len(batchDict))
|
|
|
|
for _, batch := range batchDict {
|
|
|
|
batchList = append(batchList, batch)
|
|
|
|
}
|
|
|
|
return batchList
|
|
|
|
}
|
|
|
|
|
|
|
|
// process transforms an OTel Resource into a jaeger Process.
|
|
|
|
func process(res *resource.Resource, defaultServiceName string) *gen.Process {
|
|
|
|
var process gen.Process
|
|
|
|
|
|
|
|
var serviceName attribute.KeyValue
|
|
|
|
if res != nil {
|
|
|
|
for iter := res.Iter(); iter.Next(); {
|
|
|
|
if iter.Attribute().Key == semconv.ServiceNameKey {
|
|
|
|
serviceName = iter.Attribute()
|
|
|
|
// Don't convert service.name into tag.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tag := keyValueToTag(iter.Attribute()); tag != nil {
|
|
|
|
process.Tags = append(process.Tags, tag)
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:04:46 +02:00
|
|
|
// If no service.name is contained in a Span's Resource,
|
|
|
|
// that field MUST be populated from the default Resource.
|
|
|
|
if serviceName.Value.AsString() == "" {
|
|
|
|
serviceName = semconv.ServiceVersionKey.String(defaultServiceName)
|
|
|
|
}
|
|
|
|
process.ServiceName = serviceName.Value.AsString()
|
|
|
|
|
|
|
|
return &process
|
|
|
|
}
|
|
|
|
|
|
|
|
func processToResource(process Process) *resource.Resource {
|
|
|
|
var attrs []attribute.KeyValue
|
|
|
|
if process.ServiceName != "" {
|
|
|
|
attrs = append(attrs, semconv.ServiceNameKey.String(process.ServiceName))
|
|
|
|
}
|
|
|
|
attrs = append(attrs, process.Tags...)
|
|
|
|
|
|
|
|
if len(attrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return resource.NewWithAttributes(attrs...)
|
2019-09-09 23:59:39 +02:00
|
|
|
}
|