Debian Codegraph @

tapset/linux/json.stpm

raw
1 // JSON tapset macros.
2 // Copyright (C) 2015 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8 //
9 // NB: tapsetdescription is in tapset/linux/json.stp
10
11 @define __json_output_metric_metadata(indent_str, name, pointer, type, description, units)
12 %(
13 $value .= sprintf("%s{\n", @indent_str)
14 $value .= sprintf("%s \"name\": \"%s\",\n", @indent_str, @name)
15 $value .= sprintf("%s \"pointer\": \"%s\",\n", @indent_str, @pointer)
16 $value .= sprintf("%s \"type\": \"%s\"", @indent_str, @type)
17 if (strlen(@description) > 0) {
18 $value .= sprintf(",\n%s \"description\": \"%s\"", @indent_str,
19 @description)
20 }
21 if (strlen(@units) > 0) {
22 $value .= sprintf(",\n%s \"units\": \"%s\"", @indent_str,
23 @units)
24 }
25 $value .= sprintf("\n%s}", @indent_str)
26 %)
27
28 /**
29 * smacro json_output_data_start - Start the json output.
30 *
31 * Description: The json_output_data_start macro is designed to be
32 * called from the 'json_data' probe from the user's script. It marks
33 * the start of the JSON output.
34 */
35 @define json_output_data_start
36 %(
37 __comma_needed = 0
38 $value =
39 "{\n"
40 %)
41
42 # Make sure we don't try to output the same metric twice in the same
43 # data fetch.
44 @define __json_output_check(name)
45 %(
46 if (@name in __json_metric_output)
47 error(sprintf("Metric '%s' already output", @name))
48 __json_metric_output[@name] = 1
49 %)
50
51 # Make sure we don't try to output the same array index twice in the same
52 # data fetch.
53 @define __json_output_array_check(array_index)
54 %(
55 if (@array_index in __json_array_output)
56 error(sprintf("Array index '%s' already output for array metric %s",
57 @array_index, __json_array_started))
58 __json_array_output[@array_index] = 1
59 %)
60
61 /**
62 * smacro json_output_string_value - Output a string value.
63 *
64 * @name: The name of the string metric.
65 * @value: The string value to output.
66 *
67 * Description: The json_output_string_value macro is designed to be
68 * called from the 'json_data' probe in the user's script to output a
69 * metric's string value. This metric should have been added with
70 * json_add_string_metric().
71 */
72 @define json_output_string_value(name, value)
73 %(
74 @__json_output_check(@name)
75 @__json_output_array_end
76 if (__comma_needed)
77 $value .= ",\n"
78 __comma_needed = 1
79 $value .= sprintf(" \"%s\": \"%s\"", @name, @value)
80 %)
81
82 /**
83 * smacro json_output_numeric_value - Output a numeric value.
84 *
85 * @name: The name of the numeric metric.
86 * @value: The numeric value to output.
87 *
88 * Description: The json_output_numeric_value macro is designed to be
89 * called from the 'json_data' probe in the user's script to output a
90 * metric's numeric value. This metric should have been added with
91 * json_add_numeric_metric().
92 */
93 @define json_output_numeric_value(name, value)
94 %(
95 @__json_output_check(@name)
96 @__json_output_array_end
97 if (__comma_needed)
98 $value .= ",\n"
99 __comma_needed = 1
100 $value .= sprintf(" \"%s\": %d", @name, @value)
101 %)
102
103 /**
104 * smacro json_output_array_string_value - Output a string value for metric in an array.
105 *
106 * @array_name: The name of the array.
107 * @array_index: The array index (as a string) indicating where to store the string value.
108 * @metric_name: The name of the string metric.
109 * @value: The string value to output.
110 *
111 * Description: The json_output_array_string_value macro is designed
112 * to be called from the 'json_data' probe in the user's script to
113 * output a metric's string value that is in an array. This metric
114 * should have been added with json_add_array_string_metric().
115 */
116 @define json_output_array_string_value(array_name, array_index, metric_name, value)
117 %(
118 @__json_output_array_start(@array_name, @array_index)
119 if (__comma_needed)
120 $value .= ",\n"
121 __comma_needed = 1
122 $value .= sprintf(" \"%s\": \"%s\"", @metric_name, @value)
123 %)
124
125 /**
126 * smacro json_output_array_numeric_value - Output a numeric value for metric in an array.
127 *
128 * @array_name: The name of the array.
129 * @array_index: The array index (as a string) indicating where to store the numeric value.
130 * @metric_name: The name of the numeric metric.
131 * @value: The numeric value to output.
132 *
133 * Description: The json_output_array_numeric_value macro is designed
134 * to be called from the 'json_data' probe in the user's script to
135 * output a metric's numeric value that is in an array. This metric
136 * should have been added with json_add_array_numeric_metric().
137 */
138 @define json_output_array_numeric_value(array_name, array_index, metric_name, value)
139 %(
140 @__json_output_array_start(@array_name, @array_index)
141 if (__comma_needed)
142 $value .= ",\n"
143 __comma_needed = 1
144 $value .= sprintf(" \"%s\": %d", @metric_name, @value)
145 %)
146
147 # Handle the details of starting the output of an array.
148 @define __json_output_array_start(array_name, array_index)
149 %(
150 if (__json_array_started != @array_name) {
151 @__json_output_check(@array_name)
152 @__json_output_array_end
153 if (__comma_needed)
154 $value .= ",\n"
155 __comma_needed = 1
156 $value .= sprintf(" \"%s\": [\n", @array_name)
157 $value .= " {\n"
158 __json_array_started = @array_name
159 }
160 if (__json_array_index_started != @array_index) {
161 @__json_output_array_check(@array_index)
162 if (__json_array_index_started != "") {
163 $value .=
164 "\n"
165 " },\n"
166 " {\n"
167 }
168 __json_array_index_started = @array_index
169 $value .= sprintf(" \"__id\": \"%s\"", @array_index)
170 }
171 %)
172
173 # Handle the details of finishing the output of an array.
174 @define __json_output_array_end
175 %(
176 if (__json_array_started != "") {
177 $value .=
178 "\n"
179 " }\n"
180 " ]"
181 __json_array_started = ""
182 __json_array_index_started = ""
183 delete __json_array_output
184 }
185 %)
186
187 /**
188 * smacro json_output_data_end - End the json output.
189 *
190 * Description: The json_output_data_end macro is designed to be
191 * called from the 'json_data' probe from the user's script. It marks
192 * the end of the JSON output.
193 */
194 @define json_output_data_end
195 %(
196 @__json_output_array_end
197 $value .=
198 "\n"
199 "}\n"
200 __comma_needed = 0
201 delete __json_metric_output
202 %)