Skip to content

Remove HT config from bootstrap and app classloader #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies {
// https://oss.jfrog.org/artifactory/oss-snapshot-local/io/opentelemetry/instrumentation/auto/
// https://dl.bintray.com/open-telemetry/maven/
implementation("io.opentelemetry.javaagent", "opentelemetry-javaagent", version = "${versions["opentelemetry_java_agent"]}", classifier = "all")
implementation(project(":javaagent-core"))
implementation(project(":filter-api"))
}

Expand Down Expand Up @@ -50,8 +49,7 @@ tasks {
}

relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
// TODO causes data not being reported
// relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")

// prevents conflict with library instrumentation
relocate("io.opentelemetry.instrumentation.api", "io.opentelemetry.javaagent.shaded.instrumentation.api")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,13 @@

package org.hypertrace.agent.instrument;

import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.javaagent.OpenTelemetryAgent;
import java.lang.instrument.Instrumentation;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.hypertrace.agent.core.config.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";
private static final String OTEL_PROCESSOR_BATCH_MAX_QUEUE = "otel.bsp.max.queue.size";
private static final String OTEL_DEFAULT_LOG_LEVEL =
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";

private static HypertraceAgent instance;

Expand All @@ -64,34 +49,12 @@ public static void agentmain(String agentArgs, Instrumentation inst) {
}

instance = new HypertraceAgent();
setDefaultConfig();
OpenTelemetryAgent.premain(agentArgs, inst);
System.out.printf(
"Hypertrace agent started, version: %s\n",
HypertraceAgent.class.getPackage().getImplementationVersion());
}

/** Set default values to OTEL config. OTEL config has a higher precedence. */
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().getEndpoint().getValue());
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())
.collect(Collectors.joining(","));
}

// Expected format is "arg1=val1,arg2=val2,arg3=val3"
private static Map<String, String> parseAgentArgs(String agentArgs) {
if (agentArgs == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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;

import com.google.auto.service.AutoService;
import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.javaagent.spi.config.PropertySource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.hypertrace.agent.config.Config.AgentConfig;
import org.hypertrace.agent.config.Config.PropagationFormat;
import org.hypertrace.agent.core.config.HypertraceConfig;

@AutoService(PropertySource.class)
public class HypertraceAgentConfiguration implements PropertySource {

// 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";
private static final String OTEL_PROCESSOR_BATCH_MAX_QUEUE = "otel.bsp.max.queue.size";
private static final String OTEL_DEFAULT_LOG_LEVEL =
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";

@Override
public Map<String, String> getProperties() {
AgentConfig agentConfig = HypertraceConfig.get();

Map<String, String> configProperties = new HashMap<>();
configProperties.put(OTEL_EXPORTER, "zipkin");
configProperties.put(
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, agentConfig.getServiceName().getValue());
configProperties.put(
OTEL_EXPORTER_ZIPKIN_ENDPOINT, agentConfig.getReporting().getEndpoint().getValue());
configProperties.put(
OTEL_PROPAGATORS, toOtelPropagators(agentConfig.getPropagationFormatsList()));

return configProperties;
}

@VisibleForTesting
static String toOtelPropagators(List<PropagationFormat> propagationFormats) {
return propagationFormats.stream()
.map(v -> v.name().toLowerCase())
.collect(Collectors.joining(","));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class HypertraceTracerCustomizer implements TracerCustomizer {

@Override
public void configure(SdkTracerManagement tracerManagement) {
String exporter = System.getProperty("otel.exporter");
if (exporter != null && exporter.contains("zipkin")) {
tracerManagement.addSpanProcessor(new AddTagsSpanProcessor());
}
tracerManagement.addSpanProcessor(new AddTagsSpanProcessor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
* limitations under the License.
*/

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

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 {
public class HypertraceAgentConfigurationTest {

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