Skip to content
Open
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
54 changes: 42 additions & 12 deletions tools/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

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?


bodyBytes, err := c.makeRequest(ctx, "GET", urlPath, params)
if err != nil {
Expand All @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down