1
|
|
|
package tracerexporters |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
|
7
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" |
8
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
9
|
|
|
"go.opentelemetry.io/otel/sdk/trace" |
10
|
|
|
"google.golang.org/grpc/credentials" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// NewOTLP - Creates new OTLP exporter based on protocol. |
14
|
|
|
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (trace.SpanExporter, error) { |
15
|
|
|
switch protocol { |
16
|
|
|
case "http": |
17
|
|
|
opts := []otlptracehttp.Option{ |
18
|
|
|
otlptracehttp.WithEndpoint(endpoint), |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if len(headers) > 0 { |
22
|
|
|
opts = append(opts, otlptracehttp.WithHeaders(headers)) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if urlpath != "" { |
26
|
|
|
opts = append(opts, otlptracehttp.WithURLPath(urlpath)) |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if insecure { |
30
|
|
|
opts = append(opts, otlptracehttp.WithInsecure()) |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
exporter, err := otlptracehttp.New(context.Background(), opts...) |
34
|
|
|
if err != nil { |
35
|
|
|
return nil, err |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return exporter, nil |
39
|
|
|
|
40
|
|
|
case "grpc": |
41
|
|
|
opts := []otlptracegrpc.Option{ |
42
|
|
|
otlptracegrpc.WithEndpoint(endpoint), |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if len(headers) > 0 { |
46
|
|
|
opts = append(opts, otlptracegrpc.WithHeaders(headers)) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if insecure { |
50
|
|
|
opts = append(opts, otlptracegrpc.WithInsecure()) |
51
|
|
|
} else { |
52
|
|
|
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
exporter, err := otlptracegrpc.New(context.Background(), opts...) |
56
|
|
|
if err != nil { |
57
|
|
|
return nil, err |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return exporter, nil |
61
|
|
|
|
62
|
|
|
default: |
63
|
|
|
return nil, fmt.Errorf("unsupported protocol: %s", protocol) |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|