Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Update to use options for recording attachments.
Browse files Browse the repository at this point in the history
  • Loading branch information
songy23 committed Apr 22, 2019
1 parent b3ae5af commit 0d5b2d8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
95 changes: 64 additions & 31 deletions stats/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,87 @@ func init() {
}
}

type recordOptions struct {
attachments metricdata.Attachments
mutators []tag.Mutator
measurements []Measurement
}

// WithAttachments applies provided exemplar attachments.
func WithAttachments(attachments metricdata.Attachments) Options {
return func(ro *recordOptions) {
ro.attachments = attachments
}
}

// WithTags applies provided tag mutators.
func WithTags(mutators ...tag.Mutator) Options {
return func(ro *recordOptions) {
ro.mutators = mutators
}
}

// WithMeasurements applies provided measurements.
func WithMeasurements(measurements ...Measurement) Options {
return func(ro *recordOptions) {
ro.measurements = measurements
}
}

// Options apply changes to recordOptions.
type Options func(*recordOptions)

func createRecordOption(ros ...Options) *recordOptions {
o := &recordOptions{}
for _, ro := range ros {
ro(o)
}
return o
}

// Record records one or multiple measurements with the same context at once.
// If there are any tags in the context, measurements will be tagged with them.
func Record(ctx context.Context, ms ...Measurement) {
RecordWithAttachments(ctx, nil, ms...)
RecordWithOptions(ctx, WithMeasurements(ms...))
}

// RecordWithTags records one or multiple measurements at once.
//
// Measurements will be tagged with the tags in the context mutated by the mutators.
// RecordWithTags is useful if you want to record with tag mutations but don't want
// to propagate the mutations in the context.
func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...Measurement) error {
return RecordWithOptions(ctx, WithTags(mutators...), WithMeasurements(ms...))
}

// RecordWithAttachments records measurements and the given exemplar attachments against context.
// RecordWithOptions records measurements from the given options (if any) against context
// and tags and attachments in the options (if any).
// If there are any tags in the context, measurements will be tagged with them.
func RecordWithAttachments(ctx context.Context, attachments metricdata.Attachments, ms ...Measurement) {
func RecordWithOptions(ctx context.Context, ros ...Options) error {
o := createRecordOption(ros...)
if len(o.measurements) == 0 {
return nil
}
recorder := internal.DefaultRecorder
if recorder == nil {
return
}
if len(ms) == 0 {
return
return nil
}
record := false
for _, m := range ms {
for _, m := range o.measurements {
if m.desc.subscribed() {
record = true
break
}
}
if !record {
return
return nil
}
recorder(tag.FromContext(ctx), ms, attachments)
}

// RecordWithTags records one or multiple measurements at once.
//
// Measurements will be tagged with the tags in the context mutated by the mutators.
// RecordWithTags is useful if you want to record with tag mutations but don't want
// to propagate the mutations in the context.
func RecordWithTags(ctx context.Context, mutators []tag.Mutator, ms ...Measurement) error {
return RecordWithTagsAndAttachments(ctx, mutators, nil, ms...)
}

// RecordWithTagsAndAttachments records measurements and the given exemplar attachments at once.
//
// Measurements will be tagged with the tags in the context mutated by the mutators.
// RecordWithTags is useful if you want to record with tag mutations but don't want
// to propagate the mutations in the context.
func RecordWithTagsAndAttachments(ctx context.Context, mutators []tag.Mutator, attachments metricdata.Attachments, ms ...Measurement) error {
ctx, err := tag.New(ctx, mutators...)
if err != nil {
return err
if len(o.mutators) > 0 {
var err error
if ctx, err = tag.New(ctx, o.mutators...); err != nil {
return err
}
}
RecordWithAttachments(ctx, attachments, ms...)
recorder(tag.FromContext(ctx), o.measurements, o.attachments)
return nil
}
2 changes: 1 addition & 1 deletion stats/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRecordWithAttachments(t *testing.T) {
}

attachments := map[string]interface{}{metricdata.AttachmentKeySpanContext: spanCtx}
stats.RecordWithAttachments(context.Background(), attachments, m.M(12))
stats.RecordWithOptions(context.Background(), stats.WithAttachments(attachments), stats.WithMeasurements(m.M(12)))
rows, err := view.RetrieveData("test_view")
if err != nil {
t.Errorf("Failed to retrieve data %v", err)
Expand Down

0 comments on commit 0d5b2d8

Please sign in to comment.