<commit_before>package conveymetric

import (
	"fmt"
	"github.com/Comcast/webpa-common/convey"
	"github.com/Comcast/webpa-common/xmetrics"
	"github.com/go-kit/kit/metrics"
)

const UnknownLabel = "unknown"

type MetricClosure func()

type CMetric interface {
	Update(data convey.C) (MetricClosure, error)
	//GetMetrics() []prometheus.Collector
}

func NewConveyMetric(registry xmetrics.Registry, tag string, name string) CMetric {
	return &cMetric{
		Registry: registry,
		Tag:      tag,
		Name:     name,
		metrics:  make(map[string]metrics.Gauge),
	}
}

type cMetric struct {
	Registry xmetrics.Registry
	Tag      string
	Name     string
	metrics  map[string]metrics.Gauge
}

func (m *cMetric) Update(data convey.C) (MetricClosure, error) {
	var gague metrics.Gauge

	key := UnknownLabel

	if item, ok := data[m.Tag].(string); ok {
		key = item
	}

	if val, found := m.metrics[key]; found {
		gague = val
	} else {
		m.metrics[key] = m.Registry.NewGauge(fmt.Sprintf("%s_%s_%s", m.Name, m.Tag, key))
		gague = m.metrics[key]
	}

	gague.Add(float64(1))
	return func() { gague.Add(float64(-1)) }, nil
}

//func (m *cMetric) GetMetrics() []prometheus.Collector {
//	metrics := make([]prometheus.Collector, len(m.metrics))
//
//	index := 0
//	for _, v := range m.metrics {
//		metrics[index] = v
//		index++
//	}
//
//	return metrics
//}<commit_msg>use provider.Provider instead of xmetrics.Registry<commit_after>package conveymetric

import (
	"fmt"
	"github.com/Comcast/webpa-common/convey"
	"github.com/go-kit/kit/metrics"
	"github.com/go-kit/kit/metrics/provider"
)

const UnknownLabel = "unknown"

type MetricClosure func()

type CMetric interface {
	Update(data convey.C) (MetricClosure, error)
}

func NewConveyMetric(provider provider.Provider, tag string, name string) CMetric {
	return &cMetric{
		provider: provider,
		tag:      tag,
		name:     name,
		metrics:  make(map[string]metrics.Gauge),
	}
}

type cMetric struct {
	provider provider.Provider
	tag      string
	name     string
	metrics  map[string]metrics.Gauge
}

func (m *cMetric) Update(data convey.C) (MetricClosure, error) {
	var gague metrics.Gauge

	key := UnknownLabel

	if item, ok := data[m.tag].(string); ok {
		key = item
	}

	if val, found := m.metrics[key]; found {
		gague = val
	} else {
		m.metrics[key] = m.provider.NewGauge(fmt.Sprintf("%s_%s_%s", m.name, m.tag, key))
		gague = m.metrics[key]
	}

	gague.Add(float64(1))
	return func() { gague.Add(float64(-1)) }, nil
}
{'fraction_non_alphanumeric': 0.14908350305498982, 'fraction_numerical': 0.005295315682281059, 'mean_word_length': 3.796875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1336133', 'n_tokens_mistral': 954, 'n_tokens_neox': 910, 'n_words': 201}