This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
prometheus.go
449 lines (401 loc) · 12.7 KB
/
prometheus.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2017 Istio Authors
//
// 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.
// nolint: lll
//go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -a mixer/adapter/prometheus/config/config.proto -x "-n prometheus -t metric"
// Package prometheus publishes metric values collected by Mixer for
// ingestion by prometheus.
package prometheus
import (
"context"
"crypto/sha1"
"fmt"
"hash/fnv"
"math"
"sort"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
multierror "github.com/hashicorp/go-multierror"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"istio.io/istio/mixer/adapter/prometheus/config"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/mixer/template/metric"
"istio.io/istio/pkg/cache"
)
type (
// cinfo is a collector, its kind and the sha
// of config that produced the collector.
// sha is used to confirm a cache hit.
cinfo struct {
c prometheus.Collector
sha [sha1.Size]byte
kind config.Params_MetricInfo_Kind
sortedLabels []string
}
builder struct {
// maps instance_name to collector.
metrics map[string]*cinfo
registry *prometheus.Registry
srv Server
cfg *config.Params
}
handler struct {
srv Server
metrics map[string]*cinfo
labelsCache cache.ExpiringCache
}
cacheEntry struct {
vec prometheus.Collector
labels prometheus.Labels
}
)
var (
charReplacer = strings.NewReplacer("/", "_", ".", "_", " ", "_", "-", "")
_ metric.HandlerBuilder = &builder{}
_ metric.Handler = &handler{}
)
const (
defaultNS = "istio"
)
// GetInfoWithAddr returns the Info associated with this adapter.
func GetInfoWithAddr(addr string) (adapter.Info, Server) {
singletonBuilder := &builder{
srv: newServer(addr),
}
singletonBuilder.clearState()
return adapter.Info{
Name: "prometheus",
Impl: "istio.io/istio/mixer/adapter/prometheus",
Description: "Publishes prometheus metrics",
SupportedTemplates: []string{
metric.TemplateName,
},
NewBuilder: func() adapter.HandlerBuilder { return singletonBuilder },
DefaultConfig: &config.Params{},
}, singletonBuilder.srv
}
// GetInfo returns the Info associated with this adapter.
func GetInfo() adapter.Info {
// prometheus uses a singleton http port, so we make the
// builder itself a singleton, when defaultAddr become configurable
// srv will be a map[string]server
ii, _ := GetInfoWithAddr(defaultAddr)
return ii
}
func (b *builder) clearState() {
b.registry = prometheus.NewPedanticRegistry()
b.metrics = make(map[string]*cinfo)
}
func (b *builder) SetMetricTypes(map[string]*metric.Type) {}
func (b *builder) SetAdapterConfig(cfg adapter.Config) { b.cfg = cfg.(*config.Params) }
func (b *builder) Validate() (ce *adapter.ConfigErrors) {
if b.cfg.MetricsExpirationPolicy != nil {
if b.cfg.MetricsExpirationPolicy.MetricsExpiryDuration <= 0 {
ce = ce.Appendf("metricsExpiryDuration",
"metricsExpiryDuration %v is invalid, must be > 0", b.cfg.MetricsExpirationPolicy.MetricsExpiryDuration)
}
}
return
}
func (b *builder) Build(ctx context.Context, env adapter.Env) (adapter.Handler, error) {
cfg := b.cfg
var metricErr *multierror.Error
// newMetrics collects new metric configuration
newMetrics := make([]*config.Params_MetricInfo, 0, len(cfg.Metrics))
// Check for metric redefinition.
// If a metric is redefined clear the metric registry and metric map.
// prometheus client panics on metric redefinition.
// Addition and removal of metrics is ok.
var cl *cinfo
for _, m := range cfg.Metrics {
// metric is not found in the current metric table
// should be added.
if cl = b.metrics[m.InstanceName]; cl == nil {
newMetrics = append(newMetrics, m)
continue
}
// metric collector found and sha matches
// safe to reuse the existing collector.
if cl.sha == computeSha(m, env.Logger()) {
continue
}
// sha does not match.
env.Logger().Warningf("Metric %s redefined. Reloading adapter.", m.Name)
b.clearState()
// consider all configured metrics to be "new".
newMetrics = cfg.Metrics
break
}
env.Logger().Debugf("%d new metrics defined", len(newMetrics))
var err error
for _, m := range newMetrics {
ns := defaultNS
if len(m.Namespace) > 0 {
ns = safeName(m.Namespace)
}
mname := m.InstanceName
if len(m.Name) != 0 {
mname = m.Name
}
ci := &cinfo{kind: m.Kind, sha: computeSha(m, env.Logger())}
ci.sortedLabels = make([]string, len(m.LabelNames))
copy(ci.sortedLabels, m.LabelNames)
sort.Strings(ci.sortedLabels)
switch m.Kind {
case config.GAUGE:
// TODO: make prometheus use the keys of metric.Type.Dimensions as the label names and remove from config.
ci.c, err = registerOrGet(b.registry, newGaugeVec(ns, mname, m.Description, m.LabelNames))
if err != nil {
metricErr = multierror.Append(metricErr, fmt.Errorf("could not register metric: %v", err))
continue
}
b.metrics[m.InstanceName] = ci
case config.COUNTER:
ci.c, err = registerOrGet(b.registry, newCounterVec(ns, mname, m.Description, m.LabelNames))
if err != nil {
metricErr = multierror.Append(metricErr, fmt.Errorf("could not register metric: %v", err))
continue
}
b.metrics[m.InstanceName] = ci
case config.DISTRIBUTION:
ci.c, err = registerOrGet(b.registry, newHistogramVec(ns, mname, m.Description, m.LabelNames, m.Buckets))
if err != nil {
metricErr = multierror.Append(metricErr, fmt.Errorf("could not register metric: %v", err))
continue
}
b.metrics[m.InstanceName] = ci
default:
metricErr = multierror.Append(metricErr, fmt.Errorf("unknown metric kind (%d); could not register metric %v", m.Kind, m))
}
}
// We want best-effort on metrics generation. It is important to log the failures, however,
// to help capture any breakages that may be hidden.
opts := promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
ErrorLog: &promLogger{logger: env.Logger()},
}
if err := b.srv.Start(env, promhttp.HandlerFor(b.registry, opts)); err != nil {
return nil, err
}
var expiryCache cache.ExpiringCache
if cfg.MetricsExpirationPolicy != nil {
checkDuration := cfg.MetricsExpirationPolicy.ExpiryCheckIntervalDuration
if checkDuration == 0 {
checkDuration = cfg.MetricsExpirationPolicy.MetricsExpiryDuration / 2
}
expiryCache = cache.NewTTLWithCallback(
cfg.MetricsExpirationPolicy.MetricsExpiryDuration,
checkDuration,
deleteOldMetrics)
}
return &handler{b.srv, b.metrics, expiryCache}, metricErr.ErrorOrNil()
}
func (h *handler) HandleMetric(_ context.Context, vals []*metric.Instance) error {
var result *multierror.Error
for _, val := range vals {
ci := h.metrics[val.Name]
if ci == nil {
result = multierror.Append(result, fmt.Errorf("could not find metric info from adapter config for %s", val.Name))
continue
}
collector := ci.c
switch ci.kind {
case config.GAUGE:
vec := collector.(*prometheus.GaugeVec)
amt, err := promValue(val.Value)
if err != nil {
result = multierror.Append(result, fmt.Errorf("could not get value for metric %s: %v", val.Name, err))
continue
}
pl := promLabels(val.Dimensions)
if h.labelsCache != nil {
h.labelsCache.Set(key(val.Name, "gauge", pl, ci.sortedLabels), &cacheEntry{vec, pl})
}
vec.With(pl).Set(amt)
case config.COUNTER:
vec := collector.(*prometheus.CounterVec)
amt, err := promValue(val.Value)
if err != nil {
result = multierror.Append(result, fmt.Errorf("could not get value for metric %s: %v", val.Name, err))
continue
}
pl := promLabels(val.Dimensions)
if h.labelsCache != nil {
h.labelsCache.Set(key(val.Name, "counter", pl, ci.sortedLabels), &cacheEntry{vec, pl})
}
vec.With(pl).Add(amt)
case config.DISTRIBUTION:
vec := collector.(*prometheus.HistogramVec)
amt, err := promValue(val.Value)
if err != nil {
result = multierror.Append(result, fmt.Errorf("could not get value for metric %s: %v", val.Name, err))
continue
}
pl := promLabels(val.Dimensions)
if h.labelsCache != nil {
h.labelsCache.Set(key(val.Name, "distribution", pl, ci.sortedLabels), &cacheEntry{vec, pl})
}
vec.With(pl).Observe(amt)
}
}
return result.ErrorOrNil()
}
func key(name, kind string, labels prometheus.Labels, sortedLabelKeys []string) uint64 {
buf := pool.GetBuffer()
buf.WriteString(name + ":" + kind)
for _, k := range sortedLabelKeys {
buf.WriteString(k + "=" + labels[k] + ";") // nolint: gas
}
h := fnv.New64()
buf.WriteTo(h)
pool.PutBuffer(buf)
return h.Sum64()
}
func deleteOldMetrics(key, value interface{}) {
if entry, ok := value.(*cacheEntry); ok {
switch v := entry.vec.(type) {
case *prometheus.CounterVec:
v.Delete(entry.labels)
case *prometheus.GaugeVec:
v.Delete(entry.labels)
case *prometheus.HistogramVec:
v.Delete(entry.labels)
}
}
}
func (h *handler) Close() error { return h.srv.Close() }
func newCounterVec(namespace, name, desc string, labels []string) *prometheus.CounterVec {
if desc == "" {
desc = name
}
c := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: safeName(name),
Help: desc,
},
labelNames(labels),
)
return c
}
func newGaugeVec(namespace, name, desc string, labels []string) *prometheus.GaugeVec {
if desc == "" {
desc = name
}
c := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: safeName(name),
Help: desc,
},
labelNames(labels),
)
return c
}
func newHistogramVec(namespace, name, desc string, labels []string, bucketDef *config.Params_MetricInfo_BucketsDefinition) *prometheus.HistogramVec {
if desc == "" {
desc = name
}
c := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: safeName(name),
Help: desc,
Buckets: buckets(bucketDef),
},
labelNames(labels),
)
return c
}
func buckets(def *config.Params_MetricInfo_BucketsDefinition) []float64 {
switch def.GetDefinition().(type) {
case *config.Params_MetricInfo_BucketsDefinition_ExplicitBuckets:
b := def.GetExplicitBuckets()
return b.Bounds
case *config.Params_MetricInfo_BucketsDefinition_LinearBuckets:
lb := def.GetLinearBuckets()
return prometheus.LinearBuckets(lb.Offset, lb.Width, int(lb.NumFiniteBuckets+1))
case *config.Params_MetricInfo_BucketsDefinition_ExponentialBuckets:
eb := def.GetExponentialBuckets()
return prometheus.ExponentialBuckets(eb.Scale, eb.GrowthFactor, int(eb.NumFiniteBuckets+1))
default:
return prometheus.DefBuckets
}
}
func labelNames(m []string) []string {
out := make([]string, len(m))
for i, s := range m {
out[i] = safeName(s)
}
return out
}
// borrowed from prometheus.RegisterOrGet. However, that method is
// targeted for removal soon(tm). So, we duplicate that functionality here
// to maintain it long-term, as we have a use case for the convenience.
func registerOrGet(registry *prometheus.Registry, c prometheus.Collector) (prometheus.Collector, error) {
if err := registry.Register(c); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
return are.ExistingCollector, nil
}
return nil, err
}
return c, nil
}
func safeName(n string) string {
s := strings.TrimPrefix(n, "/")
return charReplacer.Replace(s)
}
func promValue(val interface{}) (float64, error) {
switch i := val.(type) {
case float64:
return i, nil
case int64:
return float64(i), nil
case time.Duration:
// TODO: what is the right thing here?
// use seconds for now, as we get fractional values...
return i.Seconds(), nil
case string:
f, err := strconv.ParseFloat(i, 64)
if err != nil {
return math.NaN(), err
}
return f, err
default:
return math.NaN(), fmt.Errorf("could not extract numeric value for %v", val)
}
}
func promLabels(l map[string]interface{}) prometheus.Labels {
labels := make(prometheus.Labels, len(l))
for i, label := range l {
labels[i] = adapter.Stringify(label)
}
return labels
}
func computeSha(m proto.Marshaler, log adapter.Logger) [sha1.Size]byte {
ba, err := m.Marshal()
if err != nil {
log.Warningf("Unable to encode %v", err)
}
return sha1.Sum(ba)
}
type promLogger struct {
logger adapter.Logger
}
func (pl *promLogger) Println(v ...interface{}) {
pl.logger.Errorf("Prometheus handler error: %s", fmt.Sprintln(v...)) // nolint: gas
}