From d30fca6aa03732e3b1f7810099f872f70cbacd62 Mon Sep 17 00:00:00 2001 From: Gagan Juneja Date: Mon, 13 May 2024 12:53:29 +0530 Subject: [PATCH] Adds telemetry service Signed-off-by: Gagan Juneja --- .../main/java/org/opensearch/node/Node.java | 49 ++------- .../java/org/opensearch/plugins/Plugin.java | 7 ++ .../opensearch/plugins/PluginsService.java | 11 ++ .../telemetry/service/TelemetryService.java | 100 ++++++++++++++++++ .../telemetry/service/package-info.java | 12 +++ 5 files changed, 140 insertions(+), 39 deletions(-) create mode 100644 server/src/main/java/org/opensearch/telemetry/service/TelemetryService.java create mode 100644 server/src/main/java/org/opensearch/telemetry/service/package-info.java diff --git a/server/src/main/java/org/opensearch/node/Node.java b/server/src/main/java/org/opensearch/node/Node.java index 614f39166ea66..97b2d277b0034 100644 --- a/server/src/main/java/org/opensearch/node/Node.java +++ b/server/src/main/java/org/opensearch/node/Node.java @@ -242,6 +242,7 @@ import org.opensearch.telemetry.metrics.MetricsRegistry; import org.opensearch.telemetry.metrics.MetricsRegistryFactory; import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory; +import org.opensearch.telemetry.service.TelemetryService; import org.opensearch.telemetry.tracing.NoopTracerFactory; import org.opensearch.telemetry.tracing.Tracer; import org.opensearch.telemetry.tracing.TracerFactory; @@ -409,9 +410,6 @@ public static class DiscoverySettings { private final Collection pluginLifecycleComponents; private final LocalNodeFactory localNodeFactory; private final NodeService nodeService; - private final Tracer tracer; - - private final MetricsRegistry metricsRegistry; final NamedWriteableRegistry namedWriteableRegistry; private final AtomicReference runnableTaskListener; private FileCache fileCache; @@ -614,36 +612,9 @@ protected Node( ); } - TracerFactory tracerFactory; - MetricsRegistryFactory metricsRegistryFactory; - if (FeatureFlags.isEnabled(TELEMETRY)) { - final TelemetrySettings telemetrySettings = new TelemetrySettings(settings, clusterService.getClusterSettings()); - if (telemetrySettings.isTracingFeatureEnabled() || telemetrySettings.isMetricsFeatureEnabled()) { - List telemetryPlugins = pluginsService.filterPlugins(TelemetryPlugin.class); - TelemetryModule telemetryModule = new TelemetryModule(telemetryPlugins, telemetrySettings); - if (telemetrySettings.isTracingFeatureEnabled()) { - tracerFactory = new TracerFactory(telemetrySettings, telemetryModule.getTelemetry(), threadPool.getThreadContext()); - } else { - tracerFactory = new NoopTracerFactory(); - } - if (telemetrySettings.isMetricsFeatureEnabled()) { - metricsRegistryFactory = new MetricsRegistryFactory(telemetrySettings, telemetryModule.getTelemetry()); - } else { - metricsRegistryFactory = new NoopMetricsRegistryFactory(); - } - } else { - tracerFactory = new NoopTracerFactory(); - metricsRegistryFactory = new NoopMetricsRegistryFactory(); - } - } else { - tracerFactory = new NoopTracerFactory(); - metricsRegistryFactory = new NoopMetricsRegistryFactory(); - } - - tracer = tracerFactory.getTracer(); - metricsRegistry = metricsRegistryFactory.getMetricsRegistry(); - resourcesToClose.add(tracer::close); - resourcesToClose.add(metricsRegistry::close); + TelemetryService telemetryService = new TelemetryService(settings, clusterService.getClusterSettings(), pluginsService, threadPool); + pluginsService.setTelemetryService(telemetryService); + resourcesToClose.add(telemetryService); final ClusterInfoService clusterInfoService = newClusterInfoService(settings, clusterService, threadPool, client); final UsageService usageService = new UsageService(); @@ -982,7 +953,7 @@ protected Node( networkService, restController, clusterService.getClusterSettings(), - tracer, + telemetryService.getTracer(), transportInterceptors, secureSettingsFactories ); @@ -1016,7 +987,7 @@ protected Node( localNodeFactory, settingsModule.getClusterSettings(), taskHeaders, - tracer + telemetryService.getTracer() ); TopNSearchTasksLogger taskConsumer = new TopNSearchTasksLogger(settings, settingsModule.getClusterSettings()); transportService.getTaskManager().registerTaskResourceConsumer(taskConsumer); @@ -1320,10 +1291,11 @@ protected Node( b.bind(ResourceUsageCollectorService.class).toInstance(resourceUsageCollectorService); b.bind(SystemIndices.class).toInstance(systemIndices); b.bind(IdentityService.class).toInstance(identityService); - b.bind(Tracer.class).toInstance(tracer); + b.bind(Tracer.class).toInstance(telemetryService.getTracer()); b.bind(SearchRequestStats.class).toInstance(searchRequestStats); b.bind(SearchRequestSlowLog.class).toInstance(searchRequestSlowLog); - b.bind(MetricsRegistry.class).toInstance(metricsRegistry); + b.bind(TelemetryService.class).toInstance(telemetryService); + b.bind(MetricsRegistry.class).toInstance(telemetryService.getMetricsRegistry()); b.bind(RemoteClusterStateService.class).toProvider(() -> remoteClusterStateService); b.bind(RemoteIndexPathUploader.class).toProvider(() -> remoteIndexPathUploader); b.bind(PersistedStateRegistry.class).toInstance(persistedStateRegistry); @@ -1710,8 +1682,7 @@ public synchronized void close() throws IOException { toClose.add(injector.getInstance(NodeEnvironment.class)); toClose.add(stopWatch::stop); if (FeatureFlags.isEnabled(TELEMETRY)) { - toClose.add(injector.getInstance(Tracer.class)); - toClose.add(injector.getInstance(MetricsRegistry.class)); + toClose.add(injector.getInstance(TelemetryService.class)); } if (logger.isTraceEnabled()) { diff --git a/server/src/main/java/org/opensearch/plugins/Plugin.java b/server/src/main/java/org/opensearch/plugins/Plugin.java index 33c4155d12c25..e0421df013921 100644 --- a/server/src/main/java/org/opensearch/plugins/Plugin.java +++ b/server/src/main/java/org/opensearch/plugins/Plugin.java @@ -56,6 +56,7 @@ import org.opensearch.index.shard.IndexSettingProvider; import org.opensearch.repositories.RepositoriesService; import org.opensearch.script.ScriptService; +import org.opensearch.telemetry.service.TelemetryService; import org.opensearch.threadpool.ExecutorBuilder; import org.opensearch.threadpool.ThreadPool; import org.opensearch.watcher.ResourceWatcherService; @@ -278,4 +279,10 @@ public Collection getAdditionalIndexSettingProviders() { public Optional getSecureSettingFactory(Settings settings) { return Optional.empty(); } + + /** + * Called to set the {@link TelemetryService} and plugins can use it for generating the instrumentations. + * @param telemetryService telemetry service + */ + public void setTelemetryService(TelemetryService telemetryService){} } diff --git a/server/src/main/java/org/opensearch/plugins/PluginsService.java b/server/src/main/java/org/opensearch/plugins/PluginsService.java index a6eefd2f4fd17..4f067d11a4bf7 100644 --- a/server/src/main/java/org/opensearch/plugins/PluginsService.java +++ b/server/src/main/java/org/opensearch/plugins/PluginsService.java @@ -53,6 +53,7 @@ import org.opensearch.core.service.ReportingService; import org.opensearch.index.IndexModule; import org.opensearch.semver.SemverRange; +import org.opensearch.telemetry.service.TelemetryService; import org.opensearch.threadpool.ExecutorBuilder; import org.opensearch.transport.TransportSettings; @@ -312,6 +313,16 @@ public void onIndexModule(IndexModule indexModule) { } } + /** + * Sets TelemetryService to all the plugins. Which can be used to instrument the plugin code. + * @param telemetryService telemetry service + */ + public void setTelemetryService(TelemetryService telemetryService) { + for (Tuple plugin : plugins) { + plugin.v2().setTelemetryService(telemetryService); + } + } + /** * Get information about plugins and modules */ diff --git a/server/src/main/java/org/opensearch/telemetry/service/TelemetryService.java b/server/src/main/java/org/opensearch/telemetry/service/TelemetryService.java new file mode 100644 index 0000000000000..7f5dda6004085 --- /dev/null +++ b/server/src/main/java/org/opensearch/telemetry/service/TelemetryService.java @@ -0,0 +1,100 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.telemetry.service; + +import java.io.IOException; +import java.util.List; +import org.opensearch.common.annotation.ExperimentalApi; +import org.opensearch.common.lifecycle.AbstractLifecycleComponent; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; +import org.opensearch.common.util.FeatureFlags; +import org.opensearch.plugins.PluginsService; +import org.opensearch.plugins.TelemetryPlugin; +import org.opensearch.telemetry.TelemetryModule; +import org.opensearch.telemetry.TelemetrySettings; +import org.opensearch.telemetry.metrics.MetricsRegistry; +import org.opensearch.telemetry.metrics.MetricsRegistryFactory; +import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory; +import org.opensearch.telemetry.tracing.NoopTracerFactory; +import org.opensearch.telemetry.tracing.Tracer; +import org.opensearch.telemetry.tracing.TracerFactory; +import org.opensearch.threadpool.ThreadPool; + +import static org.opensearch.common.util.FeatureFlags.TELEMETRY; + +/** + * It initializes the telemetry plugin and creates the {@link TracerFactory} and {@link MetricsRegistryFactory} factories. + * It provides the access to {@link Tracer} and {@link MetricsRegistry} to other code paths for instrumentations. + * @opensearch.experimental + */ +@ExperimentalApi +public class TelemetryService extends AbstractLifecycleComponent { + + private TracerFactory tracerFactory = new NoopTracerFactory(); + private MetricsRegistryFactory metricsRegistryFactory = new NoopMetricsRegistryFactory(); + + /** + * Construcs the {@link TelemetryService} + * @param settings settings + * @param clusterSettings cluster settings. + * @param pluginsService plugin service. + * @param threadPool thread pool. + */ + public TelemetryService(Settings settings, ClusterSettings clusterSettings, PluginsService pluginsService, ThreadPool threadPool) { + init(settings, clusterSettings, pluginsService, threadPool); + } + + private void init(Settings settings, ClusterSettings clusterSettings, PluginsService pluginsService, ThreadPool threadPool) { + if (FeatureFlags.isEnabled(TELEMETRY)) { + final TelemetrySettings telemetrySettings = new TelemetrySettings(settings, clusterSettings); + if (telemetrySettings.isTracingFeatureEnabled() || telemetrySettings.isMetricsFeatureEnabled()) { + List telemetryPlugins = pluginsService.filterPlugins(TelemetryPlugin.class); + TelemetryModule telemetryModule = new TelemetryModule(telemetryPlugins, telemetrySettings); + if (telemetrySettings.isTracingFeatureEnabled()) { + tracerFactory = new TracerFactory(telemetrySettings, telemetryModule.getTelemetry(), threadPool.getThreadContext()); + } + if (telemetrySettings.isMetricsFeatureEnabled()) { + metricsRegistryFactory = new MetricsRegistryFactory(telemetrySettings, telemetryModule.getTelemetry()); + } + } + } + } + + /** + * Returns the tracer object. If tracer or telemetry plugin is not configured then returns {@link org.opensearch.telemetry.tracing.noop.NoopTracer} + * will be returned. + * @return tracer. + */ + public Tracer getTracer(){ + return tracerFactory.getTracer(); + } + + /** + * Returns the {@link MetricsRegistry} object. If metrics registry or telemetry plugin is not configured then returns {@link MetricsRegistry} + * @return + */ + public MetricsRegistry getMetricsRegistry(){ + return metricsRegistryFactory.getMetricsRegistry(); + } + + @Override + protected void doStart() { + } + + @Override + protected void doStop() { + } + + @Override + protected void doClose() throws IOException { + metricsRegistryFactory.getMetricsRegistry().close(); + tracerFactory.getTracer().close(); + } +} diff --git a/server/src/main/java/org/opensearch/telemetry/service/package-info.java b/server/src/main/java/org/opensearch/telemetry/service/package-info.java new file mode 100644 index 0000000000000..04cbec13c218b --- /dev/null +++ b/server/src/main/java/org/opensearch/telemetry/service/package-info.java @@ -0,0 +1,12 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** + * This package contains classes needed for telemetry. + */ +package org.opensearch.telemetry.service;