|
| 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 | +} |
0 commit comments