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

Create the JDBC spans as exit spans #2484

Merged
merged 2 commits into from
Mar 1, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ endif::[]
[[release-notes-1.29.1]]
==== 1.29.1 - YYYY/MM/DD

[float]
===== Potentially breaking changes
* Create the JDBC spans as exit spans- {pull}2484[#2484]

[float]
===== Features
* Added support for setting service name and version for a transaction via the public api - {pull}2451[#2451]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package co.elastic.apm.agent;

import co.elastic.apm.agent.configuration.SpyConfiguration;
import co.elastic.apm.agent.impl.metadata.MetaData;
import co.elastic.apm.agent.impl.context.Destination;
import co.elastic.apm.agent.impl.error.ErrorCapture;
import co.elastic.apm.agent.impl.metadata.MetaData;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.Outcome;
Expand Down Expand Up @@ -69,7 +69,9 @@ public class MockReporter implements Reporter {

// A set of exit span subtypes that do not support address and port discovery
private static final Set<String> SPAN_TYPES_WITHOUT_ADDRESS;
// A map of exit span type to actions that that do not support address and port discovery
// A map of exit span type to subtypes that do not support address and port discovery
private static final Map<String, Collection<String>> SPAN_SUBTYPES_WITHOUT_ADDRESS;
// A map of exit span subtypes to actions that do not support address and port discovery
private static final Map<String, Collection<String>> SPAN_ACTIONS_WITHOUT_ADDRESS;
// And for any case the disablement of the check cannot rely on subtype (eg Redis, where Jedis supports and Lettuce does not)
private boolean checkDestinationAddress = true;
Expand Down Expand Up @@ -99,6 +101,7 @@ public class MockReporter implements Reporter {

static {
SPAN_TYPES_WITHOUT_ADDRESS = Set.of("jms");
SPAN_SUBTYPES_WITHOUT_ADDRESS = Map.of("db", Set.of("h2", "unknown"));
SPAN_ACTIONS_WITHOUT_ADDRESS = Map.of("kafka", Set.of("poll"));
}

Expand Down Expand Up @@ -252,9 +255,11 @@ private void verifyDestinationFields(Span span) {
}
Destination destination = span.getContext().getDestination();
if (checkDestinationAddress && !SPAN_TYPES_WITHOUT_ADDRESS.contains(span.getSubtype())) {
// see if this span's subtype is not supported for its type
Collection<String> unsupportedSubtypes = SPAN_SUBTYPES_WITHOUT_ADDRESS.getOrDefault(span.getType(), Collections.emptySet());
// see if this span's action is not supported for its subtype
Collection<String> unsupportedActions = SPAN_ACTIONS_WITHOUT_ADDRESS.getOrDefault(span.getSubtype(), Collections.emptySet());
if (!unsupportedActions.contains(span.getAction())) {
if (!(unsupportedSubtypes.contains(span.getSubtype()) || unsupportedActions.contains(span.getAction()))) {
assertThat(destination.getAddress()).describedAs("destination address is required").isNotEmpty();
assertThat(destination.getPort()).describedAs("destination port is required").isGreaterThan(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.Span;
import co.elastic.apm.agent.jdbc.JdbcFilter;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;

import javax.annotation.Nullable;
import java.sql.Connection;
Expand Down Expand Up @@ -87,7 +87,13 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable Abs
return null;
}

Span span = parent.createSpan().activate();
Span span = parent.createExitSpan();
if (span == null) {
return null;
} else {
span.activate();
}

if (sql.isEmpty()) {
span.withName("empty query");
} else if (span.isSampled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ private void assertSpanRecordedWithoutConnection(String rawSql, boolean prepared
assertThat(destination.getPort()).isLessThanOrEqualTo(0);

Destination.Service service = destination.getService();
assertThat(service.getResource()).isNullOrEmpty();
assertThat(service.getResource().toString()).isEqualTo("unknown");
}

private static long[] toLongArray(int[] a) {
Expand Down