@@ -2,34 +2,62 @@ package meterexporters
22
33import (
44 "context"
5+ "fmt"
56
67 "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
8+ "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
79 "go.opentelemetry.io/otel/sdk/metric"
10+ "google.golang.org/grpc/credentials"
811)
912
10- // NewOTLP - Creates new OTLP exporter using HTTP protocol.
11- func NewOTLP (endpoint string , insecure bool , urlpath string , headers map [string ]string ) (metric.Exporter , error ) {
12- options := []otlpmetrichttp.Option {
13- otlpmetrichttp .WithCompression (otlpmetrichttp .GzipCompression ),
14- otlpmetrichttp .WithEndpoint (endpoint ),
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 )
1562 }
16-
17- if len (headers ) > 0 {
18- options = append (options , otlpmetrichttp .WithHeaders (headers ))
19- }
20-
21- if urlpath != "" {
22- options = append (options , otlpmetrichttp .WithURLPath (urlpath ))
23- }
24-
25- if insecure {
26- options = append (options , otlpmetrichttp .WithInsecure ())
27- }
28-
29- exporter , err := otlpmetrichttp .New (context .Background (), options ... )
30- if err != nil {
31- return nil , err
32- }
33-
34- return exporter , nil
3563}
0 commit comments