-
Notifications
You must be signed in to change notification settings - Fork 180
test: validate metrics compatibility with Grafana scrapper #6150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hanabi1224
wants to merge
3
commits into
main
Choose a base branch
from
hm/metrics-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ go 1.25.2 | |
use ( | ||
./f3-sidecar | ||
./interop-tests/src/tests/go_app | ||
./tools/prometheus_metrics_validator | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### Unit tests | ||
|
||
- run `go test -v .` | ||
|
||
### Validate Forest metrics | ||
|
||
```bash | ||
wget http://localhost:6116/metrics -O metrics.txt | ||
go run . metrics.txt | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
metrics | ||
metrics.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module premetheus_metrics_validator/v2 | ||
|
||
go 1.25.2 | ||
|
||
require ( | ||
github.com/prometheus/prometheus v0.306.0 | ||
github.com/stretchr/testify v1.11.1 | ||
github.com/urfave/cli/v3 v3.4.1 | ||
) | ||
|
||
require ( | ||
github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/grafana/regexp v0.0.0-20250905093917-f7b3be9d1853 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/prometheus/client_model v0.6.2 // indirect | ||
github.com/prometheus/common v0.67.1 // indirect | ||
go.yaml.in/yaml/v2 v2.4.3 // indirect | ||
golang.org/x/text v0.30.0 // indirect | ||
google.golang.org/protobuf v1.36.10 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/model/textparse" | ||
"github.com/urfave/cli/v3" | ||
) | ||
|
||
func main() { | ||
cmd := &cli.Command{ | ||
Name: "prometheus-metrics-validator", | ||
Usage: "A tool for validating the compatibility of Prometheus metrics text files with Grafana scrapers.", | ||
Arguments: []cli.Argument{ | ||
&cli.StringArg{ | ||
Name: "file", | ||
UsageText: "Path to the Prometheus metrics text file", | ||
}, | ||
}, | ||
Action: func(ctx context.Context, c *cli.Command) error { | ||
path := c.StringArg("file") | ||
metrics, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
if err = Validate(metrics); err != nil { | ||
fmt.Printf("Validation failed: %v\n", err) | ||
os.Exit(1) | ||
} else { | ||
fmt.Println("Validation passed!") | ||
} | ||
return nil | ||
}, | ||
} | ||
if err := cmd.Run(context.Background(), os.Args); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func Validate(metrics []byte) error { | ||
p, err := textparse.New(metrics, "text/plain", "", false, false, true, labels.NewSymbolTable()) | ||
if err != nil { | ||
return err | ||
} | ||
for { | ||
if _, err := p.Next(); err != nil { | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} else { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateWithoutEOF(t *testing.T) { | ||
metric := []byte(` | ||
cache_zstd_frame_0_size_bytes 268422051 | ||
zstd_frame_0_len 18022 | ||
zstd_frame_0_cap -1 | ||
`) | ||
e := Validate(metric) | ||
require.NoError(t, e) | ||
} | ||
|
||
func TestValidateWithEOF(t *testing.T) { | ||
metric := []byte(` | ||
# HELP cache_zstd_frame_0_size_bytes Size of LruCache zstd_frame_0 in bytes | ||
# TYPE cache_zstd_frame_0_size_bytes gauge | ||
# UNIT cache_zstd_frame_0_size_bytes bytes | ||
cache_zstd_frame_0_size_bytes 268422051 | ||
# HELP zstd_frame_0_len Length of LruCache zstd_frame_0 | ||
# TYPE zstd_frame_0_len gauge | ||
zstd_frame_0_len 18022 | ||
# HELP zstd_frame_0_cap Capacity of LruCache zstd_frame_0 | ||
# TYPE zstd_frame_0_cap gauge | ||
zstd_frame_0_cap -1 | ||
# EOF | ||
`) | ||
e := Validate(metric) | ||
require.NoError(t, e) | ||
} | ||
|
||
func TestValidateWithInvalidCharacters(t *testing.T) { | ||
metric := []byte(` | ||
cache_zstd_frame_0_size_bytes 268422051 | ||
zstd_frame_0_len 18022 | ||
zstd_frame_0_<cap> -1 | ||
`) | ||
e := Validate(metric) | ||
require.ErrorContains(t, e, "unsupported character") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.