Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 0 additions & 127 deletions metrics/point_test.go

This file was deleted.

4 changes: 4 additions & 0 deletions point/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (c *checker) check(pt *Point) *Point {
}

func (c *checker) addWarn(t, msg string) {
defer func() {
pointCheckWarnVec.WithLabelValues(t).Inc()
}()

c.warns = append(c.warns, &Warn{
Type: t, Msg: msg,
})
Expand Down
29 changes: 29 additions & 0 deletions point/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package point

import (
"github.com/GuanceCloud/cliutils/metrics"
p8s "github.com/prometheus/client_golang/prometheus"
)

var (
pointCheckWarnVec = p8s.NewCounterVec(
p8s.CounterOpts{
Namespace: "point",
Name: "check_point_warning_total",
Help: "Warnings among build new point",
},
[]string{"type"},
)
)

func ResetMetrics() {
pointCheckWarnVec.Reset()
}

func init() {
metrics.MustRegister(pointCheckWarnVec)
}
43 changes: 43 additions & 0 deletions point/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package point

import (
T "testing"

"github.com/GuanceCloud/cliutils/metrics"
p8s "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMetrics(t *T.T) {
t.Run(`basic`, func(t *T.T) {

ResetMetrics()

defer t.Cleanup(func() {
ResetMetrics()
})

reg := p8s.NewRegistry()
reg.MustRegister(PointCheckWarnVec)

pt := NewPointV2("", nil)

t.Logf("pt: %s", pt.Pretty())

mfs, err := reg.Gather()
require.NoError(t, err)

assert.Equal(t, 1.0, metrics.GetMetricOnLabels(mfs,
"point_check_point_warning_total",
"empty measurement, use __default",
"invalid_measurement",
).GetCounter().GetValue())

t.Logf("\n%s", metrics.MetricFamily2Text(mfs))
})
}
34 changes: 16 additions & 18 deletions metrics/point.go → point/p8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

package metrics
package point

import (
"strings"
"time"

"github.com/GuanceCloud/cliutils/point"
"github.com/prometheus/client_golang/prometheus"
p8s "github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)

Expand All @@ -26,15 +25,15 @@ import (
// covered the time field. For prometheus metrics, these time value are
// the same.
//
// When point.Point are logging, due to the lack of `time-series',
// When Point are logging, due to the lack of `time-series',
// we hava to merge multiple points' fields together to build a single point.
//
// For time-series data, we don't need to do this, the storage
// engine merged them automatically(grouped by time-series).
func mergePts(pts []*point.Point) []*point.Point {
func mergePts(pts []*Point) []*Point {
// same-hash point put together
var res []*point.Point
ptMap := map[string][]*point.Point{}
var res []*Point
ptMap := map[string][]*Point{}
for _, pt := range pts {
hash := pt.MD5()
ptMap[hash] = append(ptMap[hash], pt)
Expand All @@ -58,7 +57,7 @@ func mergePts(pts []*point.Point) []*point.Point {
return res
}

func doGatherPoints(reg prometheus.Gatherer) ([]*point.Point, error) {
func doGatherPoints(reg p8s.Gatherer) ([]*Point, error) {
mfs, err := reg.Gather()
if err != nil {
return nil, err
Expand All @@ -67,25 +66,25 @@ func doGatherPoints(reg prometheus.Gatherer) ([]*point.Point, error) {
// All gathered data should have the same timestamp, we enforce it.
now := time.Now()

var pts []*point.Point
var pts []*Point
for _, mf := range mfs {
arr := strings.SplitN(*mf.Name, "_", 2)

name := arr[0]
fieldName := arr[1]

for _, m := range mf.Metric {
var kvs point.KVs
var kvs KVs
for _, label := range m.GetLabel() {
kvs = append(kvs, point.NewKV(label.GetName(), label.GetValue(), point.WithKVTagSet(true)))
kvs = append(kvs, NewKV(label.GetName(), label.GetValue(), WithKVTagSet(true)))
}

switch *mf.Type {
case dto.MetricType_COUNTER:
kvs = append(kvs, point.NewKV(fieldName, m.GetCounter().GetValue()))
kvs = append(kvs, NewKV(fieldName, m.GetCounter().GetValue()))
case dto.MetricType_SUMMARY:
avg := uint64(m.GetSummary().GetSampleSum()) / m.GetSummary().GetSampleCount()
kvs = append(kvs, point.NewKV(fieldName, avg))
kvs = append(kvs, NewKV(fieldName, avg))

case dto.MetricType_GAUGE:
continue // TODO
Expand All @@ -103,16 +102,15 @@ func doGatherPoints(reg prometheus.Gatherer) ([]*point.Point, error) {
ts = time.Unix(0, int64(time.Millisecond)**m.TimestampMs)
}

opts := append(point.DefaultMetricOptions(), point.WithTime(ts))
pts = append(pts, point.NewPointV2(name, kvs, opts...))
opts := append(DefaultMetricOptions(), WithTime(ts))
pts = append(pts, NewPointV2(name, kvs, opts...))
}
}

return pts, nil
}

// GatherPoints gather all metrics in global registry, but convert these metrics
// to point.Point.
func GatherPoints() ([]*point.Point, error) {
// GatherPoints gather all metrics in reg, but convert these metrics to Point.
func GatherPoints(reg p8s.Gatherer) ([]*Point, error) {
return doGatherPoints(reg)
}
Loading