Skip to content

Commit

Permalink
feat: parse detected fields in querier
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorwhitney committed Apr 1, 2024
1 parent 0eaa0b4 commit d4a57c4
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 241 deletions.
13 changes: 0 additions & 13 deletions pkg/loghttp/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,3 @@ func ParseLabelQuery(r *http.Request) (*logproto.LabelRequest, error) {
return req, nil
}

func ParseDetectedFieldsQuery(r *http.Request) (*logproto.DetectedFieldsRequest, error) {
req := &logproto.DetectedFieldsRequest{}

start, end, err := bounds(r)
if err != nil {
return nil, err
}
req.Start = &start
req.End = &end

req.Query = query(r)
return req, nil
}
23 changes: 23 additions & 0 deletions pkg/loghttp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const (
defaultQueryLimit = 100
defaultFieldLimit = 1000
defaultSince = 1 * time.Hour
defaultDirection = logproto.BACKWARD
)
Expand All @@ -34,6 +35,28 @@ func limit(r *http.Request) (uint32, error) {
return uint32(l), nil
}

func lineLimit(r *http.Request) (uint32, error) {
l, err := parseInt(r.Form.Get("line_limit"), defaultQueryLimit)
if err != nil {
return 0, err
}
if l <= 0 {
return 0, errors.New("limit must be a positive value")
}
return uint32(l), nil
}

func fieldLimit(r *http.Request) (uint32, error) {
l, err := parseInt(r.Form.Get("field_limit"), defaultFieldLimit)
if err != nil {
return 0, err
}
if l <= 0 {
return 0, errors.New("limit must be a positive value")
}
return uint32(l), nil
}

func query(r *http.Request) string {
return r.Form.Get("query")
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/loghttp/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,93 @@ func ParseVolumeRangeQuery(r *http.Request) (*VolumeRangeQuery, error) {
}, nil
}

func ParseDetectedFieldsQuery(r *http.Request) (*logproto.DetectedFieldsRequest, error) {
var err error
result := &logproto.DetectedFieldsRequest{}

result.Query = query(r)
result.Start, result.End, err = bounds(r)
if err != nil {
return nil, err
}

if result.End.Before(result.Start) {
return nil, errEndBeforeStart
}

result.LineLimit, err = lineLimit(r)
if err != nil {
return nil, err
}

result.FieldLimit, err = fieldLimit(r)
if err != nil {
return nil, err
}

step, err := step(r, result.Start, result.End)
result.Step = step.Milliseconds()
if err != nil {
return nil, err
}

if result.Step <= 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 (result.End.Sub(result.Start) / step) > 11000 {
return nil, errStepTooSmall
}
return result, nil
}

// type DetectedFieldsQuery struct {
// Start time.Time
// End time.Time
// Step time.Duration
// Query string
// Limit uint32
// }

// func ParseDetectedFieldsQuery(r *http.Request) (*DetectedFieldsQuery, error) {
// var result DetectedFieldsQuery
// var err error

// result.Query = query(r)
// result.Start, result.End, err = bounds(r)
// if err != nil {
// return nil, err
// }

// if result.End.Before(result.Start) {
// return nil, errEndBeforeStart
// }

// result.Limit, err = limit(r)
// if err != nil {
// return nil, err
// }

// result.Step, err = step(r, result.Start, result.End)
// if err != nil {
// return nil, err
// }

// if result.Step <= 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 (result.End.Sub(result.Start) / result.Step) > 11000 {
// return nil, errStepTooSmall
// }

// return &result, nil
// }

func targetLabels(r *http.Request) []string {
lbls := strings.Split(r.Form.Get("targetLabels"), ",")
if (len(lbls) == 1 && lbls[0] == "") || len(lbls) == 0 {
Expand Down
510 changes: 305 additions & 205 deletions pkg/logproto/logproto.pb.go

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions pkg/logproto/logproto.proto
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,16 @@ message Volume {
message DetectedFieldsRequest {
google.protobuf.Timestamp start = 1 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = true
(gogoproto.nullable) = false
];
google.protobuf.Timestamp end = 2 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = true
(gogoproto.nullable) = false
];
string query = 3; // Naming this query instead of match because this should be with queryrangebase.Request interface
uint32 lineLimit = 4;
uint32 fieldLimit = 5;
int64 step = 6;
}

message DetectedFieldsResponse {
Expand All @@ -441,8 +444,6 @@ message DetectedFieldsResponse {

message DetectedField {
string label = 1;
string type = 2 [
(gogoproto.casttype) = "DetectedFieldType"
];
string type = 2 [(gogoproto.casttype) = "DetectedFieldType"];
uint64 cardinality = 3;
}
Loading

0 comments on commit d4a57c4

Please sign in to comment.