Skip to content
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

add little filter capabilities #19

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ The tool provides an API endpoint, `/api/v1/metrics`, which returns the usage da
}
```

You can used the following query parameter to filter the list returned:

* **metric_name**: when used, it will trigger a fuzzy search on the metric_name based on the pattern provided.
* **used**: when used, will return only the metric used or not (depending if you set this boolean to true or to false). Leave it empty if you want both.

## How to use it

### Central instance
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-openapi/strfmt v0.23.0
github.com/grafana/grafana-openapi-client-go v0.0.0-20241101140420-bc381928ae6e
github.com/labstack/echo/v4 v4.12.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/perses/common v0.26.0
github.com/perses/perses v0.49.0-rc.1.0.20241104091321-0a0482386006
github.com/prometheus/client_golang v1.20.5
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+k
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down Expand Up @@ -392,6 +394,7 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
Expand Down
32 changes: 31 additions & 1 deletion source/metric/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/lithammer/fuzzysearch/fuzzy"
persesEcho "github.com/perses/common/echo"
"github.com/perses/metrics-usage/database"
v1 "github.com/perses/metrics-usage/pkg/api/v1"
Expand Down Expand Up @@ -49,8 +50,37 @@ func (e *endpoint) GetMetric(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, metric)
}

type request struct {
MetricName string `query:"metric_name"`
Used *bool `query:"used"`
}

func (r *request) filter(list map[string]*v1.Metric) map[string]*v1.Metric {
result := make(map[string]*v1.Metric)
if len(r.MetricName) == 0 && r.Used == nil {
return list
}
for k, v := range list {
if len(r.MetricName) == 0 || fuzzy.Match(r.MetricName, k) {
if r.Used == nil {
result[k] = v
} else if *r.Used && list[k].Usage != nil {
result[k] = v
} else if !*r.Used && list[k].Usage == nil {
result[k] = v
}
}
}
return result
}

func (e *endpoint) ListMetrics(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, e.db.ListMetrics())
req := &request{}
err := ctx.Bind(req)
if err != nil {
return ctx.JSON(http.StatusBadRequest, echo.Map{"message": err.Error()})
}
return ctx.JSON(http.StatusOK, req.filter(e.db.ListMetrics()))
}

func (e *endpoint) PushMetricsUsage(ctx echo.Context) error {
Expand Down