Passed
Push — master ( 91093b...5624dc )
by Tolga
01:21 queued 13s
created

meterexporters.NewOTLP   C

Complexity

Conditions 10

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nop 5
dl 0
loc 48
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like meterexporters.NewOTLP often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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