Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RD-13777-Remove-Mongo-IsMaster-spans-sampler #599

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ The Lumigo OpenTelemetry Distro for Java additionally supports the following con
* `LUMIGO_FILTER_HTTP_ENDPOINTS_REGEX_CLIENT` applies the filter to client spans only. Matching is performed against the following attributes on a span: `url.full`, and `http.url`[^2].
* `LUMIGO_ENABLE_LOGS=true`: Turns on the logging instrumentation to capture log-records, for logging libraries that support open-telemetry (e.g. Logback). By default, the logging instrumentation is disabled.
* `LUMIGO_ENABLE_TRACES=true`: Turns on the tracing instrumentation. By default, the tracing instrumentation is enabled.
* `LUMIGO_REDUCED_MONGO_INSTRUMENTATION=true`: Reduces the amount of data collected by the MongoDB instrumentation, such as not collecting the `db.operation` attribute `isMaster`. By default, the MongoDB instrumentation reduces the amount of data collected.

For more configuration options, see the [Upstream Agent Configuration](https://opentelemetry.io/docs/instrumentation/java/automatic/agent-config/).

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2023 Lumigo LTD
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package io.lumigo.javaagent;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSamplerBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.util.logging.Logger;

@AutoService(AutoConfigurationCustomizerProvider.class)
public class MongoSamplingConfigurer implements AutoConfigurationCustomizerProvider {
private static final Logger LOGGER = Logger.getLogger(MongoSamplingConfigurer.class.getName());

public static final String LUMIGO_REDUCED_MONGO_INSTRUMENTATION =
"lumigo.reduced.mongo.instrumentation";

@Override
public void customize(AutoConfigurationCustomizer autoConfiguration) {
autoConfiguration.addSamplerCustomizer(MongoSamplingConfigurer::customizeMongoSpans);
}

private static Sampler customizeMongoSpans(
Sampler defaultSampler, ConfigProperties configProperties) {

RuleBasedRoutingSamplerBuilder samplerBuilder =
RuleBasedRoutingSampler.builder(SpanKind.CLIENT, defaultSampler);

String reduceMongoInstrumentation =
configProperties.getString(LUMIGO_REDUCED_MONGO_INSTRUMENTATION);
boolean isReducedMongoInstrumentationEnabled;

if (reduceMongoInstrumentation == null || reduceMongoInstrumentation.isEmpty()) {
isReducedMongoInstrumentationEnabled = true; // Default to true
} else if (reduceMongoInstrumentation.equalsIgnoreCase("true")) {
isReducedMongoInstrumentationEnabled = true;
} else if (reduceMongoInstrumentation.equalsIgnoreCase("false")) {
isReducedMongoInstrumentationEnabled = false;
} else {
LOGGER.warning(
"Invalid value for LUMIGO_REDUCED_MONGO_INSTRUMENTATION: "
+ reduceMongoInstrumentation
+ ". Defaulting to true.");
isReducedMongoInstrumentationEnabled = true;
}

if (isReducedMongoInstrumentationEnabled) {

// Log that Lumigo reduces Mongo instrumentation by omitting certain attributes, like
// `db.operation`
// For example, the `isMaster` operation is not collected by default to optimize performance.
// Setting the environment variable `LUMIGO_REDUCED_MONGO_INSTRUMENTATION=false` will disable
// this optimization.
LOGGER.finest(
"Lumigo reduces Mongo instrumentation. The `db.operation` attribute (e.g., `isMaster`) is excluded by default. Set `LUMIGO_REDUCED_MONGO_INSTRUMENTATION=false` to disable this behavior.");

// Define attribute keys
AttributeKey<String> dbSystemKey = AttributeKey.stringKey("db.system");
AttributeKey<String> dbOperationKey = AttributeKey.stringKey("db.operation");

samplerBuilder.customize(
dbSystemKey,
"mongodb",
RuleBasedRoutingSampler.builder(SpanKind.CLIENT, defaultSampler)
.drop(dbOperationKey, "isMaster")
.build());
}

return samplerBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 Lumigo LTD
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package io.lumigo.javaagent;

import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractSamplingConfiguratorTest {
protected static void addPropertiesCustomizer(AutoConfiguredOpenTelemetrySdkBuilder builder) {
builder.addPropertiesCustomizer(
config -> {
Map<String, String> overrides = new HashMap<>();
overrides.put("otel.traces.exporter", "none");
overrides.put("otel.metrics.exporter", "none");
overrides.put("otel.logs.exporter", "none");
return overrides;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2024 Lumigo LTD
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package io.lumigo.javaagent;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class MongoSamplingConfiguratorTest extends AbstractSamplingConfiguratorTest {
@AfterEach
public void tearDown() {
System.clearProperty(MongoSamplingConfigurer.LUMIGO_REDUCED_MONGO_INSTRUMENTATION);
}

@Test
public void shouldDropByDefault() {
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder();

new MongoSamplingConfigurer().customize(builder);
addPropertiesCustomizer(builder);

AutoConfiguredOpenTelemetrySdk sdk = builder.build();

SdkTracerProvider tracerProvider = sdk.getOpenTelemetrySdk().getSdkTracerProvider();
Sampler sampler = tracerProvider.getSampler();

Attributes attributes =
Attributes.of(
AttributeKey.stringKey("db.system"), "mongodb",
AttributeKey.stringKey("db.operation"), "isMaster");
SamplingResult result =
sampler.shouldSample(
Context.root(),
IdGenerator.random().generateTraceId(),
"name",
SpanKind.CLIENT,
attributes,
Collections.emptyList());
Assertions.assertEquals(SamplingResult.drop(), result);
}

@Test
public void shouldNotDropIfFalse() {
System.setProperty(MongoSamplingConfigurer.LUMIGO_REDUCED_MONGO_INSTRUMENTATION, "false");
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder();

new MongoSamplingConfigurer().customize(builder);
addPropertiesCustomizer(builder);

AutoConfiguredOpenTelemetrySdk sdk = builder.build();

SdkTracerProvider tracerProvider = sdk.getOpenTelemetrySdk().getSdkTracerProvider();
Sampler sampler = tracerProvider.getSampler();

Attributes attributes =
Attributes.of(
AttributeKey.stringKey("db.system"), "mongodb",
AttributeKey.stringKey("db.operation"), "isMaster");
SamplingResult result =
sampler.shouldSample(
Context.root(),
IdGenerator.random().generateTraceId(),
"name",
SpanKind.CLIENT,
attributes,
Collections.emptyList());
Assertions.assertEquals(SamplingResult.recordAndSample(), result);
}

@Test
public void shouldNotDropIfFind() {
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder();

new MongoSamplingConfigurer().customize(builder);
addPropertiesCustomizer(builder);

AutoConfiguredOpenTelemetrySdk sdk = builder.build();

SdkTracerProvider tracerProvider = sdk.getOpenTelemetrySdk().getSdkTracerProvider();
Sampler sampler = tracerProvider.getSampler();

Attributes attributes =
Attributes.of(
AttributeKey.stringKey("db.system"), "mongodb",
AttributeKey.stringKey("db.operation"), "find");
SamplingResult result =
sampler.shouldSample(
Context.root(),
IdGenerator.random().generateTraceId(),
"name",
SpanKind.CLIENT,
attributes,
Collections.emptyList());
Assertions.assertEquals(SamplingResult.recordAndSample(), result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SamplingConfiguratorTest {
public class SamplingConfiguratorTest extends AbstractSamplingConfiguratorTest {
@Test
public void testCustomize() {
System.setProperty(
Expand All @@ -43,14 +41,7 @@ public void testCustomize() {
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder();

new SamplingConfigurer().customize(builder);
builder.addPropertiesCustomizer(
config -> {
Map<String, String> overrides = new HashMap<>();
overrides.put("otel.traces.exporter", "none");
overrides.put("otel.metrics.exporter", "none");
overrides.put("otel.logs.exporter", "none");
return overrides;
});
addPropertiesCustomizer(builder);

AutoConfiguredOpenTelemetrySdk sdk = builder.build();

Expand Down
Loading