Skip to content

Commit

Permalink
Merge pull request #292 from solarwinds/NH-98000
Browse files Browse the repository at this point in the history
NH-98000: export only swo metrics when disabled.
  • Loading branch information
cleverchuk authored Dec 18, 2024
2 parents d8fa20b + 39f429c commit 8494d40
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.solarwinds.opentelemetry.extensions;

import com.solarwinds.joboe.config.ConfigManager;
import com.solarwinds.joboe.config.ConfigProperty;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import java.util.stream.Collectors;
import lombok.NonNull;

public class DelegatingMetricExporter implements MetricExporter {
Expand All @@ -33,7 +36,19 @@ public DelegatingMetricExporter(MetricExporter delegate) {

@Override
public CompletableResultCode export(@NonNull Collection<MetricData> metrics) {
return delegate.export(metrics);
if (ConfigManager.getConfigOptional(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED, true)) {
return delegate.export(metrics);
}

return delegate.export(
metrics.stream()
.filter(
metricData ->
MeterProvider.samplingMeterScopeName.equals(
metricData.getInstrumentationScopeInfo().getName())
|| MeterProvider.requestMeterScopeName.equals(
metricData.getInstrumentationScopeInfo().getName()))
.collect(Collectors.toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@

public class MeterProvider {

public static final String samplingMeterName = "sw.apm.sampling.metrics";
public static final String samplingMeterScopeName = "sw.apm.sampling.metrics";

public static final String requestMeterName = "sw.apm.request.metrics";
public static final String requestMeterScopeName = "sw.apm.request.metrics";

public static Meter getSamplingMetricsMeter() {
return GlobalOpenTelemetry.meterBuilder(samplingMeterName)
return GlobalOpenTelemetry.meterBuilder(samplingMeterScopeName)
.setInstrumentationVersion(BuildConfig.SOLARWINDS_AGENT_VERSION)
.build();
}

public static Meter getRequestMetricsMeter() {
return GlobalOpenTelemetry.meterBuilder(requestMeterName)
return GlobalOpenTelemetry.meterBuilder(requestMeterScopeName)
.setInstrumentationVersion(BuildConfig.SOLARWINDS_AGENT_VERSION)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.solarwinds.joboe.config.ConfigManager;
import com.solarwinds.joboe.config.ConfigProperty;
import com.solarwinds.joboe.config.InvalidConfigException;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -37,12 +47,38 @@ class DelegatingMetricExporterTest {

@Mock private MetricExporter metricExporterMock;

@Mock private MetricData metricDataMock;

@Mock private MetricData metricDataMock0;

@Mock private MetricData metricDataMock1;

@Captor private ArgumentCaptor<List<MetricData>> metricData;

@Test
void verifyThatExportIsDelegated() {
tested.export(Collections.emptyList());
void verifyThatAllMetricDataAreExported() {
tested.export(Collections.singleton(metricDataMock));
verify(metricExporterMock).export(any());
}

@Test
void verifyThatOnlySwoMetricDataAreExported() throws InvalidConfigException {
ConfigManager.setConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED, false);

when(metricDataMock.getInstrumentationScopeInfo())
.thenReturn(InstrumentationScopeInfo.create(MeterProvider.requestMeterScopeName));
when(metricDataMock0.getInstrumentationScopeInfo())
.thenReturn(InstrumentationScopeInfo.create(MeterProvider.samplingMeterScopeName));
when(metricDataMock1.getInstrumentationScopeInfo())
.thenReturn(InstrumentationScopeInfo.create("test-scope"));

tested.export(Arrays.asList(metricDataMock, metricDataMock0, metricDataMock1));
verify(metricExporterMock).export(metricData.capture());
assertEquals(Arrays.asList(metricDataMock, metricDataMock0), metricData.getValue());

ConfigManager.removeConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED);
}

@Test
void verifyThatFlushIsDelegated() {
tested.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,41 +235,38 @@ static void configureOtelLogExport(ConfigContainer container) {
}

static void configureOtelMetricExport(ConfigContainer container) {
Boolean exportMetrics = (Boolean) container.get(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED);
if (exportMetrics == null || exportMetrics) {
String serviceKey = (String) container.get(ConfigProperty.AGENT_SERVICE_KEY);
String apiKey = ServiceKeyUtils.getApiKey(serviceKey);
String serviceKey = (String) container.get(ConfigProperty.AGENT_SERVICE_KEY);
String apiKey = ServiceKeyUtils.getApiKey(serviceKey);
String dataCell = "na-01";

String dataCell = "na-01";
String env = "cloud";
String collectorEndpoint = (String) container.get(ConfigProperty.AGENT_COLLECTOR);
String env = "cloud";
String collectorEndpoint = (String) container.get(ConfigProperty.AGENT_COLLECTOR);
if (collectorEndpoint != null) {
if (collectorEndpoint.contains("appoptics.com")) {
return;
}

if (collectorEndpoint != null) {
if (collectorEndpoint.contains("appoptics.com")) {
return;
}
collectorEndpoint = collectorEndpoint.split(":")[0];
String[] fragments = collectorEndpoint.split("\\.");
if (fragments.length > 2) {
// This is based on knowledge of the SWO url format where the third name from the left in
// the domain is the data-cell name and assumes this format will stay stable.
dataCell = fragments[2];
}
collectorEndpoint = collectorEndpoint.split(":")[0];
String[] fragments = collectorEndpoint.split("\\.");
if (fragments.length > 2) {
// This is based on knowledge of the SWO url format where the third name from the left in
// the domain is the data-cell name and assumes this format will stay stable.
dataCell = fragments[2];
}

if (fragments.length > 3) {
env = fragments[3];
}
if (fragments.length > 3) {
env = fragments[3];
}
}

setSystemProperty("otel.exporter.otlp.metrics.protocol", "grpc");
setSystemProperty("otel.metrics.exporter", "otlp");
setSystemProperty(
"otel.exporter.otlp.metrics.headers", String.format("authorization=Bearer %s", apiKey));
setSystemProperty("otel.exporter.otlp.metrics.protocol", "grpc");
setSystemProperty("otel.metrics.exporter", "otlp");
setSystemProperty(
"otel.exporter.otlp.metrics.headers", String.format("authorization=Bearer %s", apiKey));

setEndpoint(
"otel.exporter.otlp.metrics.endpoint",
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env));
}
setEndpoint(
"otel.exporter.otlp.metrics.endpoint",
String.format("https://otel.collector.%s.%s.solarwinds.com", dataCell, env));
}

static void configureOtelTraceExport(ConfigContainer container) {
Expand Down Expand Up @@ -630,12 +627,8 @@ public static void processConfigs(ConfigContainer configs) throws InvalidConfigE
}

public static boolean shouldUseOtlpForMetrics() {
Boolean enabled =
(Boolean) ConfigManager.getConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED);

String collectorEndpoint = (String) ConfigManager.getConfig(ConfigProperty.AGENT_COLLECTOR);
return (enabled == null || enabled)
&& (collectorEndpoint == null || !collectorEndpoint.contains("appoptics.com"));
return collectorEndpoint == null || !collectorEndpoint.contains("appoptics.com");
}

private static void setSystemProperty(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,6 @@ void returnFalseWhenCollectorIsAOForMetric() throws InvalidConfigException {
ConfigManager.removeConfig(ConfigProperty.AGENT_COLLECTOR);
}

@Test
void returnFalseWhenDisabledForMetric() throws InvalidConfigException {
ConfigManager.setConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED, false);
assertFalse(ConfigurationLoader.shouldUseOtlpForMetrics());
ConfigManager.removeConfig(ConfigProperty.AGENT_EXPORT_METRICS_ENABLED);
}

@Test
void returnEnvironmentVariableEquivalent() {
assertEquals(
Expand Down

0 comments on commit 8494d40

Please sign in to comment.