Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions commontypes/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commontypes

type Metrics interface {
// NewMetricVec creates a new MetricVec with the provided name and help
// and partitioned by the given label names
// The name and label names may contain ASCII letters, numbers, as well as underscores.
// An error is returned if either the name or some label names is of invalid format.
NewMetricVec(name string, help string, labelNames ...string) (MetricVec, error)
}

// MetricVec must be thread safe
type MetricVec interface {
// GetMetricWith returns a Metric for the given MetricVec with the given labels map.
// If that label map is assessed for the first time, a new Metric is created.
// Label values may contain any Unicode character (UTF-8 encoded).
// An error is returned if the number and names of the labels are
// inconsistent with those of the variable labels of the MetricVec
// or if some labels' value is empty
// or if some labels' value is not a valid UTF-8 string.
// Label names are not validated since they are already validated when the MetricVec is constructed.
// The implementation of this method must guarantee that different invocations
// on the same MetricVec with the same label map returns the same Metric.
// I.e. for
// m1, err1 := metricVec.MetricWithLabels(L1)
// m2, err2 := metricVec.MetricWithLabels(L2)
// s.t. reflect.DeepEqual(L1, L2)
// then (err1 == nil && err2 == nil) implies m1 == m2
GetMetricWith(labels map[string]string) (Metric, error)
}

// Metric must be thread safe
type Metric interface {
// Set sets the Metric to an arbitrary value.
Set(float64)
// Inc increments the Metric by 1. Use Add to increment it by arbitrary
// values.
Inc()
// Dec decrements the Metric by 1. Use Sub to decrement it by arbitrary
// values.
Dec()
// Add adds the given value to the Metric. (The value can be negative,
// resulting in a decrease of the Metric.)
Add(float64)
// Sub subtracts the given value from the Metric. (The value can be
// negative, resulting in an increase of the Metric.)
Sub(float64)
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/allegro/bigcache v1.2.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
Expand All @@ -43,6 +43,7 @@ require (
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.1.5 // indirect
Expand Down Expand Up @@ -139,7 +140,7 @@ require (
go.uber.org/zap v1.16.0 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW
github.com/cespare/cp v1.1.1 h1:nCb6ZLdB7NRaqsm91JtQTAme2SKJzXVsdPIPkyJr1MU=
github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -232,8 +233,9 @@ github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgR
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF2I64qf5Fh8Aa83Q/dnOafMYV0OMwjA=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
Expand Down Expand Up @@ -1163,8 +1165,9 @@ golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 h1:uCLL3g5wH2xjxVREVuAbP9JM5PPKjRbXKRa6IBjkzmU=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
41 changes: 41 additions & 0 deletions internal/loghelper/close_log_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,44 @@ func CloseLogError(closer io.Closer, logger commontypes.Logger, msg string) {
})
}
}

// Closes closer if success is false. If an error occurs, it is logged at WARN level together with
// msg
//
// Useful for deferred closing in a constructor like this:
//
// func newFoo(logger commontypes.Logger) (*Foo, err) {
// success := false
//
// bar, err := newBar()
// if err != nil {
// return nil, err
// }
// defer loghelper.CloseLogErrorUnlessSuccess(&success, bar, logger, "failed to close bar in failed newFoo")
//
// baz, err := newBaz()
// if err != nil {
// return nil, err
// }
// defer loghelper.CloseLogErrorUnlessSuccess(&success, baz, logger, "failed to close baz in failed newFoo")
//
// success = true
// return &Foo{
// bar,
// baz
// }
// }
func CloseLogErrorUnlessSuccess(success *bool, closer io.Closer, logger commontypes.Logger, msg string) {
if success != nil && *success {
// no failure, return early
return
}
if success == nil {
logger.Debug("CloseLogErrorUnlessSuccess: got nil success value. this should not happen, assuming it means we did not succeed", nil)
}
if err := closer.Close(); err != nil {
logger.Warn(msg, commontypes.LogFields{
"error": err,
})
}
}
Loading