Skip to content

Commit

Permalink
✨ Expose reporting config to consumers of javaagent-core so that `F…
Browse files Browse the repository at this point in the history
…ilter` implementation authors can access them (#327)
  • Loading branch information
ryandens authored Jun 28, 2021
1 parent a2333cf commit 9a40dc5
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright The Hypertrace Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hypertrace.agent.core.config;

import java.util.Iterator;
import java.util.ServiceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface ReportingConfig {

/**
* @return the {@link Opa} config implementation
* @see Opa for more information on why this API is deprecated
*/
@Deprecated
Opa opa();

boolean secure();

String token();

/**
* Opa holds the configuration for the agent and filter implementations should interact with a
* remote Open Policy Agent endpoint.
*
* <p>Note, this API is deprecated because it is a goal of the Hypertrace community to migrate
* away form supplying this vendor-specific config and instead have authors of Hypertrace
* extensions/filters to have an indiomatic way to extned the Hypertrace configuration properties
* for their use cases
*/
@Deprecated
interface Opa {

boolean enabled();

String endpoint();

int pollPeriodSeconds();
}

final class ConfigProvider {

private static final Logger logger = LoggerFactory.getLogger(ConfigProvider.class);

private static volatile ReportingConfig reportingConfig;

private static ReportingConfig load() {
ServiceLoader<ReportingConfig> configs = ServiceLoader.load(ReportingConfig.class);
Iterator<ReportingConfig> iterator = configs.iterator();
if (!iterator.hasNext()) {
logger.error("Failed to load reporting config");
return null;
}
return iterator.next();
}

public static ReportingConfig get() {
if (reportingConfig == null) {
synchronized (ConfigProvider.class) {
if (reportingConfig == null) {
reportingConfig = load();
}
}
}
return reportingConfig;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hypertrace.agent.config.Config;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.DataCapture;
import org.hypertrace.agent.config.Config.Message;
import org.hypertrace.agent.core.config.InstrumentationConfig;

@AutoService(InstrumentationConfig.class)
Expand Down Expand Up @@ -65,7 +66,7 @@ public Message rpcBody() {
return this.rpcBody;
}

private class MessageImpl implements Message {
private static final class MessageImpl implements Message {

private final Config.Message message;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright The Hypertrace Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hypertrace.agent.otel.extensions.config;

import com.google.auto.service.AutoService;
import org.hypertrace.agent.config.Config;
import org.hypertrace.agent.core.config.ReportingConfig;

@AutoService(ReportingConfig.class)
public final class ReportingConfigImpl implements ReportingConfig {

private final Opa opa;
private final Config.Reporting reporting;

public ReportingConfigImpl(final Config.Reporting reporting) {
this.opa = new OpaImpl(reporting.getOpa());
this.reporting = reporting;
}

@Override
public Opa opa() {
return opa;
}

@Override
public boolean secure() {
return reporting.getSecure().getValue();
}

@Override
public String token() {
return reporting.getToken().getValue();
}

private static final class OpaImpl implements Opa {

private final Config.Opa opa;

public OpaImpl(final Config.Opa opa) {
this.opa = opa;
}

@Override
public boolean enabled() {
return opa.hasEnabled() ? opa.getEnabled().getValue() : true;
}

@Override
public String endpoint() {
return opa.getEndpoint().getValue();
}

@Override
public int pollPeriodSeconds() {
return opa.getPollPeriodSeconds().getValue();
}
}
}

0 comments on commit 9a40dc5

Please sign in to comment.