|
5 | 5 |
|
6 | 6 | package io.opentelemetry.contrib.dynamic.policy; |
7 | 7 |
|
| 8 | +import io.opentelemetry.contrib.dynamic.policy.source.SourceFormat; |
| 9 | +import io.opentelemetry.contrib.dynamic.policy.source.SourceWrapper; |
8 | 10 | import java.io.IOException; |
9 | 11 | import java.nio.file.Files; |
10 | 12 | import java.nio.file.Path; |
|
18 | 20 | * A {@link PolicyProvider} that reads policies from a local file, where each line represents a |
19 | 21 | * separate policy configuration. |
20 | 22 | * |
21 | | - * <p>The file format supports two types of lines: |
| 23 | + * <p>The file format supports JSON and key-value lines: |
22 | 24 | * |
23 | 25 | * <ul> |
24 | 26 | * <li><b>JSON Objects:</b> Lines starting with <code>{</code> are treated as JSON objects and |
25 | 27 | * validated against the registered {@link PolicyValidator}s. |
26 | | - * <li><b>Key-Value Pairs:</b> Lines in the format <code>key=value</code> are treated as aliases, |
27 | | - * where the key matches a validator's {@link PolicyValidator#getAlias()} and the value is |
28 | | - * parsed accordingly. |
| 28 | + * <li><b>Key-Value:</b> Lines containing <code>=</code> are treated as key-value policy lines and |
| 29 | + * validated against the registered {@link PolicyValidator}s. |
29 | 30 | * </ul> |
30 | 31 | * |
31 | 32 | * <p>Empty lines and lines starting with <code>#</code> are ignored. |
@@ -57,47 +58,33 @@ public List<TelemetryPolicy> fetchPolicies() throws IOException { |
57 | 58 | return; |
58 | 59 | } |
59 | 60 |
|
60 | | - TelemetryPolicy policy = null; |
61 | | - |
| 61 | + SourceFormat format; |
62 | 62 | if (trimmedLine.startsWith("{")) { |
63 | | - for (PolicyValidator validator : validators) { |
64 | | - if (trimmedLine.contains("\"" + validator.getPolicyType() + "\"")) { |
65 | | - policy = validator.validate(trimmedLine); |
66 | | - if (policy != null) { |
67 | | - break; |
68 | | - } |
69 | | - } |
70 | | - } |
| 63 | + format = SourceFormat.JSON; |
| 64 | + } else if (trimmedLine.indexOf('=') >= 0) { |
| 65 | + format = SourceFormat.KEYVALUE; |
71 | 66 | } else { |
72 | | - int idx = trimmedLine.indexOf('='); |
73 | | - if (idx > 0) { |
74 | | - String key = trimmedLine.substring(0, idx).trim(); |
75 | | - String valueStr = trimmedLine.substring(idx + 1).trim(); |
| 67 | + logger.info("Unsupported policy line format: " + trimmedLine); |
| 68 | + return; |
| 69 | + } |
| 70 | + List<SourceWrapper> parsedSources = format.parse(trimmedLine); |
| 71 | + if (parsedSources == null || parsedSources.size() != 1) { |
| 72 | + logger.info("Invalid " + format.configValue() + " policy line: " + trimmedLine); |
| 73 | + return; |
| 74 | + } |
76 | 75 |
|
77 | | - for (PolicyValidator validator : validators) { |
78 | | - String alias = validator.getAlias(); |
79 | | - if (alias != null && alias.equals(key)) { |
80 | | - try { |
81 | | - policy = validator.validateAlias(key, valueStr); |
82 | | - } catch (UnsupportedOperationException e) { |
83 | | - logger.info( |
84 | | - "Validator does not support alias validation: " |
85 | | - + validator.getClass().getName()); |
86 | | - continue; |
87 | | - } |
88 | | - if (policy != null) { |
89 | | - break; |
90 | | - } |
91 | | - } |
92 | | - } |
| 76 | + SourceWrapper parsedSource = parsedSources.get(0); |
| 77 | + TelemetryPolicy policy = null; |
| 78 | + for (PolicyValidator validator : validators) { |
| 79 | + policy = validator.validate(parsedSource); |
| 80 | + if (policy != null) { |
| 81 | + break; |
93 | 82 | } |
94 | 83 | } |
95 | | - |
96 | 84 | if (policy == null) { |
97 | 85 | logger.info("Validator not found or rejected for line: " + trimmedLine); |
98 | 86 | return; |
99 | 87 | } |
100 | | - |
101 | 88 | policies.add(policy); |
102 | 89 | }); |
103 | 90 | } |
|
0 commit comments