Skip to content

Commit

Permalink
NewSummary functions with Custom Objectives (flyteorg#56)
Browse files Browse the repository at this point in the history
* upgrade dependency, and add new summary creation function that takes custom objectives

* fix test

* refactor the newly added functions

* add what objectives mean to the comments

* add comments to SummaryOptions
  • Loading branch information
bnsblue authored Jan 27, 2020
1 parent 5e10d2d commit 0eabe40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion flytestdlib/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 flytestdlib/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
28 changes: 25 additions & 3 deletions flytestdlib/promutils/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ func (s Timer) Stop() float64 {
return scaled
}

// A SummaryOptions represents a set of options that can be supplied when creating a new prometheus summary metric
type SummaryOptions struct {
// An Objectives defines the quantile rank estimates with their respective absolute errors.
// Refer to https://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts for details
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 +140,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 +237,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 flytestdlib/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

0 comments on commit 0eabe40

Please sign in to comment.