This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd.go
196 lines (173 loc) · 6.1 KB
/
statsd.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
// 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.
//go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -f mixer/adapter/statsd/config/config.proto
// Package statsd provides an adapter that implements the metrics template to
// serialize generated metric values to a statsd backend.
package statsd
import (
"context"
"fmt"
"io/ioutil"
"text/template"
"time"
"github.com/cactus/go-statsd-client/statsd"
multierror "github.com/hashicorp/go-multierror"
"istio.io/istio/mixer/adapter/statsd/config"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/mixer/template/metric"
)
const (
defaultFlushBytes = 512
)
type (
info struct {
mtype config.Params_MetricInfo_Type
tmpl *template.Template
}
handler struct {
rate float32
client statsd.Statter
templates map[string]info // metric name -> template
}
)
func (h *handler) HandleMetric(_ context.Context, values []*metric.Instance) error {
var result *multierror.Error
for _, v := range values {
if err := h.record(v); err != nil {
result = multierror.Append(result, err)
}
}
return result.ErrorOrNil()
}
func (h *handler) record(value *metric.Instance) error {
mname := value.Name
t, found := h.templates[mname]
if !found {
return fmt.Errorf("no info for metric named %s", value.Name)
}
if t.tmpl != nil {
buf := pool.GetBuffer()
// We don't check the error here because Execute should only fail when the template is invalid; since
// we check that the templates are parsable in ValidateConfig and further check that they can be executed
// with the metric's labels in NewMetricsAspect, this should never fail.
_ = t.tmpl.Execute(buf, value.Dimensions)
mname = buf.String()
pool.PutBuffer(buf)
}
switch t.mtype {
case config.GAUGE:
v, ok := value.Value.(int64)
if !ok {
return fmt.Errorf("could not record counter '%s' expected int value, got %v", mname, value.Value)
}
return h.client.Gauge(mname, v, h.rate)
case config.COUNTER:
v, ok := value.Value.(int64)
if !ok {
return fmt.Errorf("could not record counter '%s' expected int value, got %v", mname, value.Value)
}
return h.client.Inc(mname, v, h.rate)
case config.DISTRIBUTION:
// TODO: figure out how to program histograms via config.*
// updates
v, ok := value.Value.(time.Duration)
if ok {
return h.client.TimingDuration(mname, v, h.rate)
}
// TODO: figure out support for non-duration distributions.
vint, ok := value.Value.(int64)
if ok {
return h.client.Inc(mname, vint, h.rate)
}
return fmt.Errorf("could not record distribution '%s'; expected int or duration, got %v", mname, value.Value)
default:
return fmt.Errorf("unknown metric type '%v' for metric: %s", t.mtype, value.Name)
}
}
func (h *handler) Close() error { return h.client.Close() }
////////////////// Config //////////////////////////
// GetInfo returns the Info associated with this adapter implementation.
func GetInfo() adapter.Info {
return adapter.Info{
Name: "statsd",
Impl: "istio.io/istio/mixer/adapter/statsd",
Description: "Produces statsd metrics",
SupportedTemplates: []string{
metric.TemplateName,
},
DefaultConfig: &config.Params{
Address: "localhost:8125",
Prefix: "",
FlushDuration: 300 * time.Millisecond,
FlushBytes: 512,
SamplingRate: 1.0,
},
NewBuilder: func() adapter.HandlerBuilder { return &builder{} },
}
}
type builder struct {
adapterConfig *config.Params
metricTypes map[string]*metric.Type
}
func (b *builder) SetMetricTypes(types map[string]*metric.Type) { b.metricTypes = types }
func (b *builder) SetAdapterConfig(cfg adapter.Config) { b.adapterConfig = cfg.(*config.Params) }
func (b *builder) Validate() (ce *adapter.ConfigErrors) {
ac := b.adapterConfig
if ac.FlushDuration < 0 {
ce = ce.Appendf("flushDuration", "flush duration must be >= 0")
}
if ac.FlushBytes < 0 {
ce = ce.Appendf("flushBytes", "flush bytes must be >= 0")
}
if ac.SamplingRate < 0 {
ce = ce.Appendf("samplingRate", "sampling rate must be >= 0")
}
for metricName, s := range ac.Metrics {
if _, err := template.New(metricName).Parse(s.NameTemplate); err != nil {
ce = ce.Appendf("metricNameTemplateStrings", "failed to parse template '%s' for metric '%s': %v", s, metricName, err)
}
}
return
}
func (b *builder) Build(context context.Context, env adapter.Env) (adapter.Handler, error) {
ac := b.adapterConfig
flushBytes := int(ac.FlushBytes)
if flushBytes <= 0 {
env.Logger().Infof("Got FlushBytes of '%d', defaulting to '%d'", flushBytes, defaultFlushBytes)
// the statsd impl we use defaults to 1432 byte UDP packets when flushBytes <= 0; we want to default to 512 so we check ourselves.
flushBytes = defaultFlushBytes
}
client, _ := statsd.NewBufferedClient(ac.Address, ac.Prefix, ac.FlushDuration, flushBytes)
templates := make(map[string]info)
for metricName, s := range ac.Metrics {
def, found := b.metricTypes[metricName]
if !found {
env.Logger().Infof("template registered for nonexistent metric '%s'", metricName)
continue // we don't have a metric that corresponds to this template, skip processing it
}
var t *template.Template
if s.NameTemplate != "" {
t, _ = template.New(metricName).Parse(s.NameTemplate)
if err := t.Execute(ioutil.Discard, def.Dimensions); err != nil {
env.Logger().Warningf(
"skipping custom statsd metric name for metric '%s', could not satisfy template '%s' with labels '%v': %v",
metricName, s, def.Dimensions, err)
continue
}
}
templates[metricName] = info{mtype: s.Type, tmpl: t}
}
return &handler{ac.SamplingRate, client, templates}, nil
}