-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasjsonlogging.go
85 lines (73 loc) · 1.92 KB
/
hasjsonlogging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"cuddly-eureka-/types"
"cuddly-eureka-/util"
"encoding/json"
"strings"
)
type HasJSONLogging struct {
}
// hasJSONStrings returns true if there is at least 1 log
// in the JSON format
func hasJSONStrings(list []*string) bool {
var js json.RawMessage
totalValidStringsFound := 0
for _, log := range list {
err := json.Unmarshal([]byte(*log), &js)
if err == nil {
totalValidStringsFound += 1
// Can also simply break and return !!
}
}
if totalValidStringsFound > 0 {
return true
}
return false
}
// Check accepts logGroupLogStream and an optional slice of string pointers
// The first index of opts is the CWLQuery
// If the CWL query has not been passed, will use the passed log group and the log
// stream to fetch and parse the logs
func (j *HasJSONLogging) Check(logGroupLogStream string, opts ...*string) types.MaturityCheck {
inputSplit := strings.Split(logGroupLogStream, "_")
if len(inputSplit) < 2 {
return types.MaturityValue0
}
awsLogGroup := inputSplit[0]
awsLogStream := inputSplit[1]
var cwlQuery *string
if len(opts) > 0 {
cwlQuery = opts[0]
}
aws := util.AWSInit()
logs := make([]*string, 0)
if cwlQuery != nil {
// If the CWL Query has been passed, prioritize query to
// fetch the logs
queryLogs, queryErr := aws.GetLogsUsingQuery(awsLogGroup, *cwlQuery)
if queryErr != nil {
return types.MaturityValue0
}
logs = queryLogs
} else {
// If the CWL query has not been passed, use the log group
// and log stream to fetch the logs
logStream, err := aws.GetLogs(awsLogGroup, awsLogStream)
if err != nil {
return types.MaturityValue0
}
logs = logStream
}
if hasJSONStrings(logs) {
return types.MaturityValue2
}
return types.MaturityValue1
}
func (j *HasJSONLogging) Meta() types.MaturityMeta {
return types.MaturityMeta{
Type: types.MaturityObservability,
Name: "Has JSON logging",
EcrType: false,
}
}
var Check HasJSONLogging