Skip to content

Commit

Permalink
Revert "support push config for InitPush (#53)"
Browse files Browse the repository at this point in the history
This reverts commit 42c28a8.

Reason for revert: the provided additional public API looks not very good for future support.
See #53 (review) for details.
  • Loading branch information
valyala committed Dec 17, 2023
1 parent 42c28a8 commit cd448dd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 205 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
// ... or push registered metrics every 10 seconds to http://victoria-metrics:8428/api/v1/import/prometheus
// with the added `instance="foobar"` label to all the pushed metrics.
metrics.InitPush("http://victoria-metrics:8428/api/v1/import/prometheus", 10*time.Second, `instance="foobar"`, true)

// ... or use metrics.PushConfig to see full list of configuration params
// metrics.InitPushWithConfig(metrics.PushConfig{URL: "http://victoria-metrics:8428/api/v1/import/prometheus", Interval: 10*time.Second})
```

See [docs](http://godoc.org/github.com/VictoriaMetrics/metrics) for more info.
Expand Down
52 changes: 0 additions & 52 deletions config.go

This file was deleted.

33 changes: 0 additions & 33 deletions config_test.go

This file was deleted.

63 changes: 19 additions & 44 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"

"compress/gzip"
Expand Down Expand Up @@ -88,45 +90,25 @@ func (s *Set) InitPush(pushURL string, interval time.Duration, extraLabels strin
// in this case metrics are pushed to all the provided pushURL urls.
//
// It is OK calling InitPushExt multiple times with different writeMetrics -
// in this case all the metrics generated by writeMetrics callbacks are written to pushURL.
// in this case all the metrics generated by writeMetrics callbacks are writte to pushURL.
func InitPushExt(pushURL string, interval time.Duration, extraLabels string, writeMetrics func(w io.Writer)) error {
cfg := PushConfig{
URL: pushURL,
Interval: interval,
ExtraLabels: extraLabels,
WriteMetricsFn: writeMetrics,
if interval <= 0 {
return fmt.Errorf("interval must be positive; got %s", interval)
}
return InitPushWithConfig(cfg)
}

// InitPushWithConfig sets up periodic push for metrics based on params from passed PushConfig.
//
// It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to
// https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format
//
// It is OK calling InitPushWithConfig multiple times with different PushConfig.URL -
// in this case metrics are pushed to all the provided PushConfig.URL urls.
//
// It is OK calling InitPushWithConfig multiple times with different PushConfig.WriteMetricsFn -
// in this case all the metrics generated by PushConfig.WriteMetricsFn callbacks are written to PushConfig.URL.
//
// If
func InitPushWithConfig(cfg PushConfig) error {
if err := cfg.Validate(); err != nil {
return fmt.Errorf("failed to validate PushConfig: %w", err)
if err := validateTags(extraLabels); err != nil {
return fmt.Errorf("invalid extraLabels=%q: %w", extraLabels, err)
}

writeMetrics := cfg.WriteMetricsFn
if writeMetrics == nil {
writeMetrics = func(w io.Writer) {
WritePrometheus(w, true)
}
pu, err := url.Parse(pushURL)
if err != nil {
return fmt.Errorf("cannot parse pushURL=%q: %w", pushURL, err)
}

pushURL := cfg.pushURL
pushURLRedacted := pushURL.Redacted()

interval := cfg.Interval
if pu.Scheme != "http" && pu.Scheme != "https" {
return fmt.Errorf("unsupported scheme in pushURL=%q; expecting 'http' or 'https'", pushURL)
}
if pu.Host == "" {
return fmt.Errorf("missing host in pushURL=%q", pushURL)
}
pushURLRedacted := pu.Redacted()
c := &http.Client{
Timeout: interval,
}
Expand All @@ -136,8 +118,6 @@ func InitPushWithConfig(cfg PushConfig) error {
pushDuration := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_duration_seconds{url=%q}`, pushURLRedacted))
pushBlockSize := pushMetrics.GetOrCreateHistogram(fmt.Sprintf(`metrics_push_block_size_bytes{url=%q}`, pushURLRedacted))
pushMetrics.GetOrCreateFloatCounter(fmt.Sprintf(`metrics_push_interval_seconds{url=%q}`, pushURLRedacted)).Set(interval.Seconds())

extraLabels := cfg.ExtraLabels
go func() {
ticker := time.NewTicker(interval)
var bb bytes.Buffer
Expand Down Expand Up @@ -166,17 +146,12 @@ func InitPushWithConfig(cfg PushConfig) error {
blockLen := bb.Len()
bytesPushedTotal.Add(blockLen)
pushBlockSize.Update(float64(blockLen))
req, err := http.NewRequest("GET", pushURL.String(), &bb)
req, err := http.NewRequest("GET", pushURL, &bb)
if err != nil {
panic(fmt.Errorf("BUG: metrics.push: cannot initialize request for metrics push to %q: %w", pushURLRedacted, err))
}
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Content-Encoding", "gzip")
for k, vs := range cfg.Headers {
for _, v := range vs {
req.Header.Add(k, v)
}
}
startTime := time.Now()
resp, err := c.Do(req)
pushDuration.UpdateDuration(startTime)
Expand All @@ -186,7 +161,7 @@ func InitPushWithConfig(cfg PushConfig) error {
continue
}
if resp.StatusCode/100 != 2 {
body, _ := io.ReadAll(resp.Body)
body, _ := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
log.Printf("ERROR: metrics.push: unexpected status code in response from %q: %d; expecting 2xx; response body: %q",
pushURLRedacted, resp.StatusCode, body)
Expand Down
73 changes: 0 additions & 73 deletions push_example_test.go

This file was deleted.

0 comments on commit cd448dd

Please sign in to comment.