聊聊dapr的metrics_utils

codecraft · · 347 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下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


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊dapr的metrics_utils

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

347 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传