forked from census-instrumentation/opencensus-go
/
prometheus.go
297 lines (255 loc) · 7.78 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
// Copyright 2017, OpenCensus 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.
// Package prometheus contains the Prometheus exporters for
// Stackdriver Monitoring.
//
// Please note that this exporter is currently work in progress and not complete.
package prometheus // import "go.opencensus.io/exporter/prometheus"
import (
"bytes"
"errors"
"fmt"
"log"
"net/http"
"sync"
"go.opencensus.io/internal"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
defaultNamespace = "opencensus"
)
// Exporter exports stats to Prometheus, users need
// to register the exporter as an http.Handler to be
// able to export.
type Exporter struct {
opts Options
g prometheus.Gatherer
c *collector
handler http.Handler
}
// Options contains options for configuring the exporter.
type Options struct {
Namespace string
OnError func(err error)
}
var (
newExporterOnce sync.Once
errSingletonExporter = errors.New("expecting only one exporter per instance")
)
// NewExporter returns an exporter that exports stats to Prometheus.
// Only one exporter should exist per instance
func NewExporter(o Options) (*Exporter, error) {
var err = errSingletonExporter
var exporter *Exporter
newExporterOnce.Do(func() {
exporter, err = newExporter(o)
})
return exporter, err
}
func newExporter(o Options) (*Exporter, error) {
if o.Namespace == "" {
o.Namespace = defaultNamespace
}
reg := prometheus.NewRegistry()
collector := newCollector(o, reg)
e := &Exporter{
opts: o,
g: reg,
c: collector,
handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
}
return e, nil
}
var _ http.Handler = (*Exporter)(nil)
var _ view.Exporter = (*Exporter)(nil)
func (c *collector) registerViews(views ...*view.View) {
count := 0
for _, view := range views {
sig := viewSignature(c.opts.Namespace, view)
c.registeredViewsMu.Lock()
_, ok := c.registeredViews[sig]
c.registeredViewsMu.Unlock()
if !ok {
desc := prometheus.NewDesc(
viewName(c.opts.Namespace, view),
view.Description(),
tagKeysToLabels(view.TagKeys()),
nil,
)
c.registeredViewsMu.Lock()
c.registeredViews[sig] = desc
c.registeredViewsMu.Unlock()
count++
}
}
if count == 0 {
return
}
c.reg.Unregister(c)
if err := c.reg.Register(c); err != nil {
c.opts.onError(fmt.Errorf("cannot register the collector: %v", err))
}
}
func (o *Options) onError(err error) {
if o.OnError != nil {
o.OnError(err)
} else {
log.Printf("Failed to export to Prometheus: %v", err)
}
}
// ExportView exports to the Prometheus if view data has one or more rows.
// Each OpenCensus AggregationData will be converted to
// corresponding Prometheus Metric: SumData will be converted
// to Untyped Metric, CountData will be Counter Metric,
// DistributionData will be Histogram Metric, and MeanData
// will be Summary Metric. Please note the Summary Metric from
// MeanData does not have any quantiles.
func (e *Exporter) ExportView(vd *view.Data) {
if len(vd.Rows) == 0 {
return
}
e.c.addViewData(vd)
}
// ServeHTTP serves the Prometheus endpoint.
func (e *Exporter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.handler.ServeHTTP(w, r)
}
// collector implements prometheus.Collector
type collector struct {
opts Options
mu sync.Mutex // mu guards all the fields.
// reg helps collector register views dynamically.
reg *prometheus.Registry
// viewData are accumulated and atomically
// appended to on every Export invocation, from
// stats. These views are cleared out when
// Collect is invoked and the cycle is repeated.
viewData map[string]*view.Data
registeredViewsMu sync.Mutex
// registeredViews maps a view to a prometheus desc.
registeredViews map[string]*prometheus.Desc
}
func (c *collector) addViewData(vd *view.Data) {
c.registerViews(vd.View)
sig := viewSignature(c.opts.Namespace, vd.View)
c.mu.Lock()
c.viewData[sig] = vd
c.mu.Unlock()
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
c.registeredViewsMu.Lock()
registered := make(map[string]*prometheus.Desc)
for k, desc := range c.registeredViews {
registered[k] = desc
}
c.registeredViewsMu.Unlock()
for _, desc := range registered {
ch <- desc
}
}
// Collect fetches the statistics from OpenCensus
// and delivers them as Prometheus Metrics.
// Collect is invoked everytime a prometheus.Gatherer is run
// for example when the HTTP endpoint is invoked by Prometheus.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
// We need a copy of all the view data up until this point.
viewData := c.cloneViewData()
for _, vd := range viewData {
sig := viewSignature(c.opts.Namespace, vd.View)
c.registeredViewsMu.Lock()
desc := c.registeredViews[sig]
c.registeredViewsMu.Unlock()
for _, row := range vd.Rows {
metric, err := c.toMetric(desc, vd.View, row)
if err != nil {
c.opts.onError(err)
} else {
ch <- metric
}
}
}
}
func (c *collector) toMetric(desc *prometheus.Desc, v *view.View, row *view.Row) (prometheus.Metric, error) {
switch agg := v.Aggregation().(type) {
case view.CountAggregation:
data := row.Data.(*view.CountData)
return prometheus.NewConstMetric(desc, prometheus.CounterValue, float64(*data), tagValues(row.Tags)...)
case view.DistributionAggregation:
data := row.Data.(*view.DistributionData)
points := make(map[float64]uint64)
for i, b := range agg {
points[b] = uint64(data.CountPerBucket[i])
}
return prometheus.NewConstHistogram(desc, uint64(data.Count), data.Sum(), points, tagValues(row.Tags)...)
case view.MeanAggregation:
data := row.Data.(*view.MeanData)
return prometheus.NewConstSummary(desc, uint64(data.Count), data.Sum(), make(map[float64]float64), tagValues(row.Tags)...)
case view.SumAggregation:
data := row.Data.(*view.SumData)
return prometheus.NewConstMetric(desc, prometheus.UntypedValue, float64(*data), tagValues(row.Tags)...)
default:
return nil, fmt.Errorf("aggregation %T is not yet supported", v.Aggregation())
}
}
func tagKeysToLabels(keys []tag.Key) (labels []string) {
for _, key := range keys {
labels = append(labels, internal.Sanitize(key.Name()))
}
return labels
}
func tagsToLabels(tags []tag.Tag) []string {
var names []string
for _, tag := range tags {
names = append(names, internal.Sanitize(tag.Key.Name()))
}
return names
}
func newCollector(opts Options, registrar *prometheus.Registry) *collector {
return &collector{
reg: registrar,
opts: opts,
registeredViews: make(map[string]*prometheus.Desc),
viewData: make(map[string]*view.Data),
}
}
func tagValues(t []tag.Tag) []string {
var values []string
for _, t := range t {
values = append(values, t.Value)
}
return values
}
func viewName(namespace string, v *view.View) string {
return namespace + "_" + internal.Sanitize(v.Name())
}
func viewSignature(namespace string, v *view.View) string {
var buf bytes.Buffer
buf.WriteString(viewName(namespace, v))
for _, k := range v.TagKeys() {
buf.WriteString("-" + k.Name())
}
return buf.String()
}
func (c *collector) cloneViewData() map[string]*view.Data {
c.mu.Lock()
defer c.mu.Unlock()
viewDataCopy := make(map[string]*view.Data)
for sig, viewData := range c.viewData {
viewDataCopy[sig] = viewData
}
return viewDataCopy
}