This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
summary.go
64 lines (52 loc) · 1.91 KB
/
summary.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
package labeled
import (
"context"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus"
)
// Summary represents a summary labeled with values from the context. See labeled.SetMetricsKeys for information about
// how to configure that.
type Summary struct {
*prometheus.SummaryVec
prometheus.Summary
labels []contextutils.Key
}
// Observe adds a single observation to the summary.
func (s Summary) Observe(ctx context.Context, v float64) {
summary, err := s.SummaryVec.GetMetricWith(contextutils.Values(ctx, s.labels...))
if err != nil {
panic(err.Error())
}
summary.Observe(v)
if s.Summary != nil {
s.Summary.Observe(v)
}
}
// NewSummary creates a new labeled summary. Label keys must be set before instantiating. If the unlabeled option is
// given, this object will also instantiate and emit another summary with the given name with an _unlabeled suffix.
// See labeled.SetMetricsKeys for information about how to configure that.
func NewSummary(name, description string, scope promutils.Scope, opts ...MetricOption) Summary {
if len(metricKeys) == 0 {
panic(ErrNeverSet)
}
s := Summary{}
for _, opt := range opts {
if _, emitUnlabeledMetric := opt.(EmitUnlabeledMetricOption); emitUnlabeledMetric {
s.Summary = scope.MustNewSummary(GetUnlabeledMetricName(name), description)
} else if additionalLabels, casted := opt.(AdditionalLabelsOption); casted {
// compute unique labels
labelSet := sets.NewString(metricStringKeys...)
labelSet.Insert(additionalLabels.Labels...)
labels := labelSet.List()
s.SummaryVec = scope.MustNewSummaryVec(name, description, labels...)
s.labels = contextutils.MetricKeysFromStrings(labels)
}
}
if s.SummaryVec == nil {
s.SummaryVec = scope.MustNewSummaryVec(name, description, metricStringKeys...)
s.labels = metricKeys
}
return s
}