Skip to content

Commit

Permalink
Bump config with nullable service name, multiple propagations and OPA (
Browse files Browse the repository at this point in the history
…#119)

* Bump config with nullable service name, multiple propagations and OPA

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* support all

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* remove

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* foo

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* revert zipkin sv name

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>

* Fix

Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
pavolloffay authored Nov 17, 2020
1 parent 312d34e commit e625365
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 18 deletions.
3 changes: 1 addition & 2 deletions filter-custom-opa/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
plugins {
java
`java-library`
}

dependencies {
implementation(project(":filter-api"))
implementation("org.slf4j:slf4j-api:1.7.30")
implementation("com.squareup.okhttp3:okhttp:3.14.9")
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation("com.fasterxml.jackson.core:jackson-databind:2.11.3")
implementation("com.google.auto.service:auto-service:1.0-rc7")
annotationProcessor("com.google.auto.service:auto-service:1.0-rc7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.hypertrace.agent.filter.opa.custom;

import com.google.auto.service.AutoService;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.Reporting;
import org.hypertrace.agent.core.EnvironmentConfig;
import org.hypertrace.agent.core.HypertraceConfig;
import org.hypertrace.agent.filter.FilterRegistry;
import org.hypertrace.agent.filter.api.Filter;
import org.hypertrace.agent.filter.spi.FilterProvider;
Expand All @@ -35,11 +38,12 @@ public CustomOpaLibProvider() {

@Override
public Filter create() {
String endpoint = EnvironmentConfig.getProperty("hypertrace.opa.endpoint");
String token = EnvironmentConfig.getProperty("hypertrace.reporting.token");
String pullInterval = EnvironmentConfig.getProperty("hypertrace.opa.pull_interval");
String skipVerify = EnvironmentConfig.getProperty("hypertrace.opa.skip_verify");
AgentConfig agentConfig = HypertraceConfig.get();
Reporting reporting = agentConfig.getReporting();
return new CustomOpaLib(
endpoint, token, Boolean.parseBoolean(skipVerify), Integer.parseInt(pullInterval));
reporting.getOpa().getAddress().getValue(),
reporting.getToken().getValue(),
reporting.getSecure().getValue(),
reporting.getOpa().getPollPeriod().getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package org.hypertrace.agent.core;

import com.google.protobuf.BoolValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.StringValue;
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.config.Config.Opa;
import org.hypertrace.agent.config.Config.Opa.Builder;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.hypertrace.agent.config.Config.Reporting;

public class EnvironmentConfig {
Expand All @@ -32,10 +36,16 @@ private EnvironmentConfig() {}
public static final String CONFIG_FILE_PROPERTY = HT_PREFIX + "config.file";
static final String SERVICE_NAME = HT_PREFIX + "service.name";

static final String PROPAGATION_FORMATS = HT_PREFIX + "propagation_formats";

private static final String REPORTING_PREFIX = HT_PREFIX + "reporting.";
static final String REPORTING_ADDRESS = REPORTING_PREFIX + "address";
static final String REPORTING_SECURE = REPORTING_PREFIX + "secure";

private static final String OPA_PREFIX = REPORTING_PREFIX + "opa.";
static final String OPA_ADDRESS = OPA_PREFIX + "address";
static final String OPA_POLL_PERIOD = OPA_PREFIX + "poll.period";

private static final String CAPTURE_PREFIX = HT_PREFIX + "data.capture.";
public static final String CAPTURE_HTTP_HEADERS_PREFIX = CAPTURE_PREFIX + "http.headers.";
public static final String CAPTURE_HTTP_BODY_PREFIX = CAPTURE_PREFIX + "http.body.";
Expand All @@ -45,7 +55,7 @@ private EnvironmentConfig() {}
public static AgentConfig.Builder applyPropertiesAndEnvVars(AgentConfig.Builder builder) {
String serviceName = getProperty(SERVICE_NAME);
if (serviceName != null) {
builder.setServiceName(serviceName);
builder.setServiceName(StringValue.newBuilder().setValue(serviceName).build());
}

Reporting.Builder reportingBuilder = applyReporting(builder.getReporting().toBuilder());
Expand All @@ -54,9 +64,20 @@ public static AgentConfig.Builder applyPropertiesAndEnvVars(AgentConfig.Builder
DataCapture.Builder dataCaptureBuilder =
setDefaultsToDataCapture(builder.getDataCapture().toBuilder());
builder.setDataCapture(dataCaptureBuilder);
applyPropagationFormat(builder);
return builder;
}

private static void applyPropagationFormat(AgentConfig.Builder builder) {
String propagationFormats = getProperty(PROPAGATION_FORMATS);
if (propagationFormats != null) {
String[] formats = propagationFormats.split(",");
for (String format : formats) {
builder.addPropagationFormats(PropagationFormat.valueOf(format));
}
}
}

private static Reporting.Builder applyReporting(Reporting.Builder builder) {
String reporterAddress = getProperty(REPORTING_ADDRESS);
if (reporterAddress != null) {
Expand All @@ -66,7 +87,20 @@ private static Reporting.Builder applyReporting(Reporting.Builder builder) {
if (secure != null) {
builder.setSecure(BoolValue.newBuilder().setValue(Boolean.valueOf(secure)).build());
}
Builder opaBuilder = applyOpa(builder.getOpa().toBuilder());
builder.setOpa(opaBuilder);
return builder;
}

private static Opa.Builder applyOpa(Opa.Builder builder) {
String address = getProperty(OPA_ADDRESS);
if (address != null) {
builder.setAddress(StringValue.newBuilder().setValue(address).build());
}
String pollPeriod = getProperty(OPA_POLL_PERIOD);
if (pollPeriod != null) {
builder.setPollPeriod(Int32Value.newBuilder().setValue(Integer.parseInt(pollPeriod)).build());
}
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.BoolValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.StringValue;
import com.google.protobuf.util.JsonFormat;
import java.io.File;
Expand All @@ -29,6 +30,9 @@
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.config.Config.Opa;
import org.hypertrace.agent.config.Config.Opa.Builder;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.hypertrace.agent.config.Config.Reporting;

/** {@link HypertraceConfig} loads a yaml config from file. */
Expand All @@ -38,8 +42,10 @@ private HypertraceConfig() {}

private static AgentConfig agentConfig;

static final String DEFAULT_SERVICE_NAME = "unknown";
static final String DEFAULT_REPORTING_ADDRESS = "http://localhost:9411/api/v2/spans";
static final String DEFAULT_SERVICE_NAME = "default_service_name";
static final String DEFAULT_OPA_ADDRESS = "http://opa.traceableai:8181/";
static final int DEFAULT_OPA_POLL_PERIOD_SECONDS = 30;

public static AgentConfig get() {
if (agentConfig == null) {
Expand Down Expand Up @@ -104,8 +110,8 @@ static AgentConfig load(String filename) throws IOException {
}

private static AgentConfig.Builder applyDefaults(AgentConfig.Builder configBuilder) {
if (configBuilder.getServiceName().isEmpty()) {
configBuilder.setServiceName(DEFAULT_SERVICE_NAME);
if (configBuilder.getServiceName().getValue().isEmpty()) {
configBuilder.setServiceName(StringValue.newBuilder().setValue(DEFAULT_SERVICE_NAME).build());
}

Reporting.Builder reportingBuilder =
Expand All @@ -115,13 +121,30 @@ private static AgentConfig.Builder applyDefaults(AgentConfig.Builder configBuild
DataCapture.Builder dataCaptureBuilder =
setDefaultsToDataCapture(configBuilder.getDataCapture().toBuilder());
configBuilder.setDataCapture(dataCaptureBuilder);

if (configBuilder.getPropagationFormatsList().isEmpty()) {
configBuilder.addPropagationFormats(PropagationFormat.TRACE_CONTEXT);
}
return configBuilder;
}

private static Reporting.Builder applyReportingDefaults(Reporting.Builder builder) {
if (!builder.hasAddress()) {
builder.setAddress(StringValue.newBuilder().setValue(DEFAULT_REPORTING_ADDRESS).build());
}
Builder opaBuilder = applyOpaDefaults(builder.getOpa().toBuilder());
builder.setOpa(opaBuilder);
return builder;
}

private static Opa.Builder applyOpaDefaults(Opa.Builder builder) {
if (!builder.hasAddress()) {
builder.setAddress(StringValue.newBuilder().setValue(DEFAULT_OPA_ADDRESS).build());
}
if (!builder.hasPollPeriod()) {
builder.setPollPeriod(
Int32Value.newBuilder().setValue(DEFAULT_OPA_POLL_PERIOD_SECONDS).build());
}
return builder;
}

Expand Down
2 changes: 1 addition & 1 deletion javaagent-core/src/main/proto
Submodule proto updated 2 files
+1 −0 .protolint.yaml
+37 −3 config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.hypertrace.agent.core;

import com.google.protobuf.StringValue;
import java.util.Arrays;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ClearSystemProperty;
Expand All @@ -27,20 +29,32 @@ class EnvironmentConfigTest {
@Test
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_ADDRESS)
@ClearSystemProperty(key = EnvironmentConfig.REPORTING_SECURE)
@ClearSystemProperty(key = EnvironmentConfig.OPA_ADDRESS)
@ClearSystemProperty(key = EnvironmentConfig.OPA_POLL_PERIOD)
@ClearSystemProperty(key = EnvironmentConfig.PROPAGATION_FORMATS)
@ClearSystemProperty(key = EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request")
public void systemProperties() {
// when tests are run in parallel the env vars/sys props set it junit-pioneer are visible to
// parallel tests
System.setProperty(EnvironmentConfig.REPORTING_ADDRESS, "http://:-)");
System.setProperty(EnvironmentConfig.REPORTING_SECURE, "true");
System.setProperty(EnvironmentConfig.CAPTURE_HTTP_BODY_PREFIX + "request", "true");
System.setProperty(EnvironmentConfig.OPA_ADDRESS, "http://azkaban:9090");
System.setProperty(EnvironmentConfig.OPA_POLL_PERIOD, "10");
System.setProperty(EnvironmentConfig.PROPAGATION_FORMATS, "B3,TRACE_CONTEXT");

AgentConfig.Builder configBuilder = AgentConfig.newBuilder();
configBuilder.setServiceName(StringValue.newBuilder().setValue("foo").getValue());
configBuilder.setServiceName(StringValue.newBuilder().setValue("foo"));

AgentConfig agentConfig = EnvironmentConfig.applyPropertiesAndEnvVars(configBuilder).build();
Assertions.assertEquals("foo", agentConfig.getServiceName());
Assertions.assertEquals("foo", agentConfig.getServiceName().getValue());
Assertions.assertEquals(
Arrays.asList(PropagationFormat.B3, PropagationFormat.TRACE_CONTEXT),
agentConfig.getPropagationFormatsList());
Assertions.assertEquals("http://:-)", agentConfig.getReporting().getAddress().getValue());
Assertions.assertEquals(
"http://azkaban:9090", agentConfig.getReporting().getOpa().getAddress().getValue());
Assertions.assertEquals(10, agentConfig.getReporting().getOpa().getPollPeriod().getValue());
Assertions.assertEquals(true, agentConfig.getReporting().getSecure().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getHttpBody().getRequest().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ClearSystemProperty;
Expand All @@ -29,11 +31,19 @@ public class HypertraceConfigTest {
public void defaultValues() throws IOException {
URL resource = getClass().getClassLoader().getResource("emptyconfig.yaml");
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
Assertions.assertEquals("default_service_name", agentConfig.getServiceName());
Assertions.assertEquals("unknown", agentConfig.getServiceName().getValue());
Assertions.assertEquals(
HypertraceConfig.DEFAULT_REPORTING_ADDRESS,
agentConfig.getReporting().getAddress().getValue());
Assertions.assertEquals(
Arrays.asList(PropagationFormat.TRACE_CONTEXT), agentConfig.getPropagationFormatsList());
Assertions.assertEquals(false, agentConfig.getReporting().getSecure().getValue());
Assertions.assertEquals(
HypertraceConfig.DEFAULT_OPA_ADDRESS,
agentConfig.getReporting().getOpa().getAddress().getValue());
Assertions.assertEquals(
HypertraceConfig.DEFAULT_OPA_POLL_PERIOD_SECONDS,
agentConfig.getReporting().getOpa().getPollPeriod().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue());
Assertions.assertEquals(
Expand All @@ -56,10 +66,15 @@ public void defaultValues() throws IOException {
public void config() throws IOException {
URL resource = getClass().getClassLoader().getResource("config.yaml");
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
Assertions.assertEquals("service", agentConfig.getServiceName());
Assertions.assertEquals("service", agentConfig.getServiceName().getValue());
Assertions.assertEquals(
Arrays.asList(PropagationFormat.B3), agentConfig.getPropagationFormatsList());
Assertions.assertEquals(
"http://localhost:9411", agentConfig.getReporting().getAddress().getValue());
Assertions.assertEquals(true, agentConfig.getReporting().getSecure().getValue());
Assertions.assertEquals(
"http://opa.localhost:8181/", agentConfig.getReporting().getOpa().getAddress().getValue());
Assertions.assertEquals(12, agentConfig.getReporting().getOpa().getPollPeriod().getValue());
Assertions.assertEquals(
true, agentConfig.getDataCapture().getHttpHeaders().getRequest().getValue());
Assertions.assertEquals(
Expand All @@ -79,6 +94,6 @@ public void configWithSystemProps() throws IOException {
AgentConfig agentConfig = HypertraceConfig.load(resource.getPath());
Assertions.assertEquals(
"http://nowhere.here", agentConfig.getReporting().getAddress().getValue());
Assertions.assertEquals("service", agentConfig.getServiceName());
Assertions.assertEquals("service", agentConfig.getServiceName().getValue());
}
}
5 changes: 5 additions & 0 deletions javaagent-core/src/test/resources/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
serviceName: service
propagationFormats:
- B3
reporting:
address: http://localhost:9411
secure: true
opa:
address: http://opa.localhost:8181/
pollPeriod: 12
dataCapture:
httpHeaders:
request: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package org.hypertrace.agent.instrument;

import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.javaagent.OpenTelemetryAgent;
import java.lang.instrument.Instrumentation;
import java.util.List;
import java.util.stream.Collectors;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.hypertrace.agent.core.HypertraceConfig;

public class HypertraceAgent {
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-environment-variables.md
private static final String OTEL_EXPORTER = "otel.exporter";
private static final String OTEL_PROPAGATORS = "otel.propagators";
private static final String OTEL_EXPORTER_ZIPKIN_ENDPOINT = "otel.exporter.zipkin.endpoint";
private static final String OTEL_EXPORTER_ZIPKIN_SERVICE_NAME =
"otel.exporter.zipkin.service.name";
Expand All @@ -44,8 +49,20 @@ public static void agentmain(String agentArgs, Instrumentation inst) {
private static void setDefaultConfig() {
AgentConfig agentConfig = HypertraceConfig.get();
OpenTelemetryConfig.setDefault(OTEL_EXPORTER, "zipkin");
OpenTelemetryConfig.setDefault(
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName().getValue());
OpenTelemetryConfig.setDefault(
OTEL_PROPAGATORS, toOtelPropagators(agentConfig.getPropagationFormatsList()));
OpenTelemetryConfig.setDefault(
OTEL_EXPORTER_ZIPKIN_ENDPOINT, agentConfig.getReporting().getAddress().getValue());
OpenTelemetryConfig.setDefault(OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName());
OpenTelemetryConfig.setDefault(
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName().getValue());
}

@VisibleForTesting
static String toOtelPropagators(List<PropagationFormat> propagationFormats) {
return propagationFormats.stream()
.map(v -> v.name().toLowerCase().replaceAll("_", ""))
.collect(Collectors.joining(","));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.instrument;

import java.util.Arrays;
import java.util.List;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class HypertraceAgentTest {

@Test
public void propagationFormatList() {
List<PropagationFormat> formats =
Arrays.asList(PropagationFormat.B3, PropagationFormat.TRACE_CONTEXT);
Assertions.assertEquals("b3,tracecontext", HypertraceAgent.toOtelPropagators(formats));
}
}

0 comments on commit e625365

Please sign in to comment.