Use traceable.ai config file to configure OTEL components (#21) · hypertrace/javaagent@fb72807 · GitHub
Skip to content

Commit fb72807

Browse files
authored
Use traceable.ai config file to configure OTEL components (#21)
Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
1 parent aea4a18 commit fb72807

7 files changed

Lines changed: 275 additions & 5 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion

build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ subprojects {
2424
apply(from = "$rootDir/gradle/spotless.gradle")
2525

2626
repositories {
27+
mavenCentral()
2728
jcenter()
2829
maven {
2930
url = uri("https://dl.bintray.com/open-telemetry/maven")
3031
}
3132
maven {
3233
url = uri("https://oss.jfrog.org/artifactory/oss-snapshot-local")
3334
}
34-
mavenCentral()
35+
maven {
36+
url = uri(extra.properties["artifactory_contextUrl"] as String + "/gradle")
37+
credentials {
38+
username = extra.properties["artifactory_user"] as String
39+
password = extra.properties["artifactory_password"] as String
40+
}
41+
}
3542
}
3643

3744
dependencies {

example-config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"service_name": "example",
3+
"reporting": {
4+
"queue_size": 1000,
5+
"traces_address": "http://localhost:9411/api/v2/spans"
6+
},
7+
"logging": {
8+
"level": "info",
9+
"outputPaths": [
10+
"stdout"
11+
]
12+
}
13+
}

javaagent/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
// update the dependencies also in the instrumentations sub-projects
99
// https://oss.jfrog.org/artifactory/oss-snapshot-local/io/opentelemetry/instrumentation/auto/
1010
implementation("io.opentelemetry.instrumentation.auto", "opentelemetry-javaagent", version = "0.9.0-20201008.091003-73", classifier = "all")
11+
implementation("ai.traceable.agent", "agent-config", version = "0.2.4")
1112
}
1213

1314
base.archivesBaseName = "hypertrace-agent"
@@ -24,6 +25,7 @@ tasks {
2425
}
2526

2627
shadowJar {
28+
relocate("com.fasterxml.jackson", "org.hypertrace.shaded.com.fasterxml.jackson")
2729
mergeServiceFiles {
2830
include("inst/META-INF/services/*")
2931
}
@@ -36,9 +38,9 @@ tasks {
3638
attributes.put("Implementation-Vendor", "Hypertrace.org")
3739
// TODO set to Github repository URL
3840
attributes.put("Implementation-Url", "https://hypertrace.org")
39-
attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
40-
attributes.put("Agent-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
41-
attributes.put("Premain-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
41+
attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
42+
attributes.put("Agent-Class", "org.hypertrace.agent.HypertraceAgent")
43+
attributes.put("Premain-Class", "org.hypertrace.agent.HypertraceAgent")
4244
attributes.put("Can-Redefine-Classes", true)
4345
attributes.put("Can-Retransform-Classes", true)
4446
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
18+
19+
import ai.traceable.agent.agentconfig.InstrumentationAgentConfig;
20+
import ai.traceable.agent.agentconfig.Reporting;
21+
import io.opentelemetry.javaagent.OpenTelemetryAgent;
22+
import java.lang.instrument.Instrumentation;
23+
24+
public class HypertraceAgent {
25+
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-environment-variables.md
26+
private static final String OTEL_EXPORTER = "otel.exporter";
27+
private static final String OTEL_EXPORTER_ZIPKIN_ENDPOINT = "otel.exporter.zipkin.endpoint";
28+
private static final String OTEL_EXPORTER_ZIPKIN_SERVICE_NAME =
29+
"otel.exporter.zipkin.service.name";
30+
private static final String OTEL_PROCESSOR_BATCH_MAX_QUEUE = "otel.bsp.max.queue.size";
31+
private static final String OTEL_DEFAULT_LOG_LEVEL =
32+
"io.opentelemetry.javaagent.slf4j.simpleLogger.defaultLogLevel";
33+
34+
public static void premain(String agentArgs, Instrumentation inst) {
35+
agentmain(agentArgs, inst);
36+
}
37+
38+
public static void agentmain(String agentArgs, Instrumentation inst) {
39+
configureOTELWithTraceableConfig(agentArgs);
40+
OpenTelemetryAgent.premain(agentArgs, inst);
41+
}
42+
43+
private static void configureOTELWithTraceableConfig(String agentArgs) {
44+
// The properties from traceable config file are set as default to OTEL config.
45+
InstrumentationAgentConfig traceableConfig = TraceableConfig.loadConfigFile(agentArgs);
46+
if (traceableConfig == null) {
47+
return;
48+
}
49+
50+
if (traceableConfig.getLogging() != null && traceableConfig.getLogging().getLevel() != null) {
51+
OpenTelemetryConfig.setDefault(
52+
OTEL_DEFAULT_LOG_LEVEL, traceableConfig.getLogging().getLevel());
53+
}
54+
55+
// set reporter to Zipkin because OpenTelemetry supports only Jaeger gRPC
56+
// which is not supported by OC collector in Hypertrace
57+
OpenTelemetryConfig.setDefault(OTEL_EXPORTER, "zipkin");
58+
59+
// TODO retry and backoff are not in OTEL. Maybe gRPC exporter does retry automatically?
60+
Reporting reporting = traceableConfig.getReporting();
61+
if (reporting != null) {
62+
if (reporting != null && reporting.getTracesAddress() != null) {
63+
OpenTelemetryConfig.setDefault(OTEL_EXPORTER_ZIPKIN_ENDPOINT, reporting.getTracesAddress());
64+
}
65+
if (reporting.getQueueSize() != null) {
66+
OpenTelemetryConfig.setDefault(
67+
OTEL_PROCESSOR_BATCH_MAX_QUEUE, Integer.toString(reporting.getQueueSize()));
68+
}
69+
}
70+
if (traceableConfig.getServiceName() != null) {
71+
OpenTelemetryConfig.setDefault(
72+
OTEL_EXPORTER_ZIPKIN_SERVICE_NAME, traceableConfig.getServiceName());
73+
}
74+
}
75+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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;
18+
19+
import java.io.File;
20+
import java.io.FileReader;
21+
import java.io.IOException;
22+
import java.util.Properties;
23+
24+
final class OpenTelemetryConfig {
25+
private static final String OTEL_CONF_FILE = "otel.trace.config";
26+
private static final Properties LOADED_OTEL_CONF_FILE = loadConfigurationFile();
27+
28+
private OpenTelemetryConfig() {}
29+
30+
/** Set default value for a property in OTEL trace config. */
31+
static void setDefault(String property, String value) {
32+
if (!isConfigured(property)) {
33+
System.setProperty(property, value);
34+
}
35+
}
36+
37+
private static boolean isConfigured(String propertyName) {
38+
return System.getProperty(propertyName) != null
39+
|| System.getenv(toEnvVarName(propertyName)) != null
40+
|| LOADED_OTEL_CONF_FILE.containsKey(propertyName);
41+
}
42+
43+
private static String toEnvVarName(String propertyName) {
44+
return propertyName.toUpperCase().replaceAll("\\.", "_");
45+
}
46+
47+
// Taken from
48+
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/9523f9ffe624f14a1fba1dbd40e0f7b489b005ae/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/ConfigInitializer.java#L51
49+
private static Properties loadConfigurationFile() {
50+
Properties properties = new Properties();
51+
52+
// Reading from system property first and from env after
53+
String configurationFilePath = System.getProperty(OTEL_CONF_FILE);
54+
if (configurationFilePath == null) {
55+
configurationFilePath = System.getenv(toEnvVarName(OTEL_CONF_FILE));
56+
}
57+
if (configurationFilePath == null) {
58+
return properties;
59+
}
60+
61+
// Normalizing tilde (~) paths for unix systems
62+
configurationFilePath =
63+
configurationFilePath.replaceFirst("^~", System.getProperty("user.home"));
64+
65+
File configurationFile = new File(configurationFilePath);
66+
if (!configurationFile.exists()) {
67+
return properties;
68+
}
69+
70+
try (FileReader fileReader = new FileReader(configurationFile)) {
71+
properties.load(fileReader);
72+
} catch (IOException ignored) {
73+
// OTel agent will log this error anyway
74+
}
75+
76+
return properties;
77+
}
78+
}
Lines changed: 75 additions & 0 deletions

0 commit comments

Comments
 (0)