Passed
Pull Request — master (#1490)
by Tolga
02:43
created

pkg/telemetry/logexporters/otlp.go   A

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 34
dl 0
loc 57
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C logexporters.NewOTLP 0 45 9
1
package logexporters
2
3
import (
4
	"context"
5
	"fmt"
6
7
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs"
8
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogsgrpc"
9
	"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp"
10
)
11
12
// NewOTLP - Creates new OTLP exporter based on protocol.
13
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (*otlplogs.Exporter, error) {
14
	switch protocol {
15
	case "http":
16
		options := []otlplogshttp.Option{
17
			otlplogshttp.WithCompression(otlplogshttp.GzipCompression),
18
			otlplogshttp.WithEndpoint(endpoint),
19
		}
20
21
		if urlpath != "" {
22
			options = append(options, otlplogshttp.WithURLPath(urlpath))
23
		}
24
25
		if insecure {
26
			options = append(options, otlplogshttp.WithInsecure())
27
		}
28
29
		exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
30
			otlplogshttp.NewClient(options...),
31
		))
32
		if err != nil {
33
			return nil, err
34
		}
35
36
		return exporter, nil
37
38
	case "grpc":
39
		options := []otlplogsgrpc.Option{
40
			otlplogsgrpc.WithEndpoint(endpoint),
41
		}
42
43
		if insecure {
44
			options = append(options, otlplogsgrpc.WithInsecure())
45
		}
46
47
		exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
48
			otlplogsgrpc.NewClient(options...),
49
		))
50
		if err != nil {
51
			return nil, err
52
		}
53
54
		return exporter, nil
55
56
	default:
57
		return nil, fmt.Errorf("unsupported protocol: %s", protocol)
58
	}
59
}
60