File tree Expand file tree Collapse file tree 2 files changed +79
-5
lines changed Expand file tree Collapse file tree 2 files changed +79
-5
lines changed Original file line number Diff line number Diff line change @@ -72,20 +72,67 @@ def render_tag(record)
72
72
73
73
def get_key_accessors
74
74
key_accessors = { }
75
- keywords = @format . scan ( /\$ \{ ([\w \. \/ \- ]+)}/ )
75
+ keywords = @format . scan ( /\$ \{ ([\w \. \/ \- \\ ]+)}/ )
76
76
keywords . each do |key |
77
77
placeholder = "${#{ key [ 0 ] } }"
78
- if @key_prefix != ""
79
- path = @key_prefix + "." + key [ 0 ]
78
+ if contains_escaped_period? ( key [ 0 ] )
79
+ path = create_path_with_bracket_notation ( key [ 0 ] )
80
80
else
81
- path = key [ 0 ]
81
+ path = create_path ( key [ 0 ] )
82
82
end
83
- path = "$." + path
84
83
key_accessors [ placeholder ] = record_accessor_create ( path )
85
84
end
86
85
return key_accessors
87
86
end
88
87
88
+ private
89
+
90
+ def contains_escaped_period? ( key )
91
+ key . include? ( '\.' )
92
+ end
93
+
94
+ def create_path ( key )
95
+ if @key_prefix != ""
96
+ path = @key_prefix + "." + key
97
+ else
98
+ path = key
99
+ end
100
+ path = "$." + path
101
+ end
102
+
103
+ # Produces the output in bracket notation, e.g. $['kubernetes']['labels']['app.tier']"
104
+ def create_path_with_bracket_notation ( key )
105
+ path = split_by_unescaped_dots ( key )
106
+ . map { |elem | "['#{ elem } ']" }
107
+ . join ( "" )
108
+ if @key_prefix != ""
109
+ path = "['#{ @key_prefix } ']" + path
110
+ end
111
+ path = "$" + path
112
+ end
113
+
114
+ # Splits the input string by the . character if it is not escaped by \
115
+ def split_by_unescaped_dots ( text )
116
+ result = [ ]
117
+ current_part = ""
118
+ escaped = false
119
+
120
+ text . each_char do |char |
121
+ if char == "\\ " && !escaped
122
+ escaped = true
123
+ elsif char == "." && !escaped
124
+ result << current_part
125
+ current_part = ""
126
+ else
127
+ current_part += char
128
+ escaped = false
129
+ end
130
+ end
131
+
132
+ result << current_part
133
+ result
134
+ end
135
+
89
136
end
90
137
end
91
138
end
Original file line number Diff line number Diff line change @@ -21,10 +21,37 @@ class TagNormaliserOutputTest < Test::Unit::TestCase
21
21
d = create_driver ( config )
22
22
d . run ( default_tag : 'test' ) do
23
23
d . feed ( "tag1" , event_time , record . dup )
24
+ end
25
+ events = d . events
26
+ puts events
27
+
28
+ assert_equal ( "cluster.default.understood-butterfly-nginx-logging-demo-7dcdcfdcd7-h7p9n.nginx" , events [ 0 ] [ 0 ] )
29
+ end
30
+
31
+ test "escape_test" do
32
+ config = %[
33
+ format cluster.${namespace_name}.${labels.app}.${labels.app\\ .tier}.${labels.app\\ .kubernetes\\ .io/managed-by}
34
+ ]
35
+ record = {
36
+ "log" => "Example" ,
37
+ "kubernetes" => {
38
+ "pod_name" => "understood-butterfly-nginx-logging-demo-7dcdcfdcd7-h7p9n" ,
39
+ "namespace_name" => "default" ,
40
+ "labels" => {
41
+ "app" => "nginx" ,
42
+ "app.tier" => "frontend" ,
43
+ "app.kubernetes.io/managed-by" => "helm"
44
+ }
45
+ }
46
+ }
47
+ d = create_driver ( config )
48
+ d . run ( default_tag : 'test' ) do
24
49
d . feed ( "tag1" , event_time , record . dup )
25
50
end
26
51
events = d . events
27
52
puts events
53
+
54
+ assert_equal ( "cluster.default.nginx.frontend.helm" , events [ 0 ] [ 0 ] )
28
55
end
29
56
30
57
private
You can’t perform that action at this time.
0 commit comments