TracerInstaller.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright The OpenTelemetry 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. package io.opentelemetry.auto.tooling;
  17. import com.google.common.annotations.VisibleForTesting;
  18. import io.opentelemetry.auto.config.Config;
  19. import io.opentelemetry.auto.exportersupport.MetricExporterFactory;
  20. import io.opentelemetry.sdk.OpenTelemetrySdk;
  21. import io.opentelemetry.sdk.contrib.auto.config.SpanExporterFactory;
  22. import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
  23. import io.opentelemetry.sdk.metrics.export.MetricExporter;
  24. import io.opentelemetry.sdk.trace.export.SimpleSpansProcessor;
  25. import io.opentelemetry.sdk.trace.export.SpanExporter;
  26. import java.io.File;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import java.util.Collections;
  30. import java.util.Iterator;
  31. import java.util.ServiceLoader;
  32. import lombok.extern.slf4j.Slf4j;
  33. @Slf4j
  34. public class TracerInstaller {
  35. /** Register agent tracer if no agent tracer is already registered. */
  36. public static synchronized void installAgentTracer() {
  37. if (Config.get().isTraceEnabled()) {
  38. // Try to create an exporter
  39. final String exporterJar = Config.get().getExporterJar();
  40. if (exporterJar != null) {
  41. installExportersFromJar(exporterJar);
  42. } else {
  43. log.warn("No exporter is specified. Tracing will run but spans are dropped");
  44. }
  45. } else {
  46. log.info("Tracing is disabled.");
  47. }
  48. }
  49. @VisibleForTesting
  50. private static synchronized void installExportersFromJar(final String exporterJar) {
  51. final URL url;
  52. try {
  53. url = new File(exporterJar).toURI().toURL();
  54. } catch (final MalformedURLException e) {
  55. log.warn("Filename could not be parsed: " + exporterJar + ". Exporter is not installed");
  56. log.warn("No valid exporter found. Tracing will run but spans are dropped");
  57. return;
  58. }
  59. final DefaultExporterConfig config = new DefaultExporterConfig("exporter");
  60. final ExporterClassLoader exporterLoader =
  61. new ExporterClassLoader(new URL[] {url}, TracerInstaller.class.getClassLoader());
  62. final SpanExporterFactory spanExporterFactory =
  63. getExporterFactory(SpanExporterFactory.class, exporterLoader);
  64. if (spanExporterFactory != null) {
  65. final SpanExporter spanExporter = spanExporterFactory.fromConfig(config);
  66. OpenTelemetrySdk.getTracerProvider()
  67. .addSpanProcessor(SimpleSpansProcessor.create(spanExporter));
  68. log.info("Installed span exporter: " + spanExporter.getClass().getName());
  69. } else {
  70. log.warn("No matching providers in jar " + exporterJar);
  71. log.warn("No valid exporter found. Tracing will run but spans are dropped");
  72. }
  73. final MetricExporterFactory metricExporterFactory =
  74. getExporterFactory(MetricExporterFactory.class, exporterLoader);
  75. if (metricExporterFactory != null) {
  76. final MetricExporter metricExporter = metricExporterFactory.fromConfig(config);
  77. IntervalMetricReader.builder()
  78. .setMetricExporter(metricExporter)
  79. .setMetricProducers(
  80. Collections.singleton(OpenTelemetrySdk.getMeterProvider().getMetricProducer()))
  81. .build();
  82. log.info("Installed metric exporter: " + metricExporter.getClass().getName());
  83. }
  84. }
  85. private static <F> F getExporterFactory(
  86. final Class<F> service, final ExporterClassLoader exporterLoader) {
  87. final ServiceLoader<F> serviceLoader = ServiceLoader.load(service, exporterLoader);
  88. final Iterator<F> i = serviceLoader.iterator();
  89. if (i.hasNext()) {
  90. final F factory = i.next();
  91. if (i.hasNext()) {
  92. log.warn(
  93. "Exporter JAR defines more than one {}. Only the first one found will be used",
  94. service.getName());
  95. }
  96. return factory;
  97. }
  98. return null;
  99. }
  100. public static void logVersionInfo() {
  101. VersionLogger.logAllVersions();
  102. log.debug(
  103. AgentInstaller.class.getName() + " loaded on " + AgentInstaller.class.getClassLoader());
  104. }
  105. }