Skip to content

Commit cdda1c8

Browse files
committed
Add rule information to query paramters sent to the QFE
Signed-off-by: SungJin1212 <[email protected]>
1 parent b4953a3 commit cdda1c8

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## master / unreleased
44

5+
* [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
56
* [FEATURE] Querier/Ruler: Add `query_partial_data` and `rules_partial_data` limits to allow queries/rules to be evaluated with data from a single zone, if other zones are not available. #6526
67

78
## 1.19.0 in progress

pkg/ruler/compat.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,20 @@ func EngineQueryFunc(engine promql.QueryEngine, frontendClient *frontendClient,
177177
}
178178

179179
if frontendClient != nil {
180-
v, err := frontendClient.InstantQuery(ctx, qs, t)
180+
// query parameters sent to the Query Frontend to leave rule information logs on query stats
181+
queryParams := map[string]string{}
182+
183+
if origin := ctx.Value(promql.QueryOrigin{}); origin != nil {
184+
queryLabels := origin.(map[string]interface{})
185+
rgMap := queryLabels["ruleGroup"].(map[string]string)
186+
queryParams["rule_group"] = rgMap["name"]
187+
queryParams["rule_namespace"] = rgMap["file"]
188+
}
189+
ruleDetail := rules.FromOriginContext(ctx)
190+
queryParams["rule"] = ruleDetail.Name
191+
queryParams["rule_kind"] = ruleDetail.Kind
192+
193+
v, err := frontendClient.InstantQuery(ctx, qs, t, queryParams)
181194
if err != nil {
182195
return nil, err
183196
}

pkg/ruler/frontend_client.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ func NewFrontendClient(client httpgrpc.HTTPClient, timeout time.Duration, promet
4949
}
5050
}
5151

52-
func (p *FrontendClient) makeRequest(ctx context.Context, qs string, ts time.Time) (*httpgrpc.HTTPRequest, error) {
52+
func (p *FrontendClient) makeRequest(ctx context.Context, qs string, ts time.Time, queryParams map[string]string) (*httpgrpc.HTTPRequest, error) {
5353
args := make(url.Values)
5454
args.Set("query", qs)
5555
if !ts.IsZero() {
5656
args.Set("time", ts.Format(time.RFC3339Nano))
5757
}
58+
// set query parameters sent to the Query Frontend to leave rule information logs on query stats
59+
for k, v := range queryParams {
60+
args.Set(k, v)
61+
}
62+
5863
body := []byte(args.Encode())
5964

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

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

94-
req, err := p.makeRequest(ctx, qs, t)
99+
req, err := p.makeRequest(ctx, qs, t, queryParams)
95100
if err != nil {
96101
level.Error(log).Log("err", err, "query", qs)
97102
return nil, err

pkg/ruler/frontend_client_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestTimeout(t *testing.T) {
3232
ctx := context.Background()
3333
ctx = user.InjectOrgID(ctx, "userID")
3434
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "json")
35-
_, err := frontendClient.InstantQuery(ctx, "query", time.Now())
35+
_, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
3636
require.Equal(t, context.DeadlineExceeded, err)
3737
}
3838

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

@@ -152,7 +152,7 @@ func TestInstantQueryJsonCodec(t *testing.T) {
152152
ctx := context.Background()
153153
ctx = user.InjectOrgID(ctx, "userID")
154154
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "json")
155-
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now())
155+
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
156156
require.Equal(t, test.expected, vector)
157157
require.Equal(t, test.expectedErr, err)
158158
})
@@ -301,7 +301,7 @@ func TestInstantQueryProtoCodec(t *testing.T) {
301301
ctx := context.Background()
302302
ctx = user.InjectOrgID(ctx, "userID")
303303
frontendClient := NewFrontendClient(mockHTTPGRPCClient(mockClientFn), time.Second*5, "/prometheus", "protobuf")
304-
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now())
304+
vector, err := frontendClient.InstantQuery(ctx, "query", time.Now(), nil)
305305
require.Equal(t, test.expected, vector)
306306
require.Equal(t, test.expectedErr, err)
307307
})

0 commit comments

Comments
 (0)