-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.go
48 lines (38 loc) · 1.21 KB
/
comment.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
package db
import (
"regexp"
"strings"
)
var commentRegex = regexp.MustCompile(`(((/\*)+?[\w\W]+?(\*/)+))`)
var postgresMonitorQueryCommentKeyValueString = "/* app:postgres-monitor-agent */"
var postgresMonitorQueryCommentString = " " + postgresMonitorQueryCommentKeyValueString
type ParsedComment struct {
Comment string
Query string
}
func parseComment(query string) *ParsedComment {
var comment string
// multiple comments could appear so we append them together
comments := commentRegex.FindAllString(query, -1)
for _, c := range comments {
query = strings.TrimSpace(strings.Replace(query, c, "", 1))
comment += c
}
// truncate comment if it's too big
if len(comment) > 1000 {
comment = comment[0:1000] + TruncatedString
}
return &ParsedComment{
Comment: comment,
Query: query,
}
}
// add a query comment so users can easily identify which
// queries are coming from the postgres monitor agent
func postgresMonitorQueryComment() string {
return postgresMonitorQueryCommentString
}
// a query is from the agent if it has the postgresMonitorQueryCommentString in the comment
func isAgentQueryComment(comment string) bool {
return strings.Contains(comment, postgresMonitorQueryCommentKeyValueString)
}