Skip to content

Commit 5d583c9

Browse files
committed
feat: support load batch
Signed-off-by: MEX7 <mex7.0828@gmail.com>
1 parent b82912b commit 5d583c9

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

container.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package prom2click
22

33
import (
4+
"fmt"
5+
46
"github.com/gotomicro/ego/core/econf"
57
"github.com/gotomicro/ego/core/elog"
68
)
@@ -32,6 +34,27 @@ func Load(key string) *Container {
3234
return c
3335
}
3436

37+
func LoadBatch(key string) []*Container {
38+
containers := make([]*Container, 0)
39+
configs := make([]*config, 0)
40+
if err := econf.UnmarshalKey(key, &configs); err != nil {
41+
elog.EgoLogger.With(elog.FieldComponent(PackageName)).Panic("parse config error", elog.FieldErr(err), elog.FieldKey(key))
42+
return nil
43+
}
44+
for index := range configs {
45+
c := DefaultContainer()
46+
c.logger = c.logger.With(elog.FieldComponentName(key))
47+
c.config.Host = configs[index].Host
48+
c.config.Port = configs[index].Port
49+
c.config.ClickhouseDSN = configs[index].ClickhouseDSN
50+
c.config.ClickhouseDB = configs[index].ClickhouseDB
51+
c.config.ClickhouseTable = configs[index].ClickhouseTable
52+
c.name = fmt.Sprintf("%s_%d", key, index)
53+
containers = append(containers, c)
54+
}
55+
return containers
56+
}
57+
3558
// Build 构建组件
3659
func (c *Container) Build(options ...Option) *Component {
3760
for _, option := range options {
@@ -45,6 +68,5 @@ func (c *Container) Build(options ...Option) *Component {
4568
if c.config.EnableMetricInterceptor {
4669
server.Use(metricServerInterceptor())
4770
}
48-
4971
return server
5072
}

prom_writer.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"fmt"
66
"sort"
7+
"strconv"
78
"sync"
89
"time"
910

@@ -49,37 +50,42 @@ func NewWriter(conf *config) (*promWriter, error) {
4950

5051
w.tx = prometheus.NewCounter(
5152
prometheus.CounterOpts{
52-
Name: "sent_samples_total",
53-
Help: "Total number of processed samples sent to remote storage.",
53+
Name: "sent_samples_total",
54+
Help: "Total number of processed samples sent to remote storage.",
55+
ConstLabels: map[string]string{"host": conf.Host, "port": strconv.Itoa(conf.Port)},
5456
},
5557
)
5658

5759
w.ko = prometheus.NewCounter(
5860
prometheus.CounterOpts{
59-
Name: "failed_samples_total",
60-
Help: "Total number of processed samples which failed on send to remote storage.",
61+
Name: "failed_samples_total",
62+
Help: "Total number of processed samples which failed on send to remote storage.",
63+
ConstLabels: map[string]string{"host": conf.Host, "port": strconv.Itoa(conf.Port)},
6164
},
6265
)
6366

6467
w.test = prometheus.NewCounter(
6568
prometheus.CounterOpts{
66-
Name: "prometheus_remote_storage_sent_batch_duration_seconds_bucket_test",
67-
Help: "Test metric to ensure backfilled metrics are readable via prometheus.",
69+
Name: "prometheus_remote_storage_sent_batch_duration_seconds_bucket_test",
70+
Help: "Test metric to ensure backfilled metrics are readable via prometheus.",
71+
ConstLabels: map[string]string{"host": conf.Host, "port": strconv.Itoa(conf.Port)},
6872
},
6973
)
7074

7175
w.timings = prometheus.NewHistogram(
7276
prometheus.HistogramOpts{
73-
Name: "sent_batch_duration_seconds",
74-
Help: "Duration of sample batch send calls to the remote storage.",
75-
Buckets: prometheus.DefBuckets,
77+
Name: "sent_batch_duration_seconds",
78+
Help: "Duration of sample batch send calls to the remote storage.",
79+
Buckets: prometheus.DefBuckets,
80+
ConstLabels: map[string]string{"host": conf.Host, "port": strconv.Itoa(conf.Port)},
7681
},
7782
)
7883

7984
w.rx = prometheus.NewCounter(
8085
prometheus.CounterOpts{
81-
Name: "received_samples_total",
82-
Help: "Total number of received samples.",
86+
Name: "received_samples_total",
87+
Help: "Total number of received samples.",
88+
ConstLabels: map[string]string{"host": conf.Host, "port": strconv.Itoa(conf.Port)},
8389
},
8490
)
8591

0 commit comments

Comments
 (0)