2023-04-27 11:23:52 +00:00
|
|
|
// Copyright 2023 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/snappy"
|
|
|
|
config_util "github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage/remote"
|
2023-05-23 08:29:17 +00:00
|
|
|
"github.com/prometheus/prometheus/util/fmtutil"
|
2023-04-27 11:23:52 +00:00
|
|
|
)
|
|
|
|
|
lint: Revamp our linting rules, mostly around doc comments
Several things done here:
- Set `max-issues-per-linter` to 0 so that we actually see all linter
warnings and not just 50 per linter. (As we also set
`max-same-issues` to 0, I assume this was the intention from the
beginning.)
- Stop using the golangci-lint default excludes (by setting
`exclude-use-default: false`. Those are too generous and don't match
our style conventions. (I have re-added some of the excludes
explicitly in this commit. See below.)
- Re-add the `errcheck` exclusion we have used so far via the
defaults.
- Exclude the signature requirement `govet` has for `Seek` methods
because we use non-standard `Seek` methods a lot. (But we keep other
requirements, while the default excludes completely disabled the
check for common method segnatures.)
- Exclude warnings about missing doc comments on exported symbols. (We
used to be pretty adamant about doc comments, but stopped that at
some point in the past. By now, we have about 500 missing doc
comments. We may consider reintroducing this check, but that's
outside of the scope of this commit. The default excludes of
golangci-lint essentially ignore doc comments completely.)
- By stop using the default excludes, we now get warnings back on
malformed doc comments. That's the most impactful change in this
commit. It does not enforce doc comments (again), but _if_ there is
a doc comment, it has to have the recommended form. (Most of the
changes in this commit are fixing this form.)
- Improve wording/spelling of some comments in .golangci.yml, and
remove an outdated comment.
- Leave `package-comments` inactive, but add a TODO asking if we
should change that.
- Add a new sub-linter `comment-spacings` (and fix corresponding
comments), which avoids missing spaces after the leading `//`.
Signed-off-by: beorn7 <beorn@grafana.com>
2024-08-22 11:59:36 +00:00
|
|
|
// PushMetrics to a prometheus remote write (for testing purpose only).
|
2023-05-24 08:55:49 +00:00
|
|
|
func PushMetrics(url *url.URL, roundTripper http.RoundTripper, headers map[string]string, timeout time.Duration, labels map[string]string, files ...string) int {
|
2023-04-27 11:23:52 +00:00
|
|
|
addressURL, err := url.Parse(url.String())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return failureExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// build remote write client
|
|
|
|
writeClient, err := remote.NewWriteClient("remote-write", &remote.ClientConfig{
|
|
|
|
URL: &config_util.URL{URL: addressURL},
|
|
|
|
Timeout: model.Duration(timeout),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
return failureExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// set custom tls config from httpConfigFilePath
|
|
|
|
// set custom headers to every request
|
|
|
|
client, ok := writeClient.(*remote.Client)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(os.Stderr, fmt.Errorf("unexpected type %T", writeClient))
|
|
|
|
return failureExitCode
|
|
|
|
}
|
|
|
|
client.Client.Transport = &setHeadersTransport{
|
|
|
|
RoundTripper: roundTripper,
|
|
|
|
headers: headers,
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:28:55 +00:00
|
|
|
var data []byte
|
|
|
|
var failed bool
|
2023-05-23 08:29:17 +00:00
|
|
|
|
2023-06-01 08:28:55 +00:00
|
|
|
if len(files) == 0 {
|
|
|
|
data, err = io.ReadAll(os.Stdin)
|
2023-04-27 11:23:52 +00:00
|
|
|
if err != nil {
|
2023-05-24 08:55:49 +00:00
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
2023-06-01 08:28:55 +00:00
|
|
|
return failureExitCode
|
2023-04-27 11:23:52 +00:00
|
|
|
}
|
2023-06-01 08:28:55 +00:00
|
|
|
fmt.Printf("Parsing standard input\n")
|
|
|
|
if parseAndPushMetrics(client, data, labels) {
|
|
|
|
fmt.Printf(" SUCCESS: metrics pushed to remote write.\n")
|
|
|
|
return successExitCode
|
2023-04-27 11:23:52 +00:00
|
|
|
}
|
2023-06-01 08:28:55 +00:00
|
|
|
return failureExitCode
|
|
|
|
}
|
2023-04-27 11:23:52 +00:00
|
|
|
|
2023-06-01 08:28:55 +00:00
|
|
|
for _, file := range files {
|
|
|
|
data, err = os.ReadFile(file)
|
2023-04-27 11:23:52 +00:00
|
|
|
if err != nil {
|
2023-05-24 08:55:49 +00:00
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
2023-04-27 11:23:52 +00:00
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
2023-05-24 08:55:49 +00:00
|
|
|
|
2023-06-01 08:28:55 +00:00
|
|
|
fmt.Printf("Parsing metrics file %s\n", file)
|
|
|
|
if parseAndPushMetrics(client, data, labels) {
|
|
|
|
fmt.Printf(" SUCCESS: metrics file %s pushed to remote write.\n", file)
|
|
|
|
continue
|
2023-05-24 08:55:49 +00:00
|
|
|
}
|
2023-06-01 08:28:55 +00:00
|
|
|
failed = true
|
2023-04-27 11:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
return failureExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
return successExitCode
|
|
|
|
}
|
|
|
|
|
2024-07-19 17:53:40 +00:00
|
|
|
// TODO(bwplotka): Add PRW 2.0 support.
|
2023-06-01 08:28:55 +00:00
|
|
|
func parseAndPushMetrics(client *remote.Client, data []byte, labels map[string]string) bool {
|
2023-06-27 07:30:39 +00:00
|
|
|
metricsData, err := fmtutil.MetricTextToWriteRequest(bytes.NewReader(data), labels)
|
2023-06-01 08:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := metricsData.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the request body into snappy encoding.
|
|
|
|
compressed := snappy.Encode(nil, raw)
|
2024-07-19 17:53:40 +00:00
|
|
|
_, err = client.Store(context.Background(), compressed, 0)
|
2023-06-01 08:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:23:52 +00:00
|
|
|
type setHeadersTransport struct {
|
|
|
|
http.RoundTripper
|
|
|
|
headers map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setHeadersTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
for key, value := range s.headers {
|
|
|
|
req.Header.Set(key, value)
|
|
|
|
}
|
|
|
|
return s.RoundTripper.RoundTrip(req)
|
|
|
|
}
|