Skip to content

Commit

Permalink
Add rule information to query paramters sent to the QFE
Browse files Browse the repository at this point in the history
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
  • Loading branch information
SungJin1212 committed Jan 23, 2025
1 parent e3c4990 commit 8ea9f15
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master / unreleased

* [ENHANCEMENT] Ruler: Add rule information (group name, namespace, name, and kind) to query parameters sent to the Query Frontend to leave rule information logs on query stats. #6539

## 1.19.0 in progress

* [CHANGE] Deprecate `-blocks-storage.tsdb.wal-compression-enabled` flag (use `blocks-storage.tsdb.wal-compression-type` instead). #6529
Expand Down
15 changes: 14 additions & 1 deletion pkg/ruler/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,20 @@ func EngineQueryFunc(engine promql.QueryEngine, frontendClient *frontendClient,
}

if frontendClient != nil {
v, err := frontendClient.InstantQuery(ctx, qs, t)
// query parameters sent to the Query Frontend to leave rule information logs on query stats
queryParams := map[string]string{}

if origin := ctx.Value(promql.QueryOrigin{}); origin != nil {
queryLabels := origin.(map[string]interface{})
rgMap := queryLabels["ruleGroup"].(map[string]string)
queryParams["rule_group"] = rgMap["name"]
queryParams["rule_namespace"] = rgMap["file"]
}
ruleDetail := rules.FromOriginContext(ctx)
queryParams["rule"] = ruleDetail.Name
queryParams["rule_kind"] = ruleDetail.Kind

v, err := frontendClient.InstantQuery(ctx, qs, t, queryParams)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/ruler/frontend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ func NewFrontendClient(client httpgrpc.HTTPClient, timeout time.Duration, promet
}
}

func (p *FrontendClient) makeRequest(ctx context.Context, qs string, ts time.Time) (*httpgrpc.HTTPRequest, error) {
func (p *FrontendClient) makeRequest(ctx context.Context, qs string, ts time.Time, queryParams map[string]string) (*httpgrpc.HTTPRequest, error) {
args := make(url.Values)
args.Set("query", qs)
if !ts.IsZero() {
args.Set("time", ts.Format(time.RFC3339Nano))
}
// set query parameters sent to the Query Frontend to leave rule information logs on query stats
for k, v := range queryParams {
args.Set(k, v)
}

body := []byte(args.Encode())

//lint:ignore faillint wrapper around upstream method
Expand Down Expand Up @@ -87,11 +92,11 @@ func (p *FrontendClient) makeRequest(ctx context.Context, qs string, ts time.Tim
return req, nil
}

func (p *FrontendClient) InstantQuery(ctx context.Context, qs string, t time.Time) (promql.Vector, error) {
func (p *FrontendClient) InstantQuery(ctx context.Context, qs string, t time.Time, queryParams map[string]string) (promql.Vector, error) {
log, ctx := spanlogger.New(ctx, "FrontendClient.InstantQuery")
defer log.Span.Finish()

req, err := p.makeRequest(ctx, qs, t)
req, err := p.makeRequest(ctx, qs, t, queryParams)
if err != nil {
level.Error(log).Log("err", err, "query", qs)
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/ruler/frontend_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestTimeout(t *testing.T) {
ctx := context.Background()
ctx = user.InjectOrgID(ctx, "userID")
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "json")
_, err := frontendClient.InstantQuery(ctx, "query", time.Now())
_, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
require.Equal(t, context.DeadlineExceeded, err)
}

Expand All @@ -41,7 +41,7 @@ func TestNoOrgId(t *testing.T) {
return nil, nil
}
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "json")
_, err := frontendClient.InstantQuery(context.Background(), "query", time.Now())
_, err := frontendClient.InstantQuery(context.Background(), "query", time.Now(), nil)
require.Equal(t, user.ErrNoOrgID, err)
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func TestInstantQueryJsonCodec(t *testing.T) {
ctx := context.Background()
ctx = user.InjectOrgID(ctx, "userID")
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "json")
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now())
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
require.Equal(t, test.expected, vector)
require.Equal(t, test.expectedErr, err)
})
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestInstantQueryProtoCodec(t *testing.T) {
ctx := context.Background()
ctx = user.InjectOrgID(ctx, "userID")
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "protobuf")
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now())
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
require.Equal(t, test.expected, vector)
require.Equal(t, test.expectedErr, err)
})
Expand Down

0 comments on commit 8ea9f15

Please sign in to comment.