1 // Copyright 2018, OpenCensus Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 "google.golang.org/grpc/credentials"
24 DefaultAgentPort uint16 = 55678
25 DefaultAgentHost string = "localhost"
28 type ExporterOption interface {
29 withExporter(e *Exporter)
32 type insecureGrpcConnection int
34 var _ ExporterOption = (*insecureGrpcConnection)(nil)
36 func (igc *insecureGrpcConnection) withExporter(e *Exporter) {
37 e.canDialInsecure = true
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.
43 func WithInsecure() ExporterOption { return new(insecureGrpcConnection) }
45 type addressSetter string
47 func (as addressSetter) withExporter(e *Exporter) {
48 e.agentAddress = string(as)
51 var _ ExporterOption = (*addressSetter)(nil)
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
56 func WithAddress(addr string) ExporterOption {
57 return addressSetter(addr)
60 type serviceNameSetter string
62 func (sns serviceNameSetter) withExporter(e *Exporter) {
63 e.serviceName = string(sns)
66 var _ ExporterOption = (*serviceNameSetter)(nil)
68 // WithServiceName allows one to set/override the service name
69 // that the exporter will report to the agent.
70 func WithServiceName(serviceName string) ExporterOption {
71 return serviceNameSetter(serviceName)
74 type reconnectionPeriod time.Duration
76 func (rp reconnectionPeriod) withExporter(e *Exporter) {
77 e.reconnectionPeriod = time.Duration(rp)
80 func WithReconnectionPeriod(rp time.Duration) ExporterOption {
81 return reconnectionPeriod(rp)
84 type compressorSetter string
86 func (c compressorSetter) withExporter(e *Exporter) {
87 e.compressor = string(c)
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"`
95 func UseCompressor(compressorName string) ExporterOption {
96 return compressorSetter(compressorName)
99 type headerSetter map[string]string
101 func (h headerSetter) withExporter(e *Exporter) {
102 e.headers = map[string]string(h)
105 // WithHeaders will send the provided headers when the gRPC stream connection
107 func WithHeaders(headers map[string]string) ExporterOption {
108 return headerSetter(headers)
111 type clientCredentials struct {
112 credentials.TransportCredentials
115 var _ ExporterOption = (*clientCredentials)(nil)
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.
122 func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption {
123 return &clientCredentials{TransportCredentials: creds}
126 func (cc *clientCredentials) withExporter(e *Exporter) {
127 e.clientTransportCredentials = cc.TransportCredentials