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

tracerexporters.NewOTLP   C

Complexity

Conditions 11

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 30
nop 5
dl 0
loc 50
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like tracerexporters.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 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