aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/func_counter.go
diff options
context:
space:
mode:
authorhcpp <hcpp@ydb.tech>2023-11-09 20:47:31 +0300
committerhcpp <hcpp@ydb.tech>2023-11-09 21:11:21 +0300
commitb7716e9978a4d1c2e548b9d53836a7d6894a8a38 (patch)
tree4ecf0ea759c7d44ed9749dc5d7beeaffc6376aac /library/go/core/metrics/solomon/func_counter.go
parentea9cef2dc79047c295a260f96895628b0feed43f (diff)
downloadydb-b7716e9978a4d1c2e548b9d53836a7d6894a8a38.tar.gz
YQ Connector:metrics (one more time)
custom httppuller has been added Revert "Revert "metrics have been added"" This reverts commit e2a874f25a443edf946bab9a7f077239ba569ab0, reversing changes made to 2dbbc3a1a033dd09ad29f0c168d8ea7fef97309e.
Diffstat (limited to 'library/go/core/metrics/solomon/func_counter.go')
-rw-r--r--library/go/core/metrics/solomon/func_counter.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/library/go/core/metrics/solomon/func_counter.go b/library/go/core/metrics/solomon/func_counter.go
new file mode 100644
index 0000000000..db862869e4
--- /dev/null
+++ b/library/go/core/metrics/solomon/func_counter.go
@@ -0,0 +1,86 @@
+package solomon
+
+import (
+ "encoding/json"
+ "time"
+
+ "go.uber.org/atomic"
+)
+
+var _ Metric = (*FuncCounter)(nil)
+
+// FuncCounter tracks int64 value returned by function.
+type FuncCounter struct {
+ name string
+ metricType metricType
+ tags map[string]string
+ function func() int64
+ timestamp *time.Time
+ useNameTag bool
+}
+
+func (c *FuncCounter) Name() string {
+ return c.name
+}
+
+func (c *FuncCounter) Function() func() int64 {
+ return c.function
+}
+
+func (c *FuncCounter) getType() metricType {
+ return c.metricType
+}
+
+func (c *FuncCounter) getLabels() map[string]string {
+ return c.tags
+}
+
+func (c *FuncCounter) getValue() interface{} {
+ return c.function()
+}
+
+func (c *FuncCounter) getTimestamp() *time.Time {
+ return c.timestamp
+}
+
+func (c *FuncCounter) getNameTag() string {
+ if c.useNameTag {
+ return "name"
+ } else {
+ return "sensor"
+ }
+}
+
+// MarshalJSON implements json.Marshaler.
+func (c *FuncCounter) MarshalJSON() ([]byte, error) {
+ return json.Marshal(struct {
+ Type string `json:"type"`
+ Labels map[string]string `json:"labels"`
+ Value int64 `json:"value"`
+ Timestamp *int64 `json:"ts,omitempty"`
+ }{
+ Type: c.metricType.String(),
+ Value: c.function(),
+ Labels: func() map[string]string {
+ labels := make(map[string]string, len(c.tags)+1)
+ labels[c.getNameTag()] = c.Name()
+ for k, v := range c.tags {
+ labels[k] = v
+ }
+ return labels
+ }(),
+ Timestamp: tsAsRef(c.timestamp),
+ })
+}
+
+// Snapshot returns independent copy on metric.
+func (c *FuncCounter) Snapshot() Metric {
+ return &Counter{
+ name: c.name,
+ metricType: c.metricType,
+ tags: c.tags,
+ value: *atomic.NewInt64(c.function()),
+
+ useNameTag: c.useNameTag,
+ }
+}