Skip to content

Commit

Permalink
Updated metrics for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Jan 1, 2021
1 parent 42f150b commit 5eb1658
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
23 changes: 14 additions & 9 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ import (

// Metrics provides a mechanism for defining measurements
// and emitting data, which may be time-series based and include
// tags/dimensions (which are indexed) and metrics (which are not)
// tags (which are indexed and can be used for grouping) and
// metrics (which are not, and can be aggregated).
type Metrics interface {
// Define a measurement with metric definitions and optional tag fields
NewMeasurement(string, string, ...Field) (Measurement, error)

// Field creates a field or nil if invalid
Field(string, ...interface{}) Field
// Emit tags and metrics for a named measurement, omitting timestamp
Emit(string, []Field, ...interface{}) error

// Emit metrics for a named measurement, omitting timestamp
Emit(string, ...interface{}) error

// EmitTS emits metrics for a named measurement, with defined timestamp
EmitTS(string, time.Time, ...interface{}) error
// EmitTS emits tags and metrics for a named measurement, with defined timestamp
EmitTS(string, time.Time, []Field, ...interface{}) error

// Measurements returns array of all defined measurements
Measurements() []Measurement

// Return some standard tags
// Field creates a field or nil if invalid
Field(string, ...interface{}) Field

// HostTag returns a field with the current hostname
HostTag() Field

// UserTag returns a field with the current username
UserTag() Field

// EnvTag returns a field with the value of an environment variable
EnvTag(string) Field
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/metrics/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (this *measurement) Set(name string, value interface{}) error {
}
}

func (this *measurement) Clone(ts time.Time, values ...interface{}) (*measurement, error) {
func (this *measurement) Clone(ts time.Time, tags []gopi.Field, values ...interface{}) (*measurement, error) {
// Check correct number of arguments
if len(values) != len(this.metrics) {
return nil, gopi.ErrBadParameter.WithPrefix("Clone")
Expand All @@ -137,6 +137,11 @@ func (this *measurement) Clone(ts time.Time, values ...interface{}) (*measuremen
that.ts = ts
that.fields = make(map[string]gopi.Field, len(this.fields))

// Index new tags and use them instead of defaults
for _, tag := range tags {
fmt.Println("TODO: Clone", tag)
}

// Clone tags
that.tags = make([]gopi.Field, len(this.tags))
for i, value := range this.tags {
Expand Down
8 changes: 4 additions & 4 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ func (this *metrics) Field(name string, value ...interface{}) gopi.Field {
}

// Emit metrics for a named measurement, omitting timestamp
func (this *metrics) Emit(name string, values ...interface{}) error {
return this.EmitTS(name, time.Time{}, values...)
func (this *metrics) Emit(name string, tags []gopi.Field, values ...interface{}) error {
return this.EmitTS(name, time.Time{}, tags, values...)
}

// EmitTS emits metrics for a named measurement, with defined timestamp
// will retry if the channel is temporarily full
func (this *metrics) EmitTS(name string, ts time.Time, values ...interface{}) error {
func (this *metrics) EmitTS(name string, ts time.Time, tags []gopi.Field, values ...interface{}) error {
this.RWMutex.RLock()
defer this.RWMutex.RUnlock()

// Clone measurement
if m, exists := this.m[name]; exists == false {
return gopi.ErrBadParameter.WithPrefix("Emit", name)
} else if m, err := m.Clone(ts, values...); err != nil {
} else if m, err := m.Clone(ts, tags, values...); err != nil {
return err
} else {
// Unreliable emit mechanism but don't block
Expand Down

0 comments on commit 5eb1658

Please sign in to comment.