1// Copyright (c) 2017 Uber Technologies, Inc.
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 remote
16
17import (
18	"time"
19
20	"github.com/uber/jaeger-client-go"
21)
22
23const (
24	defaultMaxValueLength  = 2048
25	defaultRefreshInterval = time.Minute
26	defaultHostPort        = "localhost:5778"
27)
28
29// Option is a function that sets some option on the RestrictionManager
30type Option func(options *options)
31
32// Options is a factory for all available options
33var Options options
34
35type options struct {
36	denyBaggageOnInitializationFailure bool
37	metrics                            *jaeger.Metrics
38	logger                             jaeger.Logger
39	hostPort                           string
40	refreshInterval                    time.Duration
41}
42
43// DenyBaggageOnInitializationFailure creates an Option that determines the startup failure mode of RestrictionManager.
44// If DenyBaggageOnInitializationFailure is true, RestrictionManager will not allow any baggage to be written until baggage
45// restrictions have been retrieved from agent.
46// If DenyBaggageOnInitializationFailure is false, RestrictionManager will allow any baggage to be written until baggage
47// restrictions have been retrieved from agent.
48func (options) DenyBaggageOnInitializationFailure(b bool) Option {
49	return func(o *options) {
50		o.denyBaggageOnInitializationFailure = b
51	}
52}
53
54// Metrics creates an Option that initializes Metrics on the RestrictionManager, which is used to emit statistics.
55func (options) Metrics(m *jaeger.Metrics) Option {
56	return func(o *options) {
57		o.metrics = m
58	}
59}
60
61// Logger creates an Option that sets the logger used by the RestrictionManager.
62func (options) Logger(logger jaeger.Logger) Option {
63	return func(o *options) {
64		o.logger = logger
65	}
66}
67
68// HostPort creates an Option that sets the hostPort of the local agent that contains the baggage restrictions.
69func (options) HostPort(hostPort string) Option {
70	return func(o *options) {
71		o.hostPort = hostPort
72	}
73}
74
75// RefreshInterval creates an Option that sets how often the RestrictionManager will poll local agent for
76// the baggage restrictions.
77func (options) RefreshInterval(refreshInterval time.Duration) Option {
78	return func(o *options) {
79		o.refreshInterval = refreshInterval
80	}
81}
82
83func applyOptions(o ...Option) options {
84	opts := options{}
85	for _, option := range o {
86		option(&opts)
87	}
88	if opts.metrics == nil {
89		opts.metrics = jaeger.NewNullMetrics()
90	}
91	if opts.logger == nil {
92		opts.logger = jaeger.NullLogger
93	}
94	if opts.hostPort == "" {
95		opts.hostPort = defaultHostPort
96	}
97	if opts.refreshInterval == 0 {
98		opts.refreshInterval = defaultRefreshInterval
99	}
100	return opts
101}
102