Skip to content

Commit a2785b1

Browse files
authored
🐛 Report config service loader fix (#328)
* ✅ add test to verify ReportingConfigImpl * 🐛 fix bug where ReportingConfigImpl could not be instantiated
1 parent 9a40dc5 commit a2785b1

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/ReportingConfigImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,18 @@ public final class ReportingConfigImpl implements ReportingConfig {
2626
private final Opa opa;
2727
private final Config.Reporting reporting;
2828

29-
public ReportingConfigImpl(final Config.Reporting reporting) {
30-
this.opa = new OpaImpl(reporting.getOpa());
31-
this.reporting = reporting;
29+
/**
30+
* This constructor is required in order to be instantiated by the {@link java.util.ServiceLoader}
31+
* API.
32+
*/
33+
@SuppressWarnings("unused")
34+
public ReportingConfigImpl() {
35+
this(HypertraceConfig.get().getReporting());
36+
}
37+
38+
public ReportingConfigImpl(final Config.Reporting reportingConfig) {
39+
this.reporting = reportingConfig;
40+
this.opa = new OpaImpl(reportingConfig.getOpa());
3241
}
3342

3443
@Override
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.hypertrace.agent.otel.extensions.config;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
import com.google.protobuf.BoolValue;
22+
import java.util.Iterator;
23+
import java.util.ServiceLoader;
24+
import org.hypertrace.agent.config.Config.Opa;
25+
import org.hypertrace.agent.config.Config.Reporting;
26+
import org.hypertrace.agent.config.Config.Reporting.Builder;
27+
import org.hypertrace.agent.core.config.ReportingConfig;
28+
import org.junit.jupiter.api.Test;
29+
30+
final class ReportingConfigImplTest {
31+
32+
@Test
33+
void instantiateViaServiceLoaderApi() {
34+
final ServiceLoader<ReportingConfig> reportingConfigs =
35+
ServiceLoader.load(ReportingConfig.class);
36+
final Iterator<ReportingConfig> iterator = reportingConfigs.iterator();
37+
assertTrue(iterator.hasNext());
38+
final ReportingConfig reportingConfig = iterator.next();
39+
assertNotNull(reportingConfig);
40+
assertFalse(iterator.hasNext());
41+
}
42+
43+
@Test
44+
void opaEnabledIfNotProvided() {
45+
final ReportingConfigImpl reportingConfig =
46+
new ReportingConfigImpl(Reporting.getDefaultInstance());
47+
assertTrue(reportingConfig.opa().enabled());
48+
}
49+
50+
@Test
51+
void opaDisabledIfExplicitlySet() {
52+
final Builder reportingBuilder = Reporting.getDefaultInstance().toBuilder();
53+
final Opa explicitOpaConfig =
54+
reportingBuilder.getOpaBuilder().setEnabled(BoolValue.of(false)).build();
55+
final ReportingConfigImpl reportingConfig =
56+
new ReportingConfigImpl(reportingBuilder.setOpa(explicitOpaConfig).build());
57+
assertFalse(reportingConfig.opa().enabled());
58+
}
59+
}

0 commit comments

Comments
 (0)