-
Notifications
You must be signed in to change notification settings - Fork 199
Added matchers in listLokiLabelValues #380
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
rahulvijil
wants to merge
7
commits into
grafana:main
Choose a base branch
from
rahulvijil:list_loki_label_values/customize
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.
+42
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
306c1bf
Added matchers in listLokiLabelValues
rahulvijil 62086ae
Refactor
rahulvijil ad97797
Add optional arg in fetchData
rahulvijil e6df9cf
Add comments
rahulvijil 93e7029
Keep the generic method fetchData
rahulvijil 817796a
Call fetchLabels
rahulvijil fc166fd
Add matchers support to LabelNames as well
rahulvijil 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,15 +139,9 @@ func (c *Client) makeRequest(ctx context.Context, method, urlPath string, params | |
| return bytes.TrimSpace(bodyBytes), nil | ||
| } | ||
|
|
||
| // fetchData is a generic method to fetch data from Loki API | ||
| func (c *Client) fetchData(ctx context.Context, urlPath string, startRFC3339, endRFC3339 string) ([]string, error) { | ||
| params := url.Values{} | ||
| if startRFC3339 != "" { | ||
| params.Add("start", startRFC3339) | ||
| } | ||
| if endRFC3339 != "" { | ||
| params.Add("end", endRFC3339) | ||
| } | ||
| func (c *Client) handleAPICall(ctx context.Context, params url.Values, urlPath string) ([]string, error) { | ||
| fullURL := fmt.Sprintf("%s?%s", urlPath, params.Encode()) | ||
| fmt.Println("Final request URL:", fullURL) | ||
|
|
||
| bodyBytes, err := c.makeRequest(ctx, "GET", urlPath, params) | ||
| if err != nil { | ||
|
|
@@ -174,9 +168,43 @@ func (c *Client) fetchData(ctx context.Context, urlPath string, startRFC3339, en | |
| return []string{}, nil | ||
| } | ||
|
|
||
| pretty, _ := json.MarshalIndent(labelResponse, "", " ") | ||
| fmt.Println("Loki Full Response:\n", string(pretty)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Println also needs removing |
||
|
|
||
| return labelResponse.Data, nil | ||
| } | ||
|
|
||
| // fetchData is a generic method to fetch data from Loki API | ||
| func (c *Client) fetchLabels(ctx context.Context, urlPath string, startRFC3339, endRFC3339 string, matchers ...string) ([]string, error) { | ||
| params := url.Values{} | ||
| var matcher string | ||
| if len(matchers) > 0 { | ||
| matcher = matchers[0] | ||
| } | ||
| if startRFC3339 != "" { | ||
| params.Add("start", startRFC3339) | ||
| } | ||
| if endRFC3339 != "" { | ||
| params.Add("end", endRFC3339) | ||
| } | ||
| if matcher != "" { | ||
| params.Add("query", matcher) | ||
| } | ||
| return c.handleAPICall(ctx, params, urlPath) | ||
| } | ||
|
|
||
| // fetchData is a generic method to fetch data from Loki API | ||
| func (c *Client) fetchData(ctx context.Context, urlPath string, startRFC3339, endRFC3339 string) ([]string, error) { | ||
| params := url.Values{} | ||
| if startRFC3339 != "" { | ||
| params.Add("start", startRFC3339) | ||
| } | ||
| if endRFC3339 != "" { | ||
| params.Add("end", endRFC3339) | ||
| } | ||
| return c.handleAPICall(ctx, params, urlPath) | ||
| } | ||
|
|
||
| func NewAuthRoundTripper(rt http.RoundTripper, accessToken, idToken, apiKey string, basicAuth *url.Userinfo) *authRoundTripper { | ||
| return &authRoundTripper{ | ||
| accessToken: accessToken, | ||
|
|
@@ -219,6 +247,7 @@ type ListLokiLabelNamesParams struct { | |
| DatasourceUID string `json:"datasourceUid" jsonschema:"required,description=The UID of the datasource to query"` | ||
| StartRFC3339 string `json:"startRfc3339,omitempty" jsonschema:"description=Optionally\\, the start time of the query in RFC3339 format (defaults to 1 hour ago)"` | ||
| EndRFC3339 string `json:"endRfc3339,omitempty" jsonschema:"description=Optionally\\, the end time of the query in RFC3339 format (defaults to now)"` | ||
| Matchers string `json:"matchers,omitempty" jsonschema:"description=Optionally\\, the optional filters such as app, namespace wrapped up with curly braces {"app"="<app-name>", "namespace"="<namespace>"} (defaults to {})"` | ||
| } | ||
|
|
||
| // listLokiLabelNames lists all label names in a Loki datasource | ||
|
|
@@ -228,7 +257,7 @@ func listLokiLabelNames(ctx context.Context, args ListLokiLabelNamesParams) ([]s | |
| return nil, fmt.Errorf("creating Loki client: %w", err) | ||
| } | ||
|
|
||
| result, err := client.fetchData(ctx, "/loki/api/v1/labels", args.StartRFC3339, args.EndRFC3339) | ||
| result, err := client.fetchLabels(ctx, "/loki/api/v1/labels", args.StartRFC3339, args.EndRFC3339, args.Matchers) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -256,6 +285,7 @@ type ListLokiLabelValuesParams struct { | |
| LabelName string `json:"labelName" jsonschema:"required,description=The name of the label to retrieve values for (e.g. 'app'\\, 'env'\\, 'pod')"` | ||
| StartRFC3339 string `json:"startRfc3339,omitempty" jsonschema:"description=Optionally\\, the start time of the query in RFC3339 format (defaults to 1 hour ago)"` | ||
| EndRFC3339 string `json:"endRfc3339,omitempty" jsonschema:"description=Optionally\\, the end time of the query in RFC3339 format (defaults to now)"` | ||
| Matchers string `json:"matchers,omitempty" jsonschema:"description=Optionally\\, the optional filters such as app, namespace wrapped up with curly braces {"app"="<app-name>", "namespace"="<namespace>"} (defaults to {})"` | ||
| } | ||
|
|
||
| // listLokiLabelValues lists all values for a specific label in a Loki datasource | ||
|
|
@@ -265,10 +295,10 @@ func listLokiLabelValues(ctx context.Context, args ListLokiLabelValuesParams) ([ | |
| return nil, fmt.Errorf("creating Loki client: %w", err) | ||
| } | ||
|
|
||
| // Use the client's fetchData method | ||
| urlPath := fmt.Sprintf("/loki/api/v1/label/%s/values", args.LabelName) | ||
|
|
||
| result, err := client.fetchData(ctx, urlPath, args.StartRFC3339, args.EndRFC3339) | ||
| // Use the client's fetchData method | ||
| result, err := client.fetchLabels(ctx, urlPath, args.StartRFC3339, args.EndRFC3339, args.Matchers) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this Println?