-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logcli: added --step support to query command (#1103)
* logcli: added --step support to query command * Changed query range API default step value from 1s to a dynamic value based on start/end input params
- Loading branch information
1 parent
7ac9e4a
commit 96237b5
Showing
6 changed files
with
124 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package querier | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHttp_defaultQueryRangeStep(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
start time.Time | ||
end time.Time | ||
expected int | ||
}{ | ||
"should not be lower then 1s": { | ||
start: time.Unix(60, 0), | ||
end: time.Unix(60, 0), | ||
expected: 1, | ||
}, | ||
"should return 1s if input time range is 5m": { | ||
start: time.Unix(60, 0), | ||
end: time.Unix(360, 0), | ||
expected: 1, | ||
}, | ||
"should return 14s if input time range is 1h": { | ||
start: time.Unix(60, 0), | ||
end: time.Unix(3660, 0), | ||
expected: 14, | ||
}, | ||
} | ||
|
||
for testName, testData := range tests { | ||
testData := testData | ||
|
||
t.Run(testName, func(t *testing.T) { | ||
assert.Equal(t, testData.expected, defaultQueryRangeStep(testData.start, testData.end)) | ||
}) | ||
} | ||
} | ||
|
||
func TestHttp_httpRequestToRangeQueryRequest(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
reqPath string | ||
expected *rangeQueryRequest | ||
}{ | ||
"should set the default step based on the input time range if the step parameter is not provided": { | ||
reqPath: "/loki/api/v1/query_range?query={}&start=0&end=3600000000000", | ||
expected: &rangeQueryRequest{ | ||
query: "{}", | ||
start: time.Unix(0, 0), | ||
end: time.Unix(3600, 0), | ||
step: 14 * time.Second, | ||
limit: 100, | ||
direction: logproto.BACKWARD, | ||
}, | ||
}, | ||
"should use the input step parameter if provided": { | ||
reqPath: "/loki/api/v1/query_range?query={}&start=0&end=3600000000000&step=5", | ||
expected: &rangeQueryRequest{ | ||
query: "{}", | ||
start: time.Unix(0, 0), | ||
end: time.Unix(3600, 0), | ||
step: 5 * time.Second, | ||
limit: 100, | ||
direction: logproto.BACKWARD, | ||
}, | ||
}, | ||
} | ||
|
||
for testName, testData := range tests { | ||
testData := testData | ||
|
||
t.Run(testName, func(t *testing.T) { | ||
req := httptest.NewRequest("GET", testData.reqPath, nil) | ||
actual, err := httpRequestToRangeQueryRequest(req) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, testData.expected, actual) | ||
}) | ||
} | ||
} |