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

Commit

Permalink
0.9.4: Add Must* methods (#8)
Browse files Browse the repository at this point in the history
This release adds methods starting with `Must` that panic instead of throwing an error.
  • Loading branch information
Janos Pasztor authored Dec 28, 2020
1 parent 8bfe5e9 commit befe233
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.9.4: Add `Must*` methods

This release adds methods starting with `Must` that panic instead of throwing an error.

## 0.9.3: Custom label support

Each of the metric methods now allow adding extra labels:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ testCounter, err := m.CreateCounterGeo(
testCounter.Increment(net.ParseIP("127.0.0.1"))
```

If you need a metric that can be decremented or set directly you can use the `Gauge` type instead.
If you need a metric that can be decremented or set directly you can use the `Gauge` type instead. Each `Create*` method also has a counterpart named `MustCreate*`, which panics instead of returning an error.

### Custom labels

Expand Down
20 changes: 18 additions & 2 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,33 @@ type Collector interface {
// CreateCounter creates a monotonic (increasing) counter with the specified name and help text.
CreateCounter(name string, unit string, help string) (SimpleCounter, error)

// MustCreateCounter creates a monotonic (increasing) counter with the specified name and help text. Panics if an
// error occurs.
MustCreateCounter(name string, unit string, help string) SimpleCounter

// CreateCounterGeo creates a monotonic (increasing) counter that is labeled with the country from the GeoIP lookup
// with the specified name and help text.
// with the specified name and help text.
CreateCounterGeo(name string, unit string, help string) (SimpleGeoCounter, error)

// MustCreateCounterGeo creates a monotonic (increasing) counter that is labeled with the country from the GeoIP
// lookup with the specified name and help text. Panics if an error occurs.
MustCreateCounterGeo(name string, unit string, help string) SimpleGeoCounter

// CreateGauge creates a freely modifiable numeric gauge with the specified name and help text.
CreateGauge(name string, unit string, help string) (SimpleGauge, error)

// MustCreateGauge creates a freely modifiable numeric gauge with the specified name and help text. Panics if an
// error occurs.
MustCreateGauge(name string, unit string, help string) SimpleGauge

// CreateGaugeGeo creates a freely modifiable numeric gauge that is labeled with the country from the GeoIP lookup
// with the specified name and help text.
// with the specified name and help text.
CreateGaugeGeo(name string, unit string, help string) (SimpleGeoGauge, error)

// MustCreateGaugeGeo creates a freely modifiable numeric gauge that is labeled with the country from the GeoIP
// lookup with the specified name and help text. Panics if an error occurs.
MustCreateGaugeGeo(name string, unit string, help string) SimpleGeoGauge

// ListMetrics returns a list of metrics metadata stored in the collector.
ListMetrics() []Metric

Expand Down
32 changes: 32 additions & 0 deletions collector_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ type collector struct {
values map[string]*metricValue
}

func (c *collector) MustCreateCounter(name string, unit string, help string) SimpleCounter {
counter, err := c.CreateCounter(name, unit, help)
if err != nil {
panic(err)
}
return counter
}

func (c *collector) MustCreateCounterGeo(name string, unit string, help string) SimpleGeoCounter {
counter, err := c.CreateCounterGeo(name, unit, help)
if err != nil {
panic(err)
}
return counter
}

func (c *collector) MustCreateGauge(name string, unit string, help string) SimpleGauge {
gauge, err := c.CreateGauge(name, unit, help)
if err != nil {
panic(err)
}
return gauge
}

func (c *collector) MustCreateGaugeGeo(name string, unit string, help string) SimpleGeoGauge {
gauge, err := c.CreateGaugeGeo(name, unit, help)
if err != nil {
panic(err)
}
return gauge
}

type metricValue struct {
metricValueMap map[string]*MetricValue
metricValues []*MetricValue
Expand Down

0 comments on commit befe233

Please sign in to comment.