Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/tracing/promreporter.go
2496 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package tracing
6
7
import (
8
"time"
9
10
"github.com/prometheus/client_golang/prometheus"
11
jaeger "github.com/uber/jaeger-client-go"
12
)
13
14
// PromReporter reports Jaeger span durations to Prometheus
15
type PromReporter struct {
16
Operations map[string]SpanMetricMapping
17
18
metrics map[string]prometheus.Histogram
19
}
20
21
// SpanMetricMapping defines how a span is to be reported to Prometheus
22
type SpanMetricMapping struct {
23
Name string
24
Help string
25
Buckets []float64
26
}
27
28
// RegisterMetrics registers all metrics created throug the mapping
29
func (r *PromReporter) RegisterMetrics() error {
30
r.metrics = make(map[string]prometheus.Histogram)
31
for k, m := range r.Operations {
32
metric := prometheus.NewHistogram(prometheus.HistogramOpts{
33
Name: m.Name,
34
Help: m.Help,
35
Buckets: m.Buckets,
36
})
37
err := prometheus.Register(metric)
38
if err != nil {
39
return err
40
}
41
42
r.metrics[k] = metric
43
}
44
return nil
45
}
46
47
// Report reports a span as Prometheus metric
48
func (r *PromReporter) Report(span *jaeger.Span) {
49
metric, ok := r.metrics[span.OperationName()]
50
if !ok {
51
return
52
}
53
54
metric.Observe(float64(span.Duration() / time.Millisecond))
55
}
56
57
// Close implements Close() method of Reporter by closing each underlying reporter.
58
func (r *PromReporter) Close() {
59
60
}
61
62