-
Notifications
You must be signed in to change notification settings - Fork 850
Querier active api tracker #7216
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
base: master
Are you sure you want to change the base?
Changes from all commits
c399131
0bd714e
3b566eb
83e2259
d06e5eb
575b518
2b585b7
5da5eeb
814d788
eeeaf01
72a8720
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package request_tracker | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/util/requestmeta" | ||
| "github.com/cortexproject/cortex/pkg/util/users" | ||
| ) | ||
|
|
||
| type Extractor interface { | ||
| Extract(r *http.Request) []byte | ||
| } | ||
|
|
||
| type DefaultExtractor struct{} | ||
|
|
||
| type ApiExtractor struct{} | ||
|
|
||
| type InstantQueryExtractor struct{} | ||
|
|
||
| type RangedQueryExtractor struct{} | ||
|
|
||
| func generateCommonMap(r *http.Request) map[string]interface{} { | ||
| ctx := r.Context() | ||
| entryMap := make(map[string]interface{}) | ||
| entryMap["timestamp-sec"] = time.Now().Unix() | ||
| entryMap["Path"] = r.URL.Path | ||
| entryMap["Method"] = r.Method | ||
| entryMap["TenantID"], _ = users.TenantID(ctx) | ||
| entryMap["RequestID"] = requestmeta.RequestIdFromContext(ctx) | ||
| entryMap["UserAgent"] = r.Header.Get("User-Agent") | ||
| entryMap["DashboardUID"] = r.Header.Get("X-Dashboard-UID") | ||
| entryMap["PanelId"] = r.Header.Get("X-Panel-Id") | ||
|
|
||
| return entryMap | ||
| } | ||
|
|
||
| func (e *DefaultExtractor) Extract(r *http.Request) []byte { | ||
| entryMap := generateCommonMap(r) | ||
|
|
||
| return generateJSONEntry(entryMap) | ||
| } | ||
|
|
||
| func (e *ApiExtractor) Extract(r *http.Request) []byte { | ||
| entryMap := generateCommonMap(r) | ||
| entryMap["limit"] = r.URL.Query().Get("limit") | ||
| entryMap["start"] = r.URL.Query().Get("start") | ||
| entryMap["end"] = r.URL.Query().Get("end") | ||
|
|
||
| matches := r.URL.Query()["match[]"] | ||
| entryMap["number-of-matches"] = len(matches) | ||
| matchesStr := strings.Join(matches, ",") | ||
|
|
||
| return generateJSONEntryWithTruncatedField(entryMap, "matches", matchesStr) | ||
| } | ||
|
|
||
| func (e *InstantQueryExtractor) Extract(r *http.Request) []byte { | ||
| entryMap := generateCommonMap(r) | ||
| entryMap["time"] = r.URL.Query().Get("time") | ||
| return generateJSONEntryWithTruncatedField(entryMap, "query", r.URL.Query().Get("query")) | ||
| } | ||
|
|
||
| func (e *RangedQueryExtractor) Extract(r *http.Request) []byte { | ||
| entryMap := generateCommonMap(r) | ||
| entryMap["start"] = r.URL.Query().Get("start") | ||
| entryMap["end"] = r.URL.Query().Get("end") | ||
| entryMap["step"] = r.URL.Query().Get("step") | ||
| return generateJSONEntryWithTruncatedField(entryMap, "query", r.URL.Query().Get("query")) | ||
| } | ||
|
|
||
| func generateJSONEntry(entryMap map[string]interface{}) []byte { | ||
| jsonEntry, err := json.Marshal(entryMap) | ||
|
Contributor
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. Can we make sure that the generated byte entries we always log queries and matchers at the end? If they are too long other parameters might be truncated
Contributor
Author
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. Were always trimming the query/matchers to fit the rest of the space using |
||
| if err != nil { | ||
| return []byte{} | ||
| } | ||
|
|
||
| return jsonEntry | ||
| } | ||
|
|
||
| func generateJSONEntryWithTruncatedField(entryMap map[string]interface{}, fieldName, fieldValue string) []byte { | ||
| entryMap[fieldName] = "" | ||
| minEntryJSON := generateJSONEntry(entryMap) | ||
| entryMap[fieldName] = trimForJsonMarshal(fieldValue, maxEntrySize-(len(minEntryJSON)+1)) | ||
| return generateJSONEntry(entryMap) | ||
| } | ||
|
|
||
| func trimStringByBytes(bytesStr []byte, size int) string { | ||
| trimIndex := len(bytesStr) | ||
| if size < len(bytesStr) { | ||
| for !utf8.RuneStart(bytesStr[size]) { | ||
| size-- | ||
| } | ||
| trimIndex = size | ||
| } | ||
|
|
||
| return string(bytesStr[:trimIndex]) | ||
| } | ||
|
|
||
| func trimForJsonMarshal(field string, size int) string { | ||
| fieldValueEncoded, err := json.Marshal(field) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| fieldValueEncoded = fieldValueEncoded[1 : len(fieldValueEncoded)-1] | ||
| fieldValueEncodedTrimmed := trimStringByBytes(fieldValueEncoded, size) | ||
| fieldValueEncodedTrimmed = "\"" + removeHalfCutEscapeChar(fieldValueEncodedTrimmed) + "\"" | ||
Check failureCode scanning / CodeQL Potentially unsafe quoting Critical
If this
JSON value Error loading related location Loading |
||
| var fieldValue string | ||
| print(fieldValueEncodedTrimmed) | ||
| json.Unmarshal([]byte(fieldValueEncodedTrimmed), &fieldValue) | ||
|
|
||
| return fieldValue | ||
| } | ||
|
|
||
| func removeHalfCutEscapeChar(str string) string { | ||
| trailingBashslashCount := len(str) - len(strings.TrimRight(str, "\\")) | ||
| if trailingBashslashCount%2 == 1 { | ||
| str = str[0 : len(str)-1] | ||
| } | ||
| return str | ||
| } | ||
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.
We should probably add more headers including user agent, dashboard ID and panel ID