本文次要钻研一下dapr的metrics_utils

NewMeasureView

dapr/pkg/diagnostics/utils/metrics_utils.go

// NewMeasureView creates opencensus View instance using stats.Measurefunc NewMeasureView(measure stats.Measure, keys []tag.Key, aggregation *view.Aggregation) *view.View {    return &view.View{        Name:        measure.Name(),        Description: measure.Description(),        Measure:     measure,        TagKeys:     keys,        Aggregation: aggregation,    }}
NewMeasureView依据measure、tag、aggregation来创立view

WithTags

dapr/pkg/diagnostics/utils/metrics_utils.go

// WithTags converts tag key and value pairs to tag.Mutator array.// WithTags(key1, value1, key2, value2) returns// []tag.Mutator{tag.Upsert(key1, value1), tag.Upsert(key2, value2)}func WithTags(opts ...interface{}) []tag.Mutator {    tagMutators := []tag.Mutator{}    for i := 0; i < len(opts)-1; i += 2 {        key, ok := opts[i].(tag.Key)        if !ok {            break        }        value, ok := opts[i+1].(string)        if !ok {            break        }        // skip if value is empty        if value != "" {            tagMutators = append(tagMutators, tag.Upsert(key, value))        }    }    return tagMutators}
WithTags办法反对变长的tag.Key类型,反对将tag.Key转换为tag.Mutator

AddTagKeyToCtx

dapr/pkg/diagnostics/utils/metrics_utils.go

// AddTagKeyToCtx assigns opencensus tag key value to contextfunc AddTagKeyToCtx(ctx context.Context, key tag.Key, value string) context.Context {    // return if value is not given    if value == "" {        return ctx    }    newCtx, err := tag.New(ctx, tag.Upsert(key, value))    if err != nil {        // return original if adding tagkey is failed.        return ctx    }    return newCtx}
AddTagKeyToCtx办法将key和value增加新的context中

AddNewTagKey

dapr/pkg/diagnostics/utils/metrics_utils.go

// AddNewTagKey adds new tag keys to existing viewfunc AddNewTagKey(views []*view.View, key *tag.Key) []*view.View {    for _, v := range views {        v.TagKeys = append(v.TagKeys, *key)    }    return views}
AddNewTagKey办法遍历views将指定key追加到view的TagKeys中

实例

dapr/pkg/diagnostics/utils/metrics_utils_test.go

func TestWithTags(t *testing.T) {    t.Run("one tag", func(t *testing.T) {        appKey := tag.MustNewKey("app_id")        mutators := WithTags(appKey, "test")        assert.Equal(t, 1, len(mutators))    })    t.Run("two tags", func(t *testing.T) {        appKey := tag.MustNewKey("app_id")        operationKey := tag.MustNewKey("operation")        mutators := WithTags(appKey, "test", operationKey, "op")        assert.Equal(t, 2, len(mutators))    })    t.Run("three tags", func(t *testing.T) {        appKey := tag.MustNewKey("app_id")        operationKey := tag.MustNewKey("operation")        methodKey := tag.MustNewKey("method")        mutators := WithTags(appKey, "test", operationKey, "op", methodKey, "method")        assert.Equal(t, 3, len(mutators))    })    t.Run("two tags with wrong value type", func(t *testing.T) {        appKey := tag.MustNewKey("app_id")        operationKey := tag.MustNewKey("operation")        mutators := WithTags(appKey, "test", operationKey, 1)        assert.Equal(t, 1, len(mutators))    })    t.Run("skip empty value key", func(t *testing.T) {        appKey := tag.MustNewKey("app_id")        operationKey := tag.MustNewKey("operation")        methodKey := tag.MustNewKey("method")        mutators := WithTags(appKey, "", operationKey, "op", methodKey, "method")        assert.Equal(t, 2, len(mutators))    })}

小结

dapr的metrics_utils基于opencensus的stats及tag提供了NewMeasureView、WithTags、AddTagKeyToCtx、AddNewTagKey办法。

doc

  • dapr