12 Commits

Author SHA1 Message Date
e4f2c77510 Merge pull request #53 from unistack-org/fixup
fix to support all metric types
2022-03-11 01:39:40 +03:00
b77f70aeb0 fix to support all metric types
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-03-11 01:37:53 +03:00
bae8886836 Merge pull request #52 from unistack-org/rewrite
rewrite prometheus meter
2022-03-11 01:26:46 +03:00
8ad0258828 rewrite most stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-03-11 01:24:14 +03:00
d7a9e96561 Merge branch 'master' into v3 2022-03-11 01:21:45 +03:00
4a8caabc52 update go version
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-03-07 13:46:11 +03:00
5d50b02888 update workflows
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-03-07 12:13:47 +03:00
dependabot[bot]
c793eeafda Bump go.unistack.org/micro/v3 from 3.8.16 to 3.8.21
Bumps [go.unistack.org/micro/v3](https://github.com/unistack-org/micro) from 3.8.16 to 3.8.21.
- [Release notes](https://github.com/unistack-org/micro/releases)
- [Commits](https://github.com/unistack-org/micro/compare/v3.8.16...v3.8.21)

---
updated-dependencies:
- dependency-name: go.unistack.org/micro/v3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-07 12:13:47 +03:00
dependabot[bot]
4bad2f0a5f Bump golangci/golangci-lint-action from 2 to 3
Bumps [golangci/golangci-lint-action](https://github.com/golangci/golangci-lint-action) from 2 to 3.
- [Release notes](https://github.com/golangci/golangci-lint-action/releases)
- [Commits](https://github.com/golangci/golangci-lint-action/compare/v2...v3)

---
updated-dependencies:
- dependency-name: golangci/golangci-lint-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-07 12:13:47 +03:00
dependabot[bot]
1dac3b9598 Bump dependabot/fetch-metadata from 1.1.1 to 1.2.1
Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1.1.1 to 1.2.1.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v1.1.1...v1.2.1)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-07 12:13:47 +03:00
dependabot[bot]
c77ac5eed2 Bump github.com/prometheus/client_golang from 1.12.0 to 1.12.1
Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.12.0 to 1.12.1.
- [Release notes](https://github.com/prometheus/client_golang/releases)
- [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prometheus/client_golang/compare/v1.12.0...v1.12.1)

---
updated-dependencies:
- dependency-name: github.com/prometheus/client_golang
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-07 12:13:47 +03:00
1a03f7e25f Merge pull request #41 from unistack-org/master
merge master
2022-01-23 16:21:56 +03:00
5 changed files with 291 additions and 186 deletions

View File

@@ -12,7 +12,7 @@ jobs:
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: 1.16 go-version: 1.17
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

View File

@@ -47,7 +47,7 @@ jobs:
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: 1.16 go-version: 1.17
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: init - name: init
uses: github/codeql-action/init@v1 uses: github/codeql-action/init@v1

View File

@@ -12,7 +12,7 @@ jobs:
- name: setup - name: setup
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: 1.16 go-version: 1.17
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: cache - name: cache

View File

@@ -2,6 +2,7 @@ package prometheus
import ( import (
"fmt" "fmt"
"hash/fnv"
"io" "io"
"sync" "sync"
"time" "time"
@@ -13,26 +14,58 @@ import (
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/meter"
) )
var _ meter.Meter = &prometheusMeter{}
type prometheusMeter struct { type prometheusMeter struct {
opts meter.Options opts meter.Options
set prometheus.Registerer set prometheus.Registerer
counter map[string]prometheusCounter counter map[string]*counters
floatCounter map[string]prometheusFloatCounter floatCounter map[string]*floatCounters
gauge map[string]prometheusGauge gauge map[string]*gauges
histogram map[string]prometheusHistogram histogram map[string]*histograms
summary map[string]prometheusSummary summary map[string]*summaries
sync.Mutex sync.Mutex
} }
func NewMeter(opts ...meter.Option) meter.Meter { type counters struct {
cs map[uint64]*prometheusCounter
}
type gauges struct {
cs map[uint64]*prometheusGauge
}
type histograms struct {
cs map[uint64]*prometheusHistogram
}
type summaries struct {
cs map[uint64]*prometheusSummary
}
type floatCounters struct {
cs map[uint64]*prometheusFloatCounter
}
func newFloat64(v float64) *float64 {
nv := v
return &nv
}
func newString(v string) *string {
nv := v
return &nv
}
func NewMeter(opts ...meter.Option) *prometheusMeter {
return &prometheusMeter{ return &prometheusMeter{
set: prometheus.DefaultRegisterer, set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
opts: meter.NewOptions(opts...), opts: meter.NewOptions(opts...),
counter: make(map[string]prometheusCounter), counter: make(map[string]*counters),
floatCounter: make(map[string]prometheusFloatCounter), floatCounter: make(map[string]*floatCounters),
gauge: make(map[string]prometheusGauge), gauge: make(map[string]*gauges),
histogram: make(map[string]prometheusHistogram), histogram: make(map[string]*histograms),
summary: make(map[string]prometheusSummary), summary: make(map[string]*summaries),
} }
} }
@@ -69,17 +102,16 @@ func (m *prometheusMeter) buildName(name string) string {
} }
func (m *prometheusMeter) buildLabels(labels ...string) []string { func (m *prometheusMeter) buildLabels(labels ...string) []string {
nl := len(m.opts.Labels) + len(labels) nl := len(labels)
if nl == 0 { if nl == 0 {
return nil return nil
} }
nlabels := make([]string, 0, nl) nlabels := make([]string, 0, nl)
nlabels = append(nlabels, m.opts.Labels...)
nlabels = append(nlabels, labels...)
for idx := 0; idx < nl; idx++ { for idx := 0; idx < nl; idx++ {
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx] nlabels = append(nlabels, m.opts.LabelPrefix+labels[idx])
nlabels = append(nlabels, labels[idx+1])
idx++ idx++
} }
return nlabels return nlabels
@@ -89,191 +121,156 @@ func (m *prometheusMeter) Name() string {
return m.opts.Name return m.opts.Name
} }
func (m *prometheusMeter) mapLabels(src ...string) map[string]string {
src = m.buildLabels(src...)
mp := make(map[string]string, len(src)/2)
for idx := 0; idx < len(src); idx++ {
mp[src[idx]] = src[idx+1]
idx++
}
return mp
}
func (m *prometheusMeter) metricEqual(src []string, dst []string) bool {
if len(src) != len(dst)/2 {
return false
}
dst = m.buildLabels(dst...)
mp := make(map[string]struct{}, len(src))
for idx := range src {
mp[src[idx]] = struct{}{}
}
for idx := 0; idx < len(dst); idx++ {
if _, ok := mp[dst[idx]]; !ok {
return false
}
idx++
}
return true
}
func (m *prometheusMeter) labelNames(src map[string]string, dst []string) []string {
dst = m.buildLabels(dst...)
nlabels := make([]string, 0, len(src))
for idx := 0; idx < len(dst); idx++ {
if src == nil {
nlabels = append(nlabels, dst[idx])
} else if _, ok := src[dst[idx]]; ok {
nlabels = append(nlabels, dst[idx])
} else {
nlabels = append(nlabels, dst[idx])
}
idx++
}
return nlabels
}
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.counter[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
var lnames []string cd, ok := m.counter[nm]
h := newHash(labels)
if !ok { if !ok {
lnames = m.labelNames(nil, labels) cd = &counters{cs: make(map[uint64]*prometheusCounter)}
fmt.Printf("!ok lnames: %v\n", lnames) c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, lnames) cd.cs[h] = c
c = prometheusCounter{c: nc, lnames: lnames} m.counter[nm] = cd
m.counter[nm] = c return c
} else if !m.metricEqual(c.lnames, labels) {
fmt.Printf("ok && !m.metricEqual lnames: %v labels: %v\n", c.lnames, labels)
lnames = m.labelNames(c.labels, labels)
m.set.Unregister(c.c)
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, lnames)
c = prometheusCounter{c: nc, lnames: lnames}
m.counter[nm] = c
m.set.MustRegister(c.c)
} else {
lnames = c.lnames
} }
fmt.Printf("lnames %v\n", lnames) c, ok := cd.cs[h]
return prometheusCounter{c: c.c, lnames: lnames, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.counter[nm] = cd
}
return c
} }
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.floatCounter[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.floatCounter[nm]
h := newHash(labels)
if !ok { if !ok {
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels)) cd = &floatCounters{cs: make(map[uint64]*prometheusFloatCounter)}
c = prometheusFloatCounter{c: nc} c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
m.floatCounter[nm] = c cd.cs[h] = c
} else if !m.metricEqual(c.lnames, labels) { m.floatCounter[nm] = cd
m.set.Unregister(c.c) return c
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
c = prometheusFloatCounter{c: nc}
m.floatCounter[nm] = c
m.set.MustRegister(c.c)
} }
c, ok := cd.cs[h]
return prometheusFloatCounter{c: c.c, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.floatCounter[nm] = cd
}
return c
} }
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge { func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.gauge[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.gauge[nm]
h := newHash(labels)
if !ok { if !ok {
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels)) cd = &gauges{cs: make(map[uint64]*prometheusGauge)}
c = prometheusGauge{c: nc} c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
m.gauge[nm] = c cd.cs[h] = c
} else if !m.metricEqual(c.lnames, labels) { m.gauge[nm] = cd
m.set.Unregister(c.c) return c
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
c = prometheusGauge{c: nc}
m.gauge[nm] = c
m.set.MustRegister(c.c)
} }
c, ok := cd.cs[h]
return prometheusGauge{c: c.c, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.gauge[nm] = cd
}
return c
} }
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.histogram[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.histogram[nm]
h := newHash(labels)
if !ok { if !ok {
nc := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: nm}, m.labelNames(c.labels, labels)) cd = &histograms{cs: make(map[uint64]*prometheusHistogram)}
c = prometheusHistogram{c: nc} c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
m.histogram[nm] = c cd.cs[h] = c
} else if !m.metricEqual(c.lnames, labels) { m.histogram[nm] = cd
m.set.Unregister(c.c) return c
nc := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: nm}, m.labelNames(c.labels, labels))
c = prometheusHistogram{c: nc}
m.histogram[nm] = c
m.set.MustRegister(c.c)
} }
c, ok := cd.cs[h]
return prometheusHistogram{c: c.c, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.histogram[nm] = cd
}
return c
} }
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.summary[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.summary[nm]
h := newHash(labels)
if !ok { if !ok {
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels)) cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
c = prometheusSummary{c: nc} c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
m.summary[nm] = c cd.cs[h] = c
} else if !m.metricEqual(c.lnames, labels) { m.summary[nm] = cd
m.set.Unregister(c.c) return c
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels))
c = prometheusSummary{c: nc}
m.summary[nm] = c
m.set.MustRegister(c.c)
} }
c, ok := cd.cs[h]
return prometheusSummary{c: c.c, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
}
return c
} }
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary { func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
c, ok := m.summary[nm] labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.summary[nm]
h := newHash(labels)
if !ok { if !ok {
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{ cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm, Name: nm,
MaxAge: window, MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}, m.labelNames(c.labels, labels)) }), labels: labels}
c = prometheusSummary{c: nc} cd.cs[h] = c
m.summary[nm] = c m.summary[nm] = cd
} else if !m.metricEqual(c.lnames, labels) { return c
m.set.Unregister(c.c)
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels))
c = prometheusSummary{c: nc}
m.summary[nm] = c
m.set.MustRegister(c.c)
} }
c, ok := cd.cs[h]
return prometheusSummary{c: c.c, labels: m.mapLabels(labels...)} if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm,
MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
}
return c
} }
func (m *prometheusMeter) Init(opts ...meter.Option) error { func (m *prometheusMeter) Init(opts ...meter.Option) error {
for _, o := range opts { for _, o := range opts {
o(&m.opts) o(&m.opts)
} }
return nil return nil
} }
@@ -299,6 +296,112 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
} }
enc := expfmt.NewEncoder(w, expfmt.FmtText) enc := expfmt.NewEncoder(w, expfmt.FmtText)
for name, metrics := range m.counter {
mf := &dto.MetricFamily{
Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
c.c.Write(m)
for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx],
Value: &c.labels[idx+1],
})
idx++
}
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
}
for name, metrics := range m.gauge {
mf := &dto.MetricFamily{
Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
c.c.Write(m)
for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx],
Value: &c.labels[idx+1],
})
idx++
}
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
}
for name, metrics := range m.floatCounter {
mf := &dto.MetricFamily{
Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
c.c.Write(m)
for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx],
Value: &c.labels[idx+1],
})
idx++
}
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
}
for name, metrics := range m.histogram {
mf := &dto.MetricFamily{
Name: newString(name),
Type: dto.MetricType_HISTOGRAM.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
c.c.Write(m)
for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx],
Value: &c.labels[idx+1],
})
idx++
}
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
}
for name, metrics := range m.summary {
mf := &dto.MetricFamily{
Name: newString(name),
Type: dto.MetricType_SUMMARY.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
c.c.Write(m)
for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx],
Value: &c.labels[idx+1],
})
idx++
}
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
}
for _, mf := range mfs { for _, mf := range mfs {
_ = enc.Encode(mf) _ = enc.Encode(mf)
} }
@@ -344,103 +447,106 @@ func (m *prometheusMeter) Set(opts ...meter.Option) meter.Meter {
} }
type prometheusCounter struct { type prometheusCounter struct {
c *prometheus.GaugeVec c prometheus.Gauge
lnames []string labels []string
labels prometheus.Labels
} }
func (c prometheusCounter) Add(n int) { func (c *prometheusCounter) Add(n int) {
nc, _ := c.c.GetMetricWith(c.labels) c.c.Add(float64(n))
nc.Add(float64(n))
} }
func (c prometheusCounter) Dec() { func (c *prometheusCounter) Dec() {
c.c.With(c.labels).Dec() c.c.Dec()
} }
func (c prometheusCounter) Inc() { func (c *prometheusCounter) Inc() {
c.c.With(c.labels).Inc() c.c.Inc()
} }
func (c prometheusCounter) Get() uint64 { func (c *prometheusCounter) Get() uint64 {
m := &dto.Metric{} m := &dto.Metric{}
if err := c.c.With(c.labels).Write(m); err != nil { if err := c.c.Write(m); err != nil {
return 0 return 0
} }
return uint64(m.GetGauge().GetValue()) return uint64(m.GetGauge().GetValue())
} }
func (c prometheusCounter) Set(n uint64) { func (c *prometheusCounter) Set(n uint64) {
c.c.With(c.labels).Set(float64(n)) c.c.Set(float64(n))
} }
type prometheusFloatCounter struct { type prometheusFloatCounter struct {
c *prometheus.GaugeVec c prometheus.Gauge
lnames []string labels []string
labels prometheus.Labels
} }
func (c prometheusFloatCounter) Add(n float64) { func (c prometheusFloatCounter) Add(n float64) {
c.c.With(c.labels).Add(n) c.c.Add(n)
} }
func (c prometheusFloatCounter) Get() float64 { func (c prometheusFloatCounter) Get() float64 {
m := &dto.Metric{} m := &dto.Metric{}
if err := c.c.With(c.labels).Write(m); err != nil { if err := c.c.Write(m); err != nil {
return 0 return 0
} }
return m.GetGauge().GetValue() return m.GetGauge().GetValue()
} }
func (c prometheusFloatCounter) Set(n float64) { func (c prometheusFloatCounter) Set(n float64) {
c.c.With(c.labels).Set(n) c.c.Set(n)
} }
func (c prometheusFloatCounter) Sub(n float64) { func (c prometheusFloatCounter) Sub(n float64) {
c.c.With(c.labels).Add(-n) c.c.Add(-n)
} }
type prometheusGauge struct { type prometheusGauge struct {
c *prometheus.GaugeVec c prometheus.Gauge
lnames []string labels []string
labels prometheus.Labels
} }
func (c prometheusGauge) Get() float64 { func (c prometheusGauge) Get() float64 {
m := &dto.Metric{} m := &dto.Metric{}
if err := c.c.With(c.labels).Write(m); err != nil { if err := c.c.Write(m); err != nil {
return 0 return 0
} }
return float64(m.GetGauge().GetValue()) return float64(m.GetGauge().GetValue())
} }
type prometheusHistogram struct { type prometheusHistogram struct {
c *prometheus.HistogramVec c prometheus.Histogram
lnames []string labels []string
labels prometheus.Labels
} }
func (c prometheusHistogram) Reset() { func (c prometheusHistogram) Reset() {
} }
func (c prometheusHistogram) Update(n float64) { func (c prometheusHistogram) Update(n float64) {
c.c.With(c.labels).Observe(n) c.c.Observe(n)
} }
func (c prometheusHistogram) UpdateDuration(n time.Time) { func (c prometheusHistogram) UpdateDuration(n time.Time) {
c.c.With(c.labels).Observe(time.Since(n).Seconds()) c.c.Observe(time.Since(n).Seconds())
} }
type prometheusSummary struct { type prometheusSummary struct {
c *prometheus.SummaryVec c prometheus.Summary
lnames []string labels []string
labels prometheus.Labels
} }
func (c prometheusSummary) Update(n float64) { func (c prometheusSummary) Update(n float64) {
c.c.With(c.labels).Observe(n) c.c.Observe(n)
} }
func (c prometheusSummary) UpdateDuration(n time.Time) { func (c prometheusSummary) UpdateDuration(n time.Time) {
c.c.With(c.labels).Observe(time.Since(n).Seconds()) c.c.Observe(time.Since(n).Seconds())
}
func newHash(labels []string) uint64 {
labels = meter.BuildLabels(labels...)
h := fnv.New64a()
for _, l := range labels {
h.Write([]byte(l))
}
return h.Sum64()
} }

View File

@@ -13,9 +13,8 @@ import (
func TestBuildName(t *testing.T) { func TestBuildName(t *testing.T) {
m := NewMeter() m := NewMeter()
im := m.(*prometheusMeter)
check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}` check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`
name := im.buildMetric("foo", "bar", "baz", "aaa", "b", "ccc", "d") name := m.buildMetric("foo", "bar", "baz", "aaa", "b", "ccc", "d")
if name != check { if name != check {
t.Fatalf("metric name error: %s != %s", name, check) t.Fatalf("metric name error: %s != %s", name, check)
} }