/
config.go
140 lines (121 loc) · 4.53 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package yarpc
import (
"context"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/uber-go/tally"
"go.uber.org/yarpc/api/middleware"
"go.uber.org/yarpc/internal/observability"
"go.uber.org/yarpc/internal/pally"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
// Sleep between pushes to Tally metrics. At some point, we may want this
// to be configurable.
_tallyPushInterval = 500 * time.Millisecond
_packageName = "yarpc"
)
// LoggingConfig describes how logging should be configured.
type LoggingConfig struct {
// Supplies a logger for the dispatcher. By default, no logs are
// emitted.
Zap *zap.Logger
// If supplied, ExtractContext is used to log request-scoped
// information carried on the context (e.g., trace and span IDs).
ContextExtractor func(context.Context) zapcore.Field
}
func (c LoggingConfig) logger(name string) *zap.Logger {
if c.Zap == nil {
return zap.NewNop()
}
return c.Zap.Named(_packageName).With(
// Use a namespace to prevent key collisions with other libraries.
zap.Namespace(_packageName),
zap.String("dispatcher", name),
)
}
func (c LoggingConfig) extractor() observability.ContextExtractor {
if c.ContextExtractor == nil {
return observability.NewNopContextExtractor()
}
return observability.ContextExtractor(c.ContextExtractor)
}
// MetricsConfig describes how telemetry should be configured.
type MetricsConfig struct {
// Tally scope used for pushing to M3 or StatsD-based systems. By
// default, metrics are collected in memory but not pushed.
Tally tally.Scope
}
func (c MetricsConfig) registry(name string, logger *zap.Logger) (*pally.Registry, context.CancelFunc) {
r := pally.NewRegistry(
pally.Labeled(pally.Labels{
"component": _packageName,
"dispatcher": pally.ScrubLabelValue(name),
}),
// Also expose all YARPC metrics via the default Prometheus registry.
pally.Federated(prometheus.DefaultRegisterer),
)
if c.Tally == nil {
return r, func() {}
}
stop, err := r.Push(c.Tally, _tallyPushInterval)
if err != nil {
logger.Error("Failed to start pushing metrics to Tally.", zap.Error(err))
return r, func() {}
}
return r, stop
}
// Config specifies the parameters of a new Dispatcher constructed via
// NewDispatcher.
type Config struct {
// Name of the service. This is the name used by other services when
// making requests to this service.
Name string
// Inbounds define how this service receives incoming requests from other
// services.
//
// This may be nil if this service does not receive any requests.
Inbounds Inbounds
// Outbounds defines how this service makes requests to other services.
//
// This may be nil if this service does not send any requests.
Outbounds Outbounds
// Inbound and Outbound Middleware that will be applied to all incoming
// and outgoing requests respectively.
//
// These may be nil if there is no middleware to apply.
InboundMiddleware InboundMiddleware
OutboundMiddleware OutboundMiddleware
// Tracer is meant to add/record tracing information to a request.
//
// Deprecated: The dispatcher does nothing with this property. Set the
// tracer directly on the transports used to build inbounds and outbounds.
Tracer opentracing.Tracer
// RouterMiddleware is middleware to control how requests are routed.
RouterMiddleware middleware.Router
// Configures logging.
Logging LoggingConfig
// Configures telemetry.
Metrics MetricsConfig
}