聊聊dapr的metrics_utils

codecraft 2021/3/8 5:11:40

序本文主要研究一下dapr的metrics_utilsNewMeasureViewdapr/pkg/diagnostics/utils/metrics_utils.go// NewMeasureView creates opencensus View instance using stats.Measure func NewMeasureView(measure stats.Measure, keys []tag.Key, aggregation *view.Aggregati…

本文主要研究一下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
随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[聊聊dapr的metrics_utils]http://www.zyiz.net/tech/detail-152454.html

上一篇:如何使用 vercel + hexo 搭建博客

下一篇:RPC实现原理之特性介绍

赞(0)

共有 条评论 网友评论

验证码: 看不清楚?
    关注微信小程序
    程序员编程王-随时随地学编程

    扫描二维码或查找【程序员编程王】

    可以随时随地学编程啦!

    技术文章导航 更多>
    扫一扫关注最新编程教程