-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add detected-fields command to logcli (#12739)
- Loading branch information
1 parent
587a6d2
commit 210ea93
Showing
6 changed files
with
211 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package detected | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"slices" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
|
||
"github.com/grafana/loki/v3/pkg/logcli/client" | ||
"github.com/grafana/loki/v3/pkg/loghttp" | ||
) | ||
|
||
type FieldsQuery struct { | ||
QueryString string | ||
Start time.Time | ||
End time.Time | ||
FieldLimit int | ||
LineLimit int | ||
Step time.Duration | ||
Quiet bool | ||
ColoredOutput bool | ||
} | ||
|
||
// DoQuery executes the query and prints out the results | ||
func (q *FieldsQuery) Do(c client.Client, outputMode string) { | ||
var resp *loghttp.DetectedFieldsResponse | ||
var err error | ||
|
||
resp, err = c.GetDetectedFields(q.QueryString, q.FieldLimit, q.LineLimit, q.Start, q.End, q.Step, q.Quiet) | ||
if err != nil { | ||
log.Fatalf("Error doing request: %+v", err) | ||
} | ||
|
||
switch outputMode { | ||
case "raw": | ||
out, err := json.Marshal(resp) | ||
if err != nil { | ||
log.Fatalf("Error marshalling response: %+v", err) | ||
} | ||
fmt.Println(string(out)) | ||
default: | ||
output := make([]string, len(resp.Fields)) | ||
for i, field := range resp.Fields { | ||
bold := color.New(color.Bold) | ||
output[i] = fmt.Sprintf("label: %s\t\t", bold.Sprintf("%s", field.Label)) + | ||
fmt.Sprintf("type: %s\t\t", bold.Sprintf("%s", field.Type)) + | ||
fmt.Sprintf("cardinality: %s", bold.Sprintf("%d", field.Cardinality)) | ||
} | ||
|
||
slices.Sort(output) | ||
fmt.Println(strings.Join(output, "\n")) | ||
} | ||
} |
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,14 @@ | ||
package loghttp | ||
|
||
import "github.com/grafana/loki/v3/pkg/logproto" | ||
|
||
// LabelResponse represents the http json response to a label query | ||
type DetectedFieldsResponse struct { | ||
Fields []DetectedField `json:"fields,omitempty"` | ||
} | ||
|
||
type DetectedField struct { | ||
Label string `json:"label,omitempty"` | ||
Type logproto.DetectedFieldType `json:"type,omitempty"` | ||
Cardinality uint64 `json:"cardinality,omitempty"` | ||
} |