Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,44 @@
import com.solarwinds.joboe.config.ConfigProperty;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.trace.ReadableSpan;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SpanStacktraceFilter implements Predicate<ReadableSpan> {
private Set<String> filterAttributes = new HashSet<>();
private static final String DEFAULT_SELECTOR_ATTRIBUTE = "db.system";

private Set<String> selectorAttributes = new HashSet<>();

public SpanStacktraceFilter() {}

public SpanStacktraceFilter(Collection<String> configuredFilterAttributes) {
Set<String> effectiveFilterAttributes = new HashSet<>(configuredFilterAttributes);
effectiveFilterAttributes.add(DEFAULT_SELECTOR_ATTRIBUTE);
selectorAttributes = effectiveFilterAttributes;
}

@Override
public boolean test(ReadableSpan readableSpan) {
if (filterAttributes.isEmpty()) {
if (selectorAttributes.isEmpty()) {
Set<String> configuredFilterAttributes =
ConfigManager.getConfigOptional(
ConfigProperty.AGENT_SPAN_STACKTRACE_FILTERS, Collections.singleton("db.system"));
ConfigProperty.AGENT_SPAN_STACKTRACE_FILTERS,
Collections.singleton(DEFAULT_SELECTOR_ATTRIBUTE));

Set<String> effectiveFilterAttributes = new HashSet<>(configuredFilterAttributes);
effectiveFilterAttributes.add("db.system");
filterAttributes = effectiveFilterAttributes;
effectiveFilterAttributes.add(DEFAULT_SELECTOR_ATTRIBUTE);
selectorAttributes = effectiveFilterAttributes;
}

Set<String> attributes =
readableSpan.getAttributes().asMap().keySet().stream()
.map(AttributeKey::getKey)
.collect(Collectors.toSet());

return filterAttributes.stream().anyMatch(attributes::contains);
return selectorAttributes.stream().anyMatch(attributes::contains);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.solarwinds.opentelemetry.extensions.config.provider;

import static com.solarwinds.opentelemetry.extensions.SharedNames.SPAN_STACKTRACE_FILTER_CLASS;

import com.google.auto.service.AutoService;
import com.solarwinds.joboe.config.ConfigManager;
import com.solarwinds.joboe.config.ConfigProperty;
Expand Down Expand Up @@ -52,7 +50,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@AutoService(DeclarativeConfigurationCustomizerProvider.class)
public class SharedConfigCustomizerProvider implements DeclarativeConfigurationCustomizerProvider {
Expand Down Expand Up @@ -88,7 +85,6 @@ public void customize(DeclarativeConfigurationCustomizer customizer) {
addMetricExporter(configurationModel);
if (tracerProvider != null) {
addSampler(tracerProvider);
addProcessors(tracerProvider);

Comment thread
cleverchuk marked this conversation as resolved.
Resolved
addSpanExporter(configurationModel);
}
Expand All @@ -110,44 +106,6 @@ private static Map<String, Object> properties(Object... keyValues) {
return properties;
}

private void addProcessors(TracerProviderModel model) {
List<SpanProcessorModel> spanProcessorModels = model.getProcessors();
if (spanProcessorModels == null) {
spanProcessorModels = new ArrayList<>();
}

ArrayList<SpanProcessorModel> allProcessors = new ArrayList<>(spanProcessorModels);
String experimentalStacktrace = "stacktrace/development";
Optional<SpanProcessorModel> modelOptional =
spanProcessorModels.stream()
.filter(
processorModel ->
processorModel.getExtensionProperties().containsKey(experimentalStacktrace))
.findFirst();

if (!modelOptional.isPresent()) {
allProcessors.add(
new SpanProcessorModel()
.setExtensionProperty(
experimentalStacktrace, properties("filter", SPAN_STACKTRACE_FILTER_CLASS)));
} else {
SpanProcessorModel spanProcessorModel = modelOptional.get();
Object existing = spanProcessorModel.getExtensionProperties().get(experimentalStacktrace);
Map<String, Object> stacktraceProperties = new LinkedHashMap<>();

if (existing instanceof Map) {
((Map<?, ?>) existing).forEach((k, v) -> stacktraceProperties.put(String.valueOf(k), v));
}

if (!stacktraceProperties.containsKey("filter")) {
stacktraceProperties.put("filter", SPAN_STACKTRACE_FILTER_CLASS);
spanProcessorModel.setExtensionProperty(experimentalStacktrace, stacktraceProperties);
}
}

model.setProcessors(allProcessors);
}

private void addSampler(TracerProviderModel model) {
SamplerModel sampler = model.getSampler();
if (sampler == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* © SolarWinds Worldwide, LLC. All rights reserved.
*
* 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 com.solarwinds.opentelemetry.extensions.config.provider;

import com.google.auto.service.AutoService;
import com.solarwinds.opentelemetry.extensions.SpanStacktraceFilter;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.contrib.stacktrace.StackTraceComponentProvider;
import io.opentelemetry.contrib.stacktrace.StackTraceSpanProcessor;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.trace.SpanProcessor;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Drop-in replacement for {@code stacktrace/development} that defaults the span filter to {@link
* SpanStacktraceFilter}. The span attribute keys that trigger capture are set with {@code
* selectors}; {@code db.system} always applies.
*/
@AutoService(ComponentProvider.class)
public class StacktraceComponentProvider implements ComponentProvider {

public static final String COMPONENT_NAME = "swo_stacktrace";

private static final long DEFAULT_MIN_DURATION_MILLIS = 5;

@Override
public Class<SpanProcessor> getType() {
return SpanProcessor.class;
}

@Override
public String getName() {
return COMPONENT_NAME;
}

@Override
public SpanProcessor create(DeclarativeConfigProperties declarativeConfigProperties) {
long minDurationMillis =
declarativeConfigProperties.getLong("min_duration", DEFAULT_MIN_DURATION_MILLIS);

if (minDurationMillis < 0) {
return SpanProcessor.composite();
}

if (declarativeConfigProperties.getString("filter") != null) {
return new StackTraceComponentProvider().create(declarativeConfigProperties);
}

List<String> selectors =
declarativeConfigProperties.getScalarList(
"selectors", String.class, Collections.emptyList());
SpanStacktraceFilter filter = new SpanStacktraceFilter(selectors);

return new StackTraceSpanProcessor(TimeUnit.MILLISECONDS.toNanos(minDurationMillis), filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.trace.ReadableSpan;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -109,6 +110,18 @@ void returnTrueWhenSpanHasCustomConfiguredLongArrayAttribute() throws InvalidCon
assertTrue(filter.test(span));
}

@Test
void explicitFiltersAreUsedWithDbSystemDefault() {
SpanStacktraceFilter filter = new SpanStacktraceFilter(Collections.singletonList("process.id"));

assertTrue(
filter.test(createSpanWithAttributes(Attributes.builder().put("process.id", 1).build())));
assertTrue(
filter.test(createSpanWithAttributes(Attributes.builder().put("db.system", "x").build())));
assertFalse(
filter.test(createSpanWithAttributes(Attributes.builder().put("other", "x").build())));
}

private ReadableSpan createSpanWithAttributes(Attributes attributes) {
ReadableSpan span = mock(ReadableSpan.class);
when(span.getAttributes()).thenReturn(attributes);
Expand Down

This file was deleted.

Loading
Loading