1// Copyright 2018, OpenCensus Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package ocagent
16
17import (
18	"time"
19
20	"google.golang.org/grpc/credentials"
21)
22
23const (
24	DefaultAgentPort uint16 = 55678
25	DefaultAgentHost string = "localhost"
26)
27
28type ExporterOption interface {
29	withExporter(e *Exporter)
30}
31
32type insecureGrpcConnection int
33
34var _ ExporterOption = (*insecureGrpcConnection)(nil)
35
36func (igc *insecureGrpcConnection) withExporter(e *Exporter) {
37	e.canDialInsecure = true
38}
39
40// WithInsecure disables client transport security for the exporter's gRPC connection
41// just like grpc.WithInsecure() https://godoc.org/google.golang.org/grpc#WithInsecure
42// does. Note, by default, client security is required unless WithInsecure is used.
43func WithInsecure() ExporterOption { return new(insecureGrpcConnection) }
44
45type addressSetter string
46
47func (as addressSetter) withExporter(e *Exporter) {
48	e.agentAddress = string(as)
49}
50
51var _ ExporterOption = (*addressSetter)(nil)
52
53// WithAddress allows one to set the address that the exporter will
54// connect to the agent on. If unset, it will instead try to use
55// connect to DefaultAgentHost:DefaultAgentPort
56func WithAddress(addr string) ExporterOption {
57	return addressSetter(addr)
58}
59
60type serviceNameSetter string
61
62func (sns serviceNameSetter) withExporter(e *Exporter) {
63	e.serviceName = string(sns)
64}
65
66var _ ExporterOption = (*serviceNameSetter)(nil)
67
68// WithServiceName allows one to set/override the service name
69// that the exporter will report to the agent.
70func WithServiceName(serviceName string) ExporterOption {
71	return serviceNameSetter(serviceName)
72}
73
74type reconnectionPeriod time.Duration
75
76func (rp reconnectionPeriod) withExporter(e *Exporter) {
77	e.reconnectionPeriod = time.Duration(rp)
78}
79
80func WithReconnectionPeriod(rp time.Duration) ExporterOption {
81	return reconnectionPeriod(rp)
82}
83
84type compressorSetter string
85
86func (c compressorSetter) withExporter(e *Exporter) {
87	e.compressor = string(c)
88}
89
90// UseCompressor will set the compressor for the gRPC client to use when sending requests.
91// It is the responsibility of the caller to ensure that the compressor set has been registered
92// with google.golang.org/grpc/encoding. This can be done by encoding.RegisterCompressor. Some
93// compressors auto-register on import, such as gzip, which can be registered by calling
94// `import _ "google.golang.org/grpc/encoding/gzip"`
95func UseCompressor(compressorName string) ExporterOption {
96	return compressorSetter(compressorName)
97}
98
99type headerSetter map[string]string
100
101func (h headerSetter) withExporter(e *Exporter) {
102	e.headers = map[string]string(h)
103}
104
105// WithHeaders will send the provided headers when the gRPC stream connection
106// is instantiated
107func WithHeaders(headers map[string]string) ExporterOption {
108	return headerSetter(headers)
109}
110
111type clientCredentials struct {
112	credentials.TransportCredentials
113}
114
115var _ ExporterOption = (*clientCredentials)(nil)
116
117// WithTLSCredentials allows the connection to use TLS credentials
118// when talking to the server. It takes in grpc.TransportCredentials instead
119// of say a Certificate file or a tls.Certificate, because the retrieving
120// these credentials can be done in many ways e.g. plain file, in code tls.Config
121// or by certificate rotation, so it is up to the caller to decide what to use.
122func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption {
123	return &clientCredentials{TransportCredentials: creds}
124}
125
126func (cc *clientCredentials) withExporter(e *Exporter) {
127	e.clientTransportCredentials = cc.TransportCredentials
128}
129