|
6 | 6 | import java.io.FileWriter;
|
7 | 7 | import java.io.IOException;
|
8 | 8 | import java.io.Serializable;
|
| 9 | +import java.util.ArrayList; |
9 | 10 | import org.slf4j.Logger;
|
10 | 11 | import org.slf4j.LoggerFactory;
|
11 | 12 |
|
@@ -199,6 +200,15 @@ public String getKey() {
|
199 | 200 | return key;
|
200 | 201 | }
|
201 | 202 |
|
| 203 | + /** |
| 204 | + * Return if key is considered sensitive |
| 205 | + * |
| 206 | + * @return boolean |
| 207 | + */ |
| 208 | + public boolean getIsSensitive() { |
| 209 | + return isSensitive; |
| 210 | + } |
| 211 | + |
202 | 212 | /**
|
203 | 213 | * Obtain given associated key type
|
204 | 214 | *
|
@@ -324,17 +334,38 @@ public AlertMeta(@JsonProperty("key") String key, @JsonProperty("value") String
|
324 | 334 | }
|
325 | 335 |
|
326 | 336 | /**
|
327 |
| - * Convert metadata to Golang |
| 337 | + * Convert metadata to various formats |
| 338 | + * |
| 339 | + * <p>This main function can be used to export metadata keys in golang format, and to export a |
| 340 | + * BigQuery view query that is suitable for metrics usage. |
328 | 341 | *
|
329 | 342 | * @param args Arguments
|
330 | 343 | */
|
331 | 344 | public static void main(String[] args) throws IOException {
|
332 |
| - BufferedWriter w = new BufferedWriter(new FileWriter(args[0])); |
333 |
| - w.write("package common\n\n// This file is automatically generated.\n\nconst (\n"); |
334 |
| - for (Key k : Key.values()) { |
335 |
| - w.write(String.format("META_%s = \"%s\"\n", k.name(), k.getKey())); |
| 345 | + if (args[0].equals("gometa")) { |
| 346 | + BufferedWriter w = new BufferedWriter(new FileWriter(args[1])); |
| 347 | + w.write("package common\n\n// This file is automatically generated.\n\nconst (\n"); |
| 348 | + for (Key k : Key.values()) { |
| 349 | + w.write(String.format("META_%s = \"%s\"\n", k.name(), k.getKey())); |
| 350 | + } |
| 351 | + w.write(")\n"); |
| 352 | + w.close(); |
| 353 | + } else if (args[0].equals("metricsview")) { |
| 354 | + BufferedWriter w = new BufferedWriter(new FileWriter(args[1])); |
| 355 | + ArrayList<String> sFields = new ArrayList<>(); |
| 356 | + for (Key k : Key.values()) { |
| 357 | + if (k.getIsSensitive()) { |
| 358 | + sFields.add("'" + k.getKey() + "'"); |
| 359 | + } |
| 360 | + } |
| 361 | + w.write( |
| 362 | + String.format( |
| 363 | + "SELECT EXTRACT(DATETIME FROM _PARTITIONTIME) AS partitiontime, " |
| 364 | + + "id, timestamp, severity, category, ARRAY(\nSELECT AS STRUCT key, " |
| 365 | + + "value FROM UNNEST(metadata) WHERE\nKEY NOT IN (%s)\n) AS metadata\n" |
| 366 | + + "FROM <<table>>\n", |
| 367 | + String.join(", ", sFields))); |
| 368 | + w.close(); |
336 | 369 | }
|
337 |
| - w.write(")\n"); |
338 |
| - w.close(); |
339 | 370 | }
|
340 | 371 | }
|
0 commit comments