-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb84303
commit 33ead60
Showing
13 changed files
with
364 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,4 @@ func ParsePatternsQuery(r *http.Request) (*logproto.QueryPatternsRequest, error) | |
|
||
return req, nil | ||
} | ||
|
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,35 @@ | ||
package loghttp | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/grafana/loki/v3/pkg/logproto" | ||
) | ||
|
||
func ParseSamplesQuery(r *http.Request) (*logproto.QuerySamplesRequest, error) { | ||
req := &logproto.QuerySamplesRequest{} | ||
|
||
req.Query = query(r) | ||
start, end, err := bounds(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Start = start | ||
req.End = end | ||
|
||
calculatedStep, err := step(r, start, end) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if calculatedStep <= 0 { | ||
return nil, errZeroOrNegativeStep | ||
} | ||
// For safety, limit the number of returned points per timeseries. | ||
// This is sufficient for 60s resolution for a week or 1h resolution for a year. | ||
if (req.End.Sub(req.Start) / calculatedStep) > 11000 { | ||
return nil, errStepTooSmall | ||
} | ||
req.Step = calculatedStep.Milliseconds() | ||
|
||
return req, nil | ||
} |
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,122 @@ | ||
package loghttp | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/v3/pkg/logproto" | ||
) | ||
|
||
func TestParseSamplesQuery(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
want *logproto.QuerySamplesRequest | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "should correctly parse valid params", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=5s", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (5 * time.Second).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should default empty step param to sensible step for the range", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (14 * time.Second).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should default start to zero for empty start param", | ||
path: "/loki/api/v1/patterns?query={}&end=3600000000000", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(0, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (14 * time.Second).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should accept step with no units as seconds", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=10", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (10 * time.Second).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should accept step as string duration in seconds", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=15s", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (15 * time.Second).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should correctly parse long duration for step", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=10h", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(3600, 0), | ||
Step: (10 * time.Hour).Milliseconds(), | ||
}, | ||
}, | ||
{ | ||
name: "should reject negative step value", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=-5s", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "should reject very small step for big range", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=3600000000000&step=50ms", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "should accept very small step for small range", | ||
path: "/loki/api/v1/patterns?query={}&start=100000000000&end=110000000000&step=50ms", | ||
want: &logproto.QuerySamplesRequest{ | ||
Query: "{}", | ||
Start: time.Unix(100, 0), | ||
End: time.Unix(110, 0), | ||
Step: (50 * time.Millisecond).Milliseconds(), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req, err := http.NewRequest(http.MethodGet, tt.path, nil) | ||
require.NoError(t, err) | ||
err = req.ParseForm() | ||
require.NoError(t, err) | ||
|
||
got, err := ParseSamplesQuery(req) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
assert.Equalf(t, tt.want, got, "Incorrect response from input path: %s", tt.path) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.