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

Fix Cassandra target #10357

Merged
merged 31 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b8043a4
Fix server.address for Cassandra
heyams Jan 30, 2024
d482ba4
Create experimental attributes extractor for cassandra
heyams Jan 31, 2024
127d2ad
Update
heyams Jan 31, 2024
be52518
Rename
heyams Jan 31, 2024
79c2d76
Fix tests
heyams Jan 31, 2024
6c16f6d
Update
heyams Jan 31, 2024
3774f3d
Rename back to CassandraAttributesExtractor
heyams Jan 31, 2024
02594ca
Consider SniEndpoint
heyams Feb 1, 2024
1858849
Fix tests
heyams Feb 1, 2024
7e1dd2a
Fix spotless
heyams Feb 1, 2024
415fc22
Use reflection to access proxyAddress
heyams Feb 5, 2024
c254dd7
Revert
heyams Feb 5, 2024
e18e666
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
heyams Feb 6, 2024
4e3c230
Cache proxyAddress field
heyams Feb 7, 2024
5e9a16f
Comment
heyams Feb 7, 2024
ea7f2a1
Merge remote-tracking branch 'upstream/main' into heya/fix-cassandra-…
heyams Feb 8, 2024
da7b1c9
Fix errorprone
heyams Feb 8, 2024
4ac21ca
Fix
heyams Feb 8, 2024
a1bca4e
Update
heyams Feb 8, 2024
d2af7b2
Address comments
heyams Feb 12, 2024
c66b91f
Comments
heyams Feb 13, 2024
447c362
empty
heyams Feb 14, 2024
7b9f479
empty
heyams Feb 14, 2024
f185da0
Use a diff data center to fix sporadic 'could not reach any contact p…
heyams Feb 14, 2024
bcb1cf2
Revert
heyams Feb 15, 2024
57424b3
Merge remote-tracking branch 'upstream/main' into heya/fix-cassandra-…
trask Feb 15, 2024
ccdb872
Use DefaultDriverConfigLoader.builder()
trask Feb 15, 2024
bc6ab22
Remove SniEndpoint testing
trask Feb 15, 2024
814290e
Fix test
heyams Feb 15, 2024
bce1ca4
Revert "Remove SniEndpoint testing"
trask Feb 15, 2024
a3694b3
One more try
trask Feb 15, 2024
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 @@ -9,13 +9,15 @@
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.cql.ExecutionInfo;
import com.datastax.oss.driver.api.core.cql.Statement;
import com.datastax.oss.driver.api.core.metadata.EndPoint;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;
import com.datastax.oss.driver.internal.core.metadata.SniEndPoint;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.semconv.SemanticAttributes;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.annotation.Nullable;

final class CassandraAttributesExtractor
Expand All @@ -38,12 +40,8 @@ public void onEnd(

Node coordinator = executionInfo.getCoordinator();
if (coordinator != null) {
SocketAddress address = coordinator.getEndPoint().resolve();
if (address instanceof InetSocketAddress) {
attributes.put(
SemanticAttributes.SERVER_ADDRESS, ((InetSocketAddress) address).getHostName());
attributes.put(SemanticAttributes.SERVER_PORT, ((InetSocketAddress) address).getPort());
}
updateServerAddressAndPort(attributes, coordinator);

if (coordinator.getDatacenter() != null) {
attributes.put(SemanticAttributes.DB_CASSANDRA_COORDINATOR_DC, coordinator.getDatacenter());
}
Expand Down Expand Up @@ -82,4 +80,22 @@ public void onEnd(
}
attributes.put(SemanticAttributes.DB_CASSANDRA_IDEMPOTENCE, idempotent);
}

private static void updateServerAddressAndPort(AttributesBuilder attributes, Node coordinator) {
EndPoint endPoint = coordinator.getEndPoint();
if (endPoint instanceof DefaultEndPoint) {
InetSocketAddress address = ((DefaultEndPoint) endPoint).resolve();
attributes.put(SemanticAttributes.SERVER_ADDRESS, address.getHostName());
attributes.put(SemanticAttributes.SERVER_PORT, address.getPort());
} else if (endPoint instanceof SniEndPoint) {
String serverName = ((SniEndPoint) endPoint).getServerName();
// parse serverName to get hostname and port
int index = serverName.indexOf(":");
if (index != -1) {
attributes.put(SemanticAttributes.SERVER_ADDRESS, serverName.substring(0, index));
attributes.put(
SemanticAttributes.SERVER_PORT, Integer.parseInt(serverName.substring(index + 1)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package io.opentelemetry.instrumentation.cassandra.v4_4;

import com.datastax.oss.driver.api.core.cql.ExecutionInfo;
import com.datastax.oss.driver.api.core.metadata.EndPoint;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;
import com.datastax.oss.driver.internal.core.metadata.SniEndPoint;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.annotation.Nullable;

final class CassandraNetworkAttributesGetter
Expand All @@ -28,7 +30,12 @@ public InetSocketAddress getNetworkPeerInetSocketAddress(
}
// resolve() returns an existing InetSocketAddress, it does not do a dns resolve,
// at least in the only current EndPoint implementation (DefaultEndPoint)
SocketAddress address = coordinator.getEndPoint().resolve();
return address instanceof InetSocketAddress ? (InetSocketAddress) address : null;
EndPoint endPoint = coordinator.getEndPoint();
if (endPoint instanceof DefaultEndPoint) {
return (InetSocketAddress) coordinator.getEndPoint().resolve();
} else if (endPoint instanceof SniEndPoint) {
return ((SniEndPoint) endPoint).resolve();
heyams marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import static org.junit.jupiter.api.Named.named;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.internal.core.metadata.SniEndPoint;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.cassandra.v4.common.AbstractCassandraTest;
import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -139,4 +144,23 @@ private static Stream<Arguments> provideReactiveParameters() {
"SELECT",
"users"))));
}

@Override
protected CqlSession getSession(String keyspace) {
DriverConfigLoader configLoader =
DriverConfigLoader.programmaticBuilder()
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(0))
.withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(10))
.build();
return wrap(
CqlSession.builder()
.addContactEndPoint(
new SniEndPoint(
new InetSocketAddress("localhost", cassandraPort),
"localhost:" + cassandraPort))
trask marked this conversation as resolved.
Show resolved Hide resolved
heyams marked this conversation as resolved.
Show resolved Hide resolved
.withConfigLoader(configLoader)
.withLocalDatacenter("datacenter1")
.withKeyspace(keyspace)
.build());
}
}
Loading