Skip to content

Commit

Permalink
core: remove labels
Browse files Browse the repository at this point in the history
  • Loading branch information
zeim839 committed Apr 19, 2024
1 parent cf40c67 commit b54ffda
Show file tree
Hide file tree
Showing 35 changed files with 430 additions and 1,072 deletions.
35 changes: 4 additions & 31 deletions appoptics/appoptics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func translateTimerAttributes(d time.Duration) (attrs map[string]interface{}) {
// measurements API.
type Reporter struct {
Token string
Tags metrics.Labels
Interval time.Duration
Registry metrics.Registry
Percentiles []float64 // percentiles to report on histogram metrics
Expand All @@ -38,7 +37,7 @@ type Reporter struct {

// NewReporter creates a new reporter.
func NewReporter(registry metrics.Registry, interval time.Duration, token string,
tags metrics.Labels, percentiles []float64, timeUnits time.Duration,
percentiles []float64, timeUnits time.Duration,
prefix string, whitelistedRuntimeMetrics []string,
measurementsURI string) *Reporter {

Expand All @@ -53,7 +52,7 @@ func NewReporter(registry metrics.Registry, interval time.Duration, token string
}
}

return &Reporter{token, tags, interval, registry, percentiles, prefix,
return &Reporter{token, interval, registry, percentiles, prefix,
whitelist, translateTimerAttributes(timeUnits),
int64(interval / time.Second), measurementsURI}
}
Expand All @@ -64,9 +63,9 @@ func NewReporter(registry metrics.Registry, interval time.Duration, token string
// only a subset of the runtime.* metrics that are gathered by go-metrics.
// Passing an empty slice disables uploads for all runtime.* metrics.
func AppOptics(registry metrics.Registry, interval time.Duration, token string,
tags metrics.Labels, percentiles []float64, timeUnits time.Duration,
percentiles []float64, timeUnits time.Duration,
prefix string, whitelistedRuntimeMetrics []string, measurementsURI string) {
NewReporter(registry, interval, token, tags, percentiles,
NewReporter(registry, interval, token, percentiles,
timeUnits, prefix, whitelistedRuntimeMetrics,
measurementsURI).Run()
}
Expand Down Expand Up @@ -114,17 +113,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
name = rep.Prefix + name
measurement := Measurement{}
measurement[Period] = rep.Interval.Seconds()

mergedTags := metrics.Labels{}
copyTags := func(tags metrics.Labels) {
for tagName, tagValue := range tags {
mergedTags[tagName] = tagValue
}
measurement[Tags] = mergedTags
}
// Copy to prevent mutating Reporter's global tags.
copyTags(rep.Tags)

switch m := metric.(type) {
case metrics.Counter:
if m.Count() <= 0 {
Expand All @@ -137,17 +125,14 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
DisplayUnitsShort: OperationsShort,
DisplayMin: "0",
}
copyTags(m.Labels())
batch.Measurements = append(batch.Measurements, measurement)
case metrics.Gauge:
measurement[Name] = name
measurement[Value] = float64(m.Value())
copyTags(m.Labels())
batch.Measurements = append(batch.Measurements, measurement)
case metrics.GaugeFloat64:
measurement[Name] = name
measurement[Value] = m.Value()
copyTags(m.Labels())
batch.Measurements = append(batch.Measurements, measurement)
case metrics.Histogram:
s := m.Snapshot().Sample()
Expand All @@ -168,12 +153,10 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
measurement[Min] = float64(s.Min())
measurement[Sum] = float64(s.Sum())
measurement[StdDev] = float64(s.StdDev())
copyTags(m.Labels())
measurements[0] = measurement
for i, p := range rep.Percentiles {
measurements[i+i] = Measurement{
Name: fmt.Sprintf("%s.%.2f", measurement[Name], p),
Tags: mergedTags,
Value: s.Percentile(p),
Period: measurement[Period],
}
Expand All @@ -183,12 +166,10 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
s := m.Snapshot()
measurement[Name] = name
measurement[Value] = float64(s.Count())
copyTags(s.Labels())
batch.Measurements = append(batch.Measurements, measurement)
batch.Measurements = append(batch.Measurements,
Measurement{
Name: fmt.Sprintf("%s.%s", name, "1min"),
Tags: mergedTags,
Value: s.Rate1(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand All @@ -199,7 +180,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
},
Measurement{
Name: fmt.Sprintf("%s.%s", name, "5min"),
Tags: mergedTags,
Value: s.Rate5(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand All @@ -210,7 +190,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
},
Measurement{
Name: fmt.Sprintf("%s.%s", name, "15min"),
Tags: mergedTags,
Value: s.Rate15(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand All @@ -224,7 +203,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
s := m.Snapshot()
measurement[Name] = name
measurement[Value] = float64(s.Count())
copyTags(s.Labels())
batch.Measurements = append(batch.Measurements, measurement)
if m.Count() <= 0 {
return
Expand All @@ -233,7 +211,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
measurements := make([]Measurement, histogramMeasurementCount)
measurements[0] = Measurement{
Name: appOpticsName,
Tags: mergedTags,
Count: uint64(s.Count()),
Sum: s.Mean() * float64(s.Count()),
Max: float64(s.Max()),
Expand All @@ -245,7 +222,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
for i, p := range rep.Percentiles {
measurements[i+1] = Measurement{
Name: fmt.Sprintf("%s.timer.%2.0f", name, p*100),
Tags: mergedTags,
Value: m.Percentile(p),
Period: int64(rep.Interval.Seconds()),
Attributes: rep.TimerAttributes,
Expand All @@ -255,7 +231,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
batch.Measurements = append(batch.Measurements,
Measurement{
Name: fmt.Sprintf("%s.%s", name, "rate.1min"),
Tags: mergedTags,
Value: s.Rate1(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand All @@ -266,7 +241,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
},
Measurement{
Name: fmt.Sprintf("%s.%s", name, "rate.5min"),
Tags: mergedTags,
Value: s.Rate5(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand All @@ -277,7 +251,6 @@ func (rep *Reporter) BuildRequest(now time.Time, r metrics.Registry) (batch Batc
},
Measurement{
Name: fmt.Sprintf("%s.%s", name, "rate.15min"),
Tags: mergedTags,
Value: s.Rate15(),
Period: int64(rep.Interval.Seconds()),
Attributes: map[string]interface{}{
Expand Down
6 changes: 3 additions & 3 deletions appoptics/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

func ExampleAppOptics() {
metrics.GetOrRegisterCounter("myCounter", nil, metrics.Labels{"foo": "bar"})
metrics.GetOrRegisterMeter("myMeter", nil, nil)
metrics.GetOrRegisterCounter("myCounter", nil)
metrics.GetOrRegisterMeter("myMeter", nil)

go AppOptics(metrics.DefaultRegistry, time.Second, "token",
metrics.Labels{"hostname": "localhost"}, []float64{0.5, 0.75, 0.95, 0.99},
[]float64{0.5, 0.75, 0.95, 0.99},
time.Millisecond, "myservice.", nil, DefaultMeasurementsURI)
}
51 changes: 6 additions & 45 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,38 @@ type Counter interface {
Dec(int64)
Inc(int64)
Snapshot() Counter
Labels() Labels
WithLabels(Labels) Counter
}

// GetOrRegisterCounter returns an existing Counter or constructs and registers
// a new StandardCounter.
func GetOrRegisterCounter(name string, r Registry, labels Labels) Counter {
func GetOrRegisterCounter(name string, r Registry) Counter {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, func() Counter {
return NewCounter(labels)
}).(Counter)
return r.GetOrRegister(name, NewCounter).(Counter)
}

// NewRegisteredCounter constructs and registers a new StandardCounter.
func NewRegisteredCounter(name string, r Registry, labels Labels) Counter {
func NewRegisteredCounter(name string, r Registry) Counter {
if nil == r {
r = DefaultRegistry
}
c := NewCounter(labels)
c := NewCounter()
r.Register(name, c)
return c
}

// NewCounter constructs a new StandardCounter.
func NewCounter(labels Labels) Counter {
func NewCounter() Counter {
if UseNilMetrics {
return NilCounter{}
}
return &StandardCounter{labels: deepCopyLabels(labels)}
return &StandardCounter{}
}

// CounterSnapshot is a read-only copy of another Counter.
type CounterSnapshot struct {
count int64
labels Labels
}

// Clear panics.
Expand All @@ -69,21 +64,6 @@ func (CounterSnapshot) Inc(int64) {
// Snapshot returns the snapshot.
func (c CounterSnapshot) Snapshot() Counter { return c }

// Labels returns a copy of the snapshot's labels.
func (c CounterSnapshot) Labels() Labels { return deepCopyLabels(c.labels) }

// WithLabels returns the snapshot with the given labels appended.
func (c CounterSnapshot) WithLabels(labels Labels) Counter {
newLabels := c.labels
for k, v := range labels {
newLabels[k] = v
}
return CounterSnapshot{
count: c.Count(),
labels: newLabels,
}
}

// NilCounter is a no-op Counter.
type NilCounter struct{}

Expand All @@ -102,17 +82,10 @@ func (NilCounter) Inc(i int64) {}
// Snapshot is a no-op.
func (NilCounter) Snapshot() Counter { return NilCounter{} }

// Labels is a no-op.
func (NilCounter) Labels() Labels { return Labels{} }

// WithLabels is a no-op.
func (NilCounter) WithLabels(Labels) Counter { return NilCounter{} }

// StandardCounter is the standard implementation of a Counter and uses the
// sync/atomic package to manage a single int64 value.
type StandardCounter struct {
count atomic.Int64
labels Labels
}

// Clear sets the counter to zero.
Expand All @@ -139,17 +112,5 @@ func (c *StandardCounter) Inc(i int64) {
func (c *StandardCounter) Snapshot() Counter {
return CounterSnapshot{
count: c.Count(),
labels: c.Labels(),
}
}

// Labels returns a deep copy of the counter's labels.
func (c *StandardCounter) Labels() Labels {
return deepCopyLabels(c.labels)
}

// WithLabels returns a snapshot of the counter with the given labels appended
// to its current list of labels.
func (c *StandardCounter) WithLabels(labels Labels) Counter {
return c.Snapshot().WithLabels(labels)
}
Loading

0 comments on commit b54ffda

Please sign in to comment.