关于golang:聊聊dapr的metricsutils

7次阅读

共计 2826 个字符,预计需要花费 8 分钟才能阅读完成。

本文次要钻研一下 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
正文完
 0