Skip to content

Commit

Permalink
update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed Sep 21, 2023
1 parent 7e22c61 commit 518ebcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
9 changes: 5 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type PushConfig struct {
// ExtraLabels may contain comma-separated list of `label="value"` labels, which will be added
// to all the metrics before pushing them to PushURL.
ExtraLabels string
// WriteMetrics defines the function to write metrics
WriteMetrics func(w io.Writer)

pushURL *url.URL
}
Expand All @@ -50,7 +48,10 @@ func (pc *PushConfig) Validate() error {
}

// Push run request to the defined PushURL every Interval
func (pc *PushConfig) Push() {
func (pc *PushConfig) Push(writeMetrics func(w io.Writer)) {
if writeMetrics == nil {
panic(fmt.Errorf("write metrics function not defined"))

Check warning on line 53 in config.go

View check run for this annotation

Codecov / codecov/patch

config.go#L51-L53

Added lines #L51 - L53 were not covered by tests
}
pushURLRedacted := pc.pushURL.Redacted()

Check warning on line 55 in config.go

View check run for this annotation

Codecov / codecov/patch

config.go#L55

Added line #L55 was not covered by tests
// by default set interval to one second
if pc.Interval == 0 {
Expand All @@ -71,7 +72,7 @@ func (pc *PushConfig) Push() {
zw := gzip.NewWriter(&bb)
for range ticker.C {
bb.Reset()
pc.WriteMetrics(&bb)
writeMetrics(&bb)
if len(pc.ExtraLabels) > 0 {
tmpBuf = addExtraLabels(tmpBuf[:0], bb.Bytes(), pc.ExtraLabels)
bb.Reset()
Expand Down
21 changes: 8 additions & 13 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func InitPushWithConfig(pushConfig *PushConfig, pushProcessMetrics bool) error {
if err := pushConfig.Validate(); err != nil {
return err

Check warning on line 59 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L57-L59

Added lines #L57 - L59 were not covered by tests
}
pushConfig.WriteMetrics = func(w io.Writer) {
f := func(w io.Writer) {
WritePrometheus(w, pushProcessMetrics)

Check warning on line 62 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}
go pushConfig.Push()
go pushConfig.Push(f)
return nil

Check warning on line 65 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

Expand Down Expand Up @@ -94,10 +94,10 @@ func (s *Set) InitPushWithConfig(pushConfig *PushConfig) error {
if err := pushConfig.Validate(); err != nil {
return err

Check warning on line 95 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L93-L95

Added lines #L93 - L95 were not covered by tests
}
pushConfig.WriteMetrics = func(w io.Writer) {
f := func(w io.Writer) {
s.WritePrometheus(w)

Check warning on line 98 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}
go pushConfig.Push()
go pushConfig.Push(f)
return nil

Check warning on line 101 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}

Expand All @@ -118,25 +118,20 @@ func (s *Set) InitPushWithConfig(pushConfig *PushConfig) error {
// It is OK calling InitPushExt multiple times with different writeMetrics -
// in this case all the metrics generated by writeMetrics callbacks are written to pushURL.
func InitPushExt(pushURL string, interval time.Duration, extraLabels string, writeMetrics func(w io.Writer)) error {
pc := &PushConfig{
PushURL: pushURL,
Interval: interval,
ExtraLabels: extraLabels,
WriteMetrics: writeMetrics,
}
return InitPushExtWithConfig(pc)
pc := &PushConfig{PushURL: pushURL, Interval: interval, ExtraLabels: extraLabels}
return InitPushExtWithConfig(pc, writeMetrics)
}

// InitPushExtWithConfig sets up periodic push for metrics obtained by calling writeMetrics with the given interval
// defined in the PushConfig.
//
// It is OK calling InitPushExtWithConfig multiple times with different writeMetrics -
// in this case all the metrics generated by writeMetrics callbacks are written to PushURL.
func InitPushExtWithConfig(pushConfig *PushConfig) error {
func InitPushExtWithConfig(pushConfig *PushConfig, writeMetrics func(w io.Writer)) error {
if err := pushConfig.Validate(); err != nil {
return err
}
go pushConfig.Push()
go pushConfig.Push(writeMetrics)

Check warning on line 134 in push.go

View check run for this annotation

Codecov / codecov/patch

push.go#L134

Added line #L134 was not covered by tests
return nil
}

Expand Down

0 comments on commit 518ebcd

Please sign in to comment.