forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c229aa
commit feba6ab
Showing
5 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package logentry | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/grafana/loki/pkg/util" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
type counters struct { | ||
mtx sync.Mutex | ||
counters map[model.Fingerprint]prometheus.Counter | ||
} | ||
|
||
// newCounters Counts log entries by streams. | ||
func newCounters() *counters { | ||
return &counters{ | ||
counters: map[model.Fingerprint]prometheus.Counter{}, | ||
} | ||
} | ||
|
||
func (c *counters) Gather() ([]*dto.MetricFamily, error) { | ||
c.mtx.Lock() | ||
defer c.mtx.Unlock() | ||
|
||
var result []*dto.MetricFamily | ||
help := "the total count of log entries" | ||
name := "log_entries_total" | ||
mtype := dto.MetricType_COUNTER | ||
counters := &dto.MetricFamily{ | ||
Help: &help, | ||
Name: &name, | ||
Type: &mtype, | ||
} | ||
result = append(result, counters) | ||
for _, m := range c.counters { | ||
metric := &dto.Metric{} | ||
if err := m.Write(metric); err == nil { | ||
counters.Metric = append(counters.Metric, metric) | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (c *counters) Handle(labels model.LabelSet, time time.Time, entry string) error { | ||
c.mtx.Lock() | ||
defer c.mtx.Unlock() | ||
|
||
fp := labels.Fingerprint() | ||
var ok bool | ||
var counter prometheus.Counter | ||
if counter, ok = c.counters[fp]; !ok { | ||
counter = prometheus.NewCounter(prometheus.CounterOpts{ | ||
Help: "the total count of log entries", | ||
Name: "log_entries_total", | ||
ConstLabels: util.ModelLabelSetToMap(labels), | ||
}) | ||
c.counters[fp] = counter | ||
} | ||
counter.Inc() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package logentry | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
testutil "github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
var expected = `# HELP log_entries_total the total count of log entries | ||
# TYPE log_entries_total counter | ||
log_entries_total 10.0 | ||
log_entries_total{foo="bar"} 5.0 | ||
log_entries_total{bar="foo"} 5.0 | ||
log_entries_total{bar="foo",foo="bar"} 5.0 | ||
` | ||
|
||
func Test_newCounters(t *testing.T) { | ||
t.Parallel() | ||
|
||
handler := newCounters() | ||
|
||
workerCount := 5 | ||
var wg sync.WaitGroup | ||
for i := 0; i < workerCount; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
_ = handler.Handle(model.LabelSet(map[model.LabelName]model.LabelValue{}), time.Now(), "") | ||
_ = handler.Handle(model.LabelSet(map[model.LabelName]model.LabelValue{"foo": "bar"}), time.Now(), "") | ||
_ = handler.Handle(model.LabelSet(map[model.LabelName]model.LabelValue{"bar": "foo"}), time.Now(), "") | ||
_ = handler.Handle(model.LabelSet(map[model.LabelName]model.LabelValue{"bar": "foo", "foo": "bar"}), time.Now(), "") | ||
_ = handler.Handle(model.LabelSet(map[model.LabelName]model.LabelValue{}), time.Now(), "") | ||
|
||
}() | ||
} | ||
wg.Wait() | ||
|
||
if err := testutil.GatherAndCompare(handler, strings.NewReader(expected), "log_entries_total"); err != nil { | ||
t.Fatalf("missmatch metrics: %v", err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
vendor/github.com/prometheus/client_golang/prometheus/testutil/testutil.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.