|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.dynamic.policy; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.io.TempDir; |
| 18 | + |
| 19 | +class LinePerPolicyFileProviderTest { |
| 20 | + |
| 21 | + private static final String TRACE_SAMPLING_TYPE = "trace-sampling"; |
| 22 | + private static final String TRACE_SAMPLING_ALIAS = "trace-sampling.probability"; |
| 23 | + |
| 24 | + @TempDir Path tempDir; |
| 25 | + |
| 26 | + @Test |
| 27 | + void fetchPoliciesReturnsEmptyWhenFileMissing() throws Exception { |
| 28 | + Path missingFile = tempDir.resolve("missing-policies.txt"); |
| 29 | + LinePerPolicyFileProvider provider = |
| 30 | + new LinePerPolicyFileProvider(missingFile, Collections.singletonList(acceptingValidator())); |
| 31 | + |
| 32 | + List<TelemetryPolicy> policies = provider.fetchPolicies(); |
| 33 | + |
| 34 | + assertThat(policies).isEmpty(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void fetchPoliciesParsesJsonLines() throws Exception { |
| 39 | + Path file = writeLines("{\"trace-sampling\": {\"probability\": 0.5}}"); |
| 40 | + LinePerPolicyFileProvider provider = |
| 41 | + new LinePerPolicyFileProvider(file, Collections.singletonList(acceptingValidator())); |
| 42 | + |
| 43 | + List<TelemetryPolicy> policies = provider.fetchPolicies(); |
| 44 | + |
| 45 | + assertThat(policies).hasSize(1); |
| 46 | + assertThat(policies.get(0).getType()).isEqualTo(TRACE_SAMPLING_TYPE); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void fetchPoliciesParsesAliasLines() throws Exception { |
| 51 | + Path file = writeLines("trace-sampling.probability=0.5"); |
| 52 | + LinePerPolicyFileProvider provider = |
| 53 | + new LinePerPolicyFileProvider(file, Collections.singletonList(acceptingValidator())); |
| 54 | + |
| 55 | + List<TelemetryPolicy> policies = provider.fetchPolicies(); |
| 56 | + |
| 57 | + assertThat(policies).hasSize(1); |
| 58 | + assertThat(policies.get(0).getType()).isEqualTo(TRACE_SAMPLING_TYPE); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void fetchPoliciesSkipsBlankLinesAndComments() throws Exception { |
| 63 | + Path file = writeLines("", " ", "# comment line", "trace-sampling.probability=0.25"); |
| 64 | + LinePerPolicyFileProvider provider = |
| 65 | + new LinePerPolicyFileProvider(file, Collections.singletonList(acceptingValidator())); |
| 66 | + |
| 67 | + List<TelemetryPolicy> policies = provider.fetchPolicies(); |
| 68 | + |
| 69 | + assertThat(policies).hasSize(1); |
| 70 | + assertThat(policies.get(0).getType()).isEqualTo(TRACE_SAMPLING_TYPE); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void fetchPoliciesSkipsUnknownOrRejectedPolicies() throws Exception { |
| 75 | + PolicyValidator rejectingValidator = |
| 76 | + new TestPolicyValidator(/* acceptJson= */ false, /* acceptAlias= */ false); |
| 77 | + Path file = |
| 78 | + writeLines( |
| 79 | + "{\"trace-sampling\": {\"probability\": 0.5}}", |
| 80 | + "{\"other-policy\": {\"probability\": 0.5}}", |
| 81 | + "other.key=1"); |
| 82 | + LinePerPolicyFileProvider provider = |
| 83 | + new LinePerPolicyFileProvider(file, Collections.singletonList(rejectingValidator)); |
| 84 | + |
| 85 | + List<TelemetryPolicy> policies = provider.fetchPolicies(); |
| 86 | + |
| 87 | + assertThat(policies).isEmpty(); |
| 88 | + } |
| 89 | + |
| 90 | + private Path writeLines(String... lines) throws IOException { |
| 91 | + Path file = tempDir.resolve("policies.txt"); |
| 92 | + Files.write(file, Arrays.asList(lines)); |
| 93 | + return file; |
| 94 | + } |
| 95 | + |
| 96 | + private static PolicyValidator acceptingValidator() { |
| 97 | + return new TestPolicyValidator(/* acceptJson= */ true, /* acceptAlias= */ true); |
| 98 | + } |
| 99 | + |
| 100 | + private static class TestPolicyValidator implements PolicyValidator { |
| 101 | + private final boolean acceptJson; |
| 102 | + private final boolean acceptAlias; |
| 103 | + |
| 104 | + private TestPolicyValidator(boolean acceptJson, boolean acceptAlias) { |
| 105 | + this.acceptJson = acceptJson; |
| 106 | + this.acceptAlias = acceptAlias; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public TelemetryPolicy validate(String json) { |
| 111 | + if (!acceptJson) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return new TelemetryPolicy(TRACE_SAMPLING_TYPE, null); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String getPolicyType() { |
| 119 | + return TRACE_SAMPLING_TYPE; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public TelemetryPolicy validateAlias(String key, String value) { |
| 124 | + if (!acceptAlias) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + return new TelemetryPolicy(TRACE_SAMPLING_TYPE, null); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String getAlias() { |
| 132 | + return TRACE_SAMPLING_ALIAS; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments