1// +build tracer_static,tracer_static_lightstep
2
3package impl
4
5import (
6	"context"
7	"fmt"
8	"io"
9
10	lightstep "github.com/lightstep/lightstep-tracer-go"
11	opentracing "github.com/opentracing/opentracing-go"
12	log "github.com/sirupsen/logrus"
13)
14
15type lightstepCloser struct {
16	tracer lightstep.Tracer
17}
18
19func (c *lightstepCloser) Close() error {
20	lightstep.Close(context.Background(), c.tracer)
21	return nil
22}
23
24var lightstepConfigMapper = map[string]func(traceCfg *lightstep.Options, value string) error{
25	"service_name": func(options *lightstep.Options, value string) error {
26		options.Tags[lightstep.ComponentNameKey] = value
27		return nil
28	},
29	"access_token": func(options *lightstep.Options, value string) error {
30		options.AccessToken = value
31		return nil
32	},
33}
34
35func lightstepTracerFactory(config map[string]string) (opentracing.Tracer, io.Closer, error) {
36	options := lightstep.Options{
37		Tags: map[string]interface{}{},
38	}
39
40	// Convert the configuration map into a jaeger configuration
41	for k, v := range config {
42		mapper := lightstepConfigMapper[k]
43		if k == keyStrictConnectionParsing {
44			continue
45		}
46
47		if mapper != nil {
48			err := mapper(&options, v)
49			if err != nil {
50				return nil, nil, err
51			}
52		} else {
53			if config[keyStrictConnectionParsing] != "" {
54				return nil, nil, fmt.Errorf("lightstep tracer: invalid option: %s: %w", k, ErrConfiguration)
55			}
56
57			log.Printf("lightstep tracer: warning: ignoring unknown configuration option: %s", k)
58		}
59	}
60
61	if options.AccessToken == "" {
62		return nil, nil, fmt.Errorf("failed to parse access_token from config: %q: %w", config, ErrConfiguration)
63	}
64
65	tracer := lightstep.NewTracer(options)
66	if tracer == nil {
67		return nil, nil, fmt.Errorf("lightstep tracer: unable to create tracer, review log messages: %w", ErrConfiguration)
68	}
69
70	return tracer, &lightstepCloser{tracer}, nil
71}
72
73func init() { // nolint:gochecknoinits
74	registerTracer("lightstep", lightstepTracerFactory)
75}
76