C/C++教程

聊聊dapr的metrics_utils

本文主要是介绍聊聊dapr的metrics_utils,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文主要研究一下dapr的metrics_utils

NewMeasureView

dapr/pkg/diagnostics/utils/metrics_utils.go

// NewMeasureView creates opencensus View instance using stats.Measure
func 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 context
func 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 view
func 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
这篇关于聊聊dapr的metrics_utils的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!