Skip to content

Commit

Permalink
Add log filtering support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirage20 committed Jan 15, 2021
1 parent aecd19c commit 57ec905
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ The `aks-logs` tool will help you to query the Container logs running inside the
AKS Cluster name. Required
-containerName string
Container name of the Kubernetes workload. Required
-contains string
Filter log entries by list of contents if specified. Example: -contains=val1,val2
-endTime string
End time of the log entries. Default to (current time) if not specified. Format: 2006-01-02T15:04:05.999999999+07:00
-maxRecords int
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

GIT_REVISION=$(git rev-parse --short --verify HEAD)
TIME=$(date -u +%Y%m%d.%H%M%S)
VERSION=1.0.${GIT_REVISION}.${TIME}
VERSION=1.1.${GIT_REVISION}.${TIME}

build_artifacts () {
local os=$1
Expand Down
15 changes: 12 additions & 3 deletions cmd/aks-logs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"time"

"github.com/mirage20/aks-logs/pkg/client"
Expand All @@ -18,6 +19,7 @@ var (
namespace string
workloadName string
containerName string
contains string
startTime string
endTime string

Expand Down Expand Up @@ -64,9 +66,15 @@ func main() {
ContainerName: containerName,
StartTime: start,
EndTime: end,
MaxRecords: maxRecords,
ShowQuery: showQuery,
ShowDescending: showDescending,
FilterContains: func() []string {
if len(strings.TrimSpace(contains)) > 0 {
return strings.Split(contains, ",")
}
return nil
}(),
MaxRecords: maxRecords,
ShowQuery: showQuery,
ShowDescending: showDescending,
}
ctx := context.Background()
resp, err := c.Query(ctx, &req)
Expand Down Expand Up @@ -119,6 +127,7 @@ func init() {
flag.StringVar(&containerName, "containerName", "", "Container name of the Kubernetes workload. Required")
flag.StringVar(&startTime, "startTime", "", "Start time of the log entries. Default to (current time - 24h) if not specified. Format: 2006-01-02T15:04:05.999999999+07:00")
flag.StringVar(&endTime, "endTime", "", "End time of the log entries. Default to (current time) if not specified. Format: 2006-01-02T15:04:05.999999999+07:00")
flag.StringVar(&contains, "contains", "", "Filter log entries by list of contents if specified. Example: -contains=val1,val2")
flag.IntVar(&maxRecords, "maxRecords", 0, "Maximum number of log entries to query. Default to 1000")
flag.BoolVar(&showDescending, "showDescending", false, "Output the logs in descending order based on generated time")
flag.BoolVar(&showQuery, "showQuery", false, "Output the Kusto query")
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (c *Client) Query(ctx context.Context, qReq *QueryRequest) (*QueryResponse,
sb.WriteString(fmt.Sprintf("| distinct ContainerID;\n"))
sb.WriteString(fmt.Sprintf("ContainerLog\n"))
sb.WriteString(fmt.Sprintf("| where ContainerID in (ContainerIdList)\n"))
for _, v := range qReq.FilterContains {
sb.WriteString(fmt.Sprintf("| where LogEntry contains '%s'\n", v))
}
sb.WriteString(fmt.Sprintf("| where TimeGenerated between (datetime(%s)..datetime(%s))\n", qReq.StartTime.In(locUtc).Format(datetimeFormat), qReq.EndTime.In(locUtc).Format(datetimeFormat)))

if qReq.ShowDescending {
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type QueryRequest struct {
StartTime time.Time
EndTime time.Time

FilterContains []string

MaxRecords int
ShowQuery bool
ShowDescending bool
Expand Down

0 comments on commit 57ec905

Please sign in to comment.