1// Copyright (c) 2018 The Jaeger 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 remote
16
17import (
18	"time"
19
20	"github.com/uber/jaeger-client-go"
21)
22
23const (
24	defaultHostPort        = "localhost:5778"
25	defaultRefreshInterval = time.Second * 5
26)
27
28// Option is a function that sets some option on the Throttler
29type Option func(options *options)
30
31// Options is a factory for all available options
32var Options options
33
34type options struct {
35	metrics                   *jaeger.Metrics
36	logger                    jaeger.Logger
37	hostPort                  string
38	refreshInterval           time.Duration
39	synchronousInitialization bool
40}
41
42// Metrics creates an Option that initializes Metrics on the Throttler, which is used to emit statistics.
43func (options) Metrics(m *jaeger.Metrics) Option {
44	return func(o *options) {
45		o.metrics = m
46	}
47}
48
49// Logger creates an Option that sets the logger used by the Throttler.
50func (options) Logger(logger jaeger.Logger) Option {
51	return func(o *options) {
52		o.logger = logger
53	}
54}
55
56// HostPort creates an Option that sets the hostPort of the local agent that keeps track of credits.
57func (options) HostPort(hostPort string) Option {
58	return func(o *options) {
59		o.hostPort = hostPort
60	}
61}
62
63// RefreshInterval creates an Option that sets how often the Throttler will poll local agent for
64// credits.
65func (options) RefreshInterval(refreshInterval time.Duration) Option {
66	return func(o *options) {
67		o.refreshInterval = refreshInterval
68	}
69}
70
71// SynchronousInitialization creates an Option that determines whether the throttler should synchronously
72// fetch credits from the agent when an operation is seen for the first time. This should be set to true
73// if the client will be used by a short lived service that needs to ensure that credits are fetched upfront
74// such that sampling or throttling occurs.
75func (options) SynchronousInitialization(b bool) Option {
76	return func(o *options) {
77		o.synchronousInitialization = b
78	}
79}
80
81func applyOptions(o ...Option) options {
82	opts := options{}
83	for _, option := range o {
84		option(&opts)
85	}
86	if opts.metrics == nil {
87		opts.metrics = jaeger.NewNullMetrics()
88	}
89	if opts.logger == nil {
90		opts.logger = jaeger.NullLogger
91	}
92	if opts.hostPort == "" {
93		opts.hostPort = defaultHostPort
94	}
95	if opts.refreshInterval == 0 {
96		opts.refreshInterval = defaultRefreshInterval
97	}
98	return opts
99}
100