forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
95 lines (83 loc) · 3.13 KB
/
builder.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metrics
import (
"errors"
"flag"
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
xkit "github.com/uber/jaeger-lib/metrics/go-kit"
kitexpvar "github.com/uber/jaeger-lib/metrics/go-kit/expvar"
kitprom "github.com/uber/jaeger-lib/metrics/go-kit/prometheus"
"github.com/uber/jaeger/examples/hotrod/pkg/httpexpvar"
"github.com/uber/jaeger-lib/metrics"
)
const (
metricsBackend = "metrics-backend"
metricsHTTPRoute = "metrics-http-route"
defaultMetricsBackend = "expvar"
defaultMetricsRoute = "/debug/vars"
)
var errUnknownBackend = errors.New("unknown metrics backend specified")
// Builder provides command line options to configure metrics backend used by Jaeger executables.
type Builder struct {
Backend string
HTTPRoute string // endpoint name to expose metrics, e.g. for scraping
handler http.Handler
}
// AddFlags adds flags for Builder.
func AddFlags(flags *flag.FlagSet) {
flags.String(
metricsBackend,
defaultMetricsBackend,
fmt.Sprintf("Defines which metrics backend to use for metrics reporting: %s, prometheus, none",
defaultMetricsBackend))
flags.String(
metricsHTTPRoute,
defaultMetricsRoute,
"Defines the route of HTTP endpoint for metrics backends that support scraping")
}
// InitFromViper initializes Builder with properties retrieved from Viper.
func (b *Builder) InitFromViper(v *viper.Viper) {
b.Backend = v.GetString(metricsBackend)
b.HTTPRoute = v.GetString(metricsHTTPRoute)
}
// CreateMetricsFactory creates a metrics factory based on the configured type of the backend.
// If the metrics backend supports HTTP endpoint for scraping, it is stored in the builder and
// can be later added by RegisterHandler function.
func (b *Builder) CreateMetricsFactory(namespace string) (metrics.Factory, error) {
if b.Backend == "prometheus" {
metricsFactory := xkit.Wrap(namespace, kitprom.NewFactory("", "", nil))
b.handler = promhttp.Handler()
return metricsFactory, nil
}
if b.Backend == "expvar" {
metricsFactory := xkit.Wrap(namespace, kitexpvar.NewFactory(10))
// TODO register official expvar handler once we upgrade to Go 1.8
b.handler = http.HandlerFunc(httpexpvar.Handler)
return metricsFactory, nil
}
if b.Backend == "none" || b.Backend == "" {
return metrics.NullFactory, nil
}
return nil, errUnknownBackend
}
// RegisterHandler adds an endpoint to the mux if the metrics backend supports it.
func (b *Builder) RegisterHandler(mux *http.ServeMux) {
if b.handler != nil && b.HTTPRoute != "" {
mux.Handle(b.HTTPRoute, b.handler)
}
}