Skip to content

Commit 9a40dc5

Browse files
authored
✨ Expose reporting config to consumers of javaagent-core so that Filter implementation authors can access them (#327)
1 parent a2333cf commit 9a40dc5

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.core.config;
18+
19+
import java.util.Iterator;
20+
import java.util.ServiceLoader;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
public interface ReportingConfig {
25+
26+
/**
27+
* @return the {@link Opa} config implementation
28+
* @see Opa for more information on why this API is deprecated
29+
*/
30+
@Deprecated
31+
Opa opa();
32+
33+
boolean secure();
34+
35+
String token();
36+
37+
/**
38+
* Opa holds the configuration for the agent and filter implementations should interact with a
39+
* remote Open Policy Agent endpoint.
40+
*
41+
* <p>Note, this API is deprecated because it is a goal of the Hypertrace community to migrate
42+
* away form supplying this vendor-specific config and instead have authors of Hypertrace
43+
* extensions/filters to have an indiomatic way to extned the Hypertrace configuration properties
44+
* for their use cases
45+
*/
46+
@Deprecated
47+
interface Opa {
48+
49+
boolean enabled();
50+
51+
String endpoint();
52+
53+
int pollPeriodSeconds();
54+
}
55+
56+
final class ConfigProvider {
57+
58+
private static final Logger logger = LoggerFactory.getLogger(ConfigProvider.class);
59+
60+
private static volatile ReportingConfig reportingConfig;
61+
62+
private static ReportingConfig load() {
63+
ServiceLoader<ReportingConfig> configs = ServiceLoader.load(ReportingConfig.class);
64+
Iterator<ReportingConfig> iterator = configs.iterator();
65+
if (!iterator.hasNext()) {
66+
logger.error("Failed to load reporting config");
67+
return null;
68+
}
69+
return iterator.next();
70+
}
71+
72+
public static ReportingConfig get() {
73+
if (reportingConfig == null) {
74+
synchronized (ConfigProvider.class) {
75+
if (reportingConfig == null) {
76+
reportingConfig = load();
77+
}
78+
}
79+
}
80+
return reportingConfig;
81+
}
82+
}
83+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hypertrace.agent.config.Config;
2121
import org.hypertrace.agent.config.Config.AgentConfig;
2222
import org.hypertrace.agent.config.Config.DataCapture;
23+
import org.hypertrace.agent.config.Config.Message;
2324
import org.hypertrace.agent.core.config.InstrumentationConfig;
2425

2526
@AutoService(InstrumentationConfig.class)
@@ -65,7 +66,7 @@ public Message rpcBody() {
6566
return this.rpcBody;
6667
}
6768

68-
private class MessageImpl implements Message {
69+
private static final class MessageImpl implements Message {
6970

7071
private final Config.Message message;
7172

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 com.google.auto.service.AutoService;
20+
import org.hypertrace.agent.config.Config;
21+
import org.hypertrace.agent.core.config.ReportingConfig;
22+
23+
@AutoService(ReportingConfig.class)
24+
public final class ReportingConfigImpl implements ReportingConfig {
25+
26+
private final Opa opa;
27+
private final Config.Reporting reporting;
28+
29+
public ReportingConfigImpl(final Config.Reporting reporting) {
30+
this.opa = new OpaImpl(reporting.getOpa());
31+
this.reporting = reporting;
32+
}
33+
34+
@Override
35+
public Opa opa() {
36+
return opa;
37+
}
38+
39+
@Override
40+
public boolean secure() {
41+
return reporting.getSecure().getValue();
42+
}
43+
44+
@Override
45+
public String token() {
46+
return reporting.getToken().getValue();
47+
}
48+
49+
private static final class OpaImpl implements Opa {
50+
51+
private final Config.Opa opa;
52+
53+
public OpaImpl(final Config.Opa opa) {
54+
this.opa = opa;
55+
}
56+
57+
@Override
58+
public boolean enabled() {
59+
return opa.hasEnabled() ? opa.getEnabled().getValue() : true;
60+
}
61+
62+
@Override
63+
public String endpoint() {
64+
return opa.getEndpoint().getValue();
65+
}
66+
67+
@Override
68+
public int pollPeriodSeconds() {
69+
return opa.getPollPeriodSeconds().getValue();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)