1
|
|
|
package meterexporters |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"fmt" |
6
|
|
|
|
7
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
8
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
9
|
|
|
"go.opentelemetry.io/otel/sdk/metric" |
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) (metric.Exporter, error) { |
15
|
|
|
switch protocol { |
16
|
|
|
case "http": |
17
|
|
|
options := []otlpmetrichttp.Option{ |
18
|
|
|
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression), |
19
|
|
|
otlpmetrichttp.WithEndpoint(endpoint), |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if len(headers) > 0 { |
23
|
|
|
options = append(options, otlpmetrichttp.WithHeaders(headers)) |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if urlpath != "" { |
27
|
|
|
options = append(options, otlpmetrichttp.WithURLPath(urlpath)) |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if insecure { |
31
|
|
|
options = append(options, otlpmetrichttp.WithInsecure()) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
exporter, err := otlpmetrichttp.New(context.Background(), options...) |
35
|
|
|
if err != nil { |
36
|
|
|
return nil, err |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return exporter, nil |
40
|
|
|
|
41
|
|
|
case "grpc": |
42
|
|
|
options := []otlpmetricgrpc.Option{ |
43
|
|
|
otlpmetricgrpc.WithEndpoint(endpoint), |
44
|
|
|
otlpmetricgrpc.WithHeaders(headers), |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if insecure { |
48
|
|
|
options = append(options, otlpmetricgrpc.WithInsecure()) |
49
|
|
|
} else { |
50
|
|
|
options = append(options, otlpmetricgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
exporter, err := otlpmetricgrpc.New(context.Background(), options...) |
54
|
|
|
if err != nil { |
55
|
|
|
return nil, err |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return exporter, nil |
59
|
|
|
|
60
|
|
|
default: |
61
|
|
|
return nil, fmt.Errorf("unsupported protocol: %s", protocol) |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|