From c66faa595cabf70f6989bcfbde34a14528e22ba1 Mon Sep 17 00:00:00 2001 From: Shashank Patidar <74622220+shashank11p@users.noreply.github.com> Date: Thu, 27 Apr 2023 09:56:34 +0530 Subject: [PATCH] added reporting config installer (#383) --- .../agent/core/config/ReportingConfig.java | 17 ++++++++-- .../config/ReportingConfigInstaller.java | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/ReportingConfigInstaller.java diff --git a/javaagent-core/src/main/java/org/hypertrace/agent/core/config/ReportingConfig.java b/javaagent-core/src/main/java/org/hypertrace/agent/core/config/ReportingConfig.java index 0d38fb042..4a6343dcb 100644 --- a/javaagent-core/src/main/java/org/hypertrace/agent/core/config/ReportingConfig.java +++ b/javaagent-core/src/main/java/org/hypertrace/agent/core/config/ReportingConfig.java @@ -33,8 +33,8 @@ final class ConfigProvider { private static volatile ReportingConfig reportingConfig; - private static ReportingConfig load() { - ServiceLoader configs = ServiceLoader.load(ReportingConfig.class); + private static ReportingConfig load(ClassLoader cl) { + ServiceLoader configs = ServiceLoader.load(ReportingConfig.class, cl); Iterator iterator = configs.iterator(); if (!iterator.hasNext()) { logger.error("Failed to load reporting config"); @@ -43,11 +43,22 @@ private static ReportingConfig load() { return iterator.next(); } + public static ReportingConfig get(ClassLoader cl) { + if (reportingConfig == null) { + synchronized (ConfigProvider.class) { + if (reportingConfig == null) { + reportingConfig = load(cl); + } + } + } + return reportingConfig; + } + public static ReportingConfig get() { if (reportingConfig == null) { synchronized (ConfigProvider.class) { if (reportingConfig == null) { - reportingConfig = load(); + reportingConfig = load(Thread.currentThread().getContextClassLoader()); } } } diff --git a/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/ReportingConfigInstaller.java b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/ReportingConfigInstaller.java new file mode 100644 index 000000000..5ad1ce2d8 --- /dev/null +++ b/otel-extensions/src/main/java/org/hypertrace/agent/otel/extensions/config/ReportingConfigInstaller.java @@ -0,0 +1,31 @@ +/* + * 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 io.opentelemetry.javaagent.tooling.BeforeAgentListener; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import org.hypertrace.agent.core.config.ReportingConfig.ConfigProvider; + +@AutoService(BeforeAgentListener.class) +public class ReportingConfigInstaller implements BeforeAgentListener { + + @Override + public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) { + ConfigProvider.get(getClass().getClassLoader()); + } +}