Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

NewSummary functions with Custom Objectives #56

Merged
merged 5 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boilerplate/lyft/golang_support_tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/lyft/boilerplate
go 1.13

require (
github.com/alvaroloes/enumer v1.1.2
github.com/golangci/golangci-lint v1.22.2
github.com/lyft/flytestdlib v0.2.31
github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5
github.com/alvaroloes/enumer v1.1.2
)

replace github.com/vektra/mockery => github.com/enghabu/mockery v0.0.0-20191009061720-9d0c8670c2f0
3 changes: 3 additions & 0 deletions boilerplate/lyft/golang_support_tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aws/aws-sdk-go v1.25.16 h1:k7Fy6T/uNuLX6zuayU/TJoP7yMgGcJSkZpF7QVjwYpA=
github.com/aws/aws-sdk-go v1.25.16/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
Expand Down Expand Up @@ -262,6 +263,7 @@ github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.5.0 h1:5BakdOZdtKJ1FFk6QdL8iSGrMWsXgchNJcrnarjbmJQ=
Expand Down Expand Up @@ -468,6 +470,7 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190521203540-521d6ed310dd/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
Expand Down
25 changes: 22 additions & 3 deletions promutils/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (s Timer) Stop() float64 {
return scaled
}

type SummaryOptions struct {
bnsblue marked this conversation as resolved.
Show resolved Hide resolved
Objectives map[float64]float64
}

// A Scope represents a prefix in Prometheus. It is nestable, thus every metric that is published does not need to
// provide a prefix, but just the name of the metric. As long as the Scope is used to create a new instance of the metric
// The prefix (or scope) is automatically set.
Expand All @@ -133,6 +137,11 @@ type Scope interface {
NewSummary(name, description string) (prometheus.Summary, error)
MustNewSummary(name, description string) prometheus.Summary

// Creates new prometheus.Summary metric with custom options, such as a custom set of objectives (i.e., target quantiles).
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewSummaryWithOptions(name, description string, options SummaryOptions) (prometheus.Summary, error)
MustNewSummaryWithOptions(name, description string, options SummaryOptions) prometheus.Summary

// Creates new prometheus.SummaryVec metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewSummaryVec(name, description string, labelNames ...string) (*prometheus.SummaryVec, error)
Expand Down Expand Up @@ -225,19 +234,29 @@ func (m metricsScope) MustNewGaugeVec(name, description string, labelNames ...st
}

func (m metricsScope) NewSummary(name, description string) (prometheus.Summary, error) {
return m.NewSummaryWithOptions(name, description, SummaryOptions{Objectives: defaultObjectives})
}

func (m metricsScope) MustNewSummary(name, description string) prometheus.Summary {
s, err := m.NewSummary(name, description)
panicIfError(err)
return s
}

func (m metricsScope) NewSummaryWithOptions(name, description string, options SummaryOptions) (prometheus.Summary, error) {
s := prometheus.NewSummary(
prometheus.SummaryOpts{
Name: m.NewScopedMetricName(name),
Help: description,
Objectives: defaultObjectives,
Objectives: options.Objectives,
},
)

return s, prometheus.Register(s)
}

func (m metricsScope) MustNewSummary(name, description string) prometheus.Summary {
s, err := m.NewSummary(name, description)
func (m metricsScope) MustNewSummaryWithOptions(name, description string, options SummaryOptions) prometheus.Summary {
s, err := m.NewSummaryWithOptions(name, description, options)
panicIfError(err)
return s
}
Expand Down
3 changes: 3 additions & 0 deletions promutils/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func TestMetricsScope(t *testing.T) {
t.Run("Summary", func(t *testing.T) {
m := s.MustNewSummary("xs", description)
assert.Equal(t, `Desc{fqName: "test:xs", help: "some x", constLabels: {}, variableLabels: []}`, m.Desc().String())
mco, err := s.NewSummaryWithOptions("xsco", description, SummaryOptions{Objectives: map[float64]float64{0.5: 0.05, 1.0: 0.0}})
assert.Nil(t, err)
assert.Equal(t, `Desc{fqName: "test:xsco", help: "some x", constLabels: {}, variableLabels: []}`, mco.Desc().String())
mv := s.MustNewSummaryVec("xsv", description)
assert.NotNil(t, mv)
assert.Panics(t, func() {
Expand Down