Skip to content

Commit

Permalink
Merge pull request #1525 from newrelic/saxon-db-attr-pr-dup
Browse files Browse the repository at this point in the history
Add database and external span attributes to correlate to metric data (Duplicate)
  • Loading branch information
obenkenobi authored Oct 6, 2023
2 parents 0e70cdf + a53fcfc commit 9b4c2fe
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public SpanEventFactory putAllUserAttributes(Map<String, ?> userAttributes) {
return this;
}


public SpanEventFactory putAllUserAttributesIfAbsent(Map<String, ?> userAttributes) {
builder.putAllUserAttributesIfAbsent(filter.filterUserAttributes(appName, userAttributes));
return this;
Expand Down Expand Up @@ -204,6 +205,10 @@ public SpanEventFactory setUri(URI uri) {
final URI sanitizedURI = ExternalsUtil.sanitizeURI(uri);
if (sanitizedURI != null) {
builder.putAgentAttribute("http.url", sanitizedURI.toString());
setServerAddress(sanitizedURI.getHost());
if (sanitizedURI.getPort() > 0) {
setServerPort(sanitizedURI.getPort());
}
}
}
return this;
Expand Down Expand Up @@ -238,42 +243,53 @@ public SpanEventFactory setHttpStatusText(String statusText) {

// datastore parameter
public SpanEventFactory setDatabaseName(String databaseName) {
builder.putIntrinsic("db.instance", databaseName);
builder.putAgentAttribute("db.instance", databaseName);
return this;
}

// datastore parameter
public SpanEventFactory setDatastoreComponent(String component) {
builder.putIntrinsic("component", component);
return this;
}

// datastore parameter
public SpanEventFactory setHostName(String host) {
builder.putIntrinsic("peer.hostname", host);
builder.putAgentAttribute("db.system", component);
return this;
}

// datastore parameter
public SpanEventFactory setAddress(String hostName, String portPathOrId) {
if (portPathOrId != null && hostName != null) {
String address = MessageFormat.format("{0}:{1}", hostName, portPathOrId);
builder.putIntrinsic("peer.address", address);
builder.putAgentAttribute("peer.address", address);
}
return this;
}

public SpanEventFactory setServerAddress(String host) {
builder.putAgentAttribute("server.address", host);
builder.putAgentAttribute("peer.hostname", host);
return this;
}

public SpanEventFactory setServerPort(int port) {
builder.putAgentAttribute("server.port", port);
return this;
}

// datastore parameter
public SpanEventFactory setDatabaseStatement(String query) {
if (query != null) {
builder.putIntrinsic("db.statement", truncateWithEllipsis(query, DB_STATEMENT_TRUNCATE_LENGTH));
builder.putAgentAttribute("db.statement", truncateWithEllipsis(query, DB_STATEMENT_TRUNCATE_LENGTH));
}
return this;
}

// datastore parameter
private SpanEventFactory setDatabaseCollection(String collection) {
builder.putIntrinsic("db.collection", collection);
builder.putAgentAttribute("db.collection", collection);
return this;
}

// datastore parameter
private SpanEventFactory setDatabaseOperation(String operation) {
builder.putAgentAttribute("db.operation", operation);
return this;
}

Expand Down Expand Up @@ -354,8 +370,12 @@ public SpanEventFactory setExternalParameterAttributes(ExternalParameters parame
setDatastoreComponent(datastoreParameters.getProduct());
setDatabaseName(datastoreParameters.getDatabaseName());
setDatabaseCollection(datastoreParameters.getCollection());
setHostName(datastoreParameters.getHost());
setDatabaseOperation(datastoreParameters.getOperation());
setServerAddress(datastoreParameters.getHost());
setKindFromUserAttributes();
if (datastoreParameters.getPort() != null) {
setServerPort(datastoreParameters.getPort());
}
if (datastoreParameters instanceof SlowQueryDatastoreParameters) {
SlowQueryDatastoreParameters<?> queryDatastoreParameters = (SlowQueryDatastoreParameters<?>) datastoreParameters;
setDatabaseStatement(determineObfuscationLevel(queryDatastoreParameters));
Expand Down Expand Up @@ -387,4 +407,3 @@ public SpanEvent build() {
return builder.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ public void uriShouldBeSet() {
SpanEvent target = spanEventFactory.setUri(URI.create("https://newrelic.com")).build();

assertEquals("https://newrelic.com", target.getAgentAttributes().get("http.url"));
assertEquals("newrelic.com", target.getAgentAttributes().get("server.address"));
assertEquals("newrelic.com", target.getAgentAttributes().get("peer.hostname"));
assertNull(target.getAgentAttributes().get("server.port"));
}

@Test
public void addressShouldBeSet() {
SpanEvent target = spanEventFactory.setAddress("localhost", "3306").build();
SpanEvent target = spanEventFactory.setServerAddress("localhost").setServerPort(3306).build();

assertEquals("localhost:3306", target.getIntrinsics().get("peer.address"));
assertEquals("localhost", target.getAgentAttributes().get("server.address"));
assertEquals("localhost", target.getAgentAttributes().get("peer.hostname"));
assertEquals(3306, target.getAgentAttributes().get("server.port"));
}

@Test
Expand All @@ -76,7 +81,7 @@ public void shouldTruncate3KDBStatementTo2K() {
SpanEvent target = spanEventFactory.setDatabaseStatement(threeKStatement).build();

assertEquals(2000,
target.getIntrinsics().get("db.statement").toString().length());
target.getAgentAttributes().get("db.statement").toString().length());
}

@Test
Expand Down Expand Up @@ -179,10 +184,22 @@ public void shouldNotSetNullStatusText() {
public void shouldSetDataStoreParameters() {
DatastoreParameters mockParameters = mock(DatastoreParameters.class);
when(mockParameters.getDatabaseName()).thenReturn("database name");
when(mockParameters.getOperation()).thenReturn("select");
when(mockParameters.getCollection()).thenReturn("users");
when(mockParameters.getProduct()).thenReturn("MySQL");
when(mockParameters.getHost()).thenReturn("dbserver");
when(mockParameters.getPort()).thenReturn(3306);

SpanEvent target = spanEventFactory.setExternalParameterAttributes(mockParameters).build();

assertEquals("database name", target.getIntrinsics().get("db.instance"));
assertEquals("database name", target.getAgentAttributes().get("db.instance"));
assertEquals("select", target.getAgentAttributes().get("db.operation"));
assertEquals("users", target.getAgentAttributes().get("db.collection"));
assertEquals("MySQL", target.getAgentAttributes().get("db.system"));
assertEquals("dbserver", target.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver", target.getAgentAttributes().get("server.address"));
assertEquals(3306, target.getAgentAttributes().get("server.port"));
assertEquals("dbserver:3306", target.getAgentAttributes().get("peer.address"));
}

@Test
Expand Down Expand Up @@ -262,7 +279,3 @@ public boolean shouldIncludeAgentAttribute(String appName, String attributeName)
}
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testMaxSamplesStored() {
.setDecider(true)
.setPriority(1.23f)
.setDurationInSeconds(1.3f)
.setHostName("yourHost")
.setServerAddress("yourHost")
.setTraceId("gnisnacirema")
.setGuid("globallyuniqueidentifier")
.setSampled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,11 @@ public void testSpanEventDatastore() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals("SELECT price, name FROM BOOKS WHERE price <= 79.99", spanEvent.getIntrinsics().get("db.statement"));
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals("SELECT price, name FROM BOOKS WHERE price <= 79.99", spanEvent.getAgentAttributes().get("db.statement"));
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down Expand Up @@ -500,12 +500,12 @@ public void testSpanEventDatastoreTruncation() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("a..."));
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals(2000, spanEvent.getAgentAttributes().get("db.statement").toString().length());
assertTrue(spanEvent.getAgentAttributes().get("db.statement").toString().endsWith("a..."));
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down Expand Up @@ -536,12 +536,13 @@ public void testSpanEventDatastoreTruncationAtExactLimit() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("aaa")); // Should not end with ... since it's exactly at the limit
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("server.address"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals(2000, spanEvent.getAgentAttributes().get("db.statement").toString().length());
assertTrue(spanEvent.getAgentAttributes().get("db.statement").toString().endsWith("aaa")); // Should not end with ... since it's exactly at the limit
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,11 @@ public void testSpanEventDatastore() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals("SELECT price, name FROM BOOKS WHERE price <= 79.99", spanEvent.getIntrinsics().get("db.statement"));
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals("SELECT price, name FROM BOOKS WHERE price <= 79.99", spanEvent.getAgentAttributes().get("db.statement"));
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down Expand Up @@ -673,12 +673,12 @@ public void testSpanEventDatastoreTruncation() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("a..."));
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals(2000, spanEvent.getAgentAttributes().get("db.statement").toString().length());
assertTrue(spanEvent.getAgentAttributes().get("db.statement").toString().endsWith("a..."));
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down Expand Up @@ -709,12 +709,12 @@ public void testSpanEventDatastoreTruncationAtExactLimit() throws SQLException {
assertNotNull(spanEvent);

assertEquals("datastore", spanEvent.getIntrinsics().get("category"));
assertEquals("MySQL", spanEvent.getIntrinsics().get("component"));
assertEquals("dbserver.nerd.us", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getIntrinsics().get("peer.address"));
assertEquals(2000, spanEvent.getIntrinsics().get("db.statement").toString().length());
assertTrue(spanEvent.getIntrinsics().get("db.statement").toString().endsWith("aaa")); // Should not end with ... since it's exactly at the limit
assertEquals("books", spanEvent.getIntrinsics().get("db.collection"));
assertEquals("MySQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("dbserver.nerd.us", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbserver.nerd.us:9945", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals(2000, spanEvent.getAgentAttributes().get("db.statement").toString().length());
assertTrue(spanEvent.getAgentAttributes().get("db.statement").toString().endsWith("aaa")); // Should not end with ... since it's exactly at the limit
assertEquals("books", spanEvent.getAgentAttributes().get("db.collection"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertEquals("Datastore/statement/MySQL/books/select", spanEvent.getName());
assertNotNull(spanEvent.getTraceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ public void testSpanEventDatastore() {
assertNotNull(spanEvent);

assertNull(spanEvent.getParentId());
assertEquals("YourSQL", spanEvent.getIntrinsics().get("component"));
assertEquals("databaseServer", spanEvent.getIntrinsics().get("peer.hostname"));
assertEquals("dbName", spanEvent.getIntrinsics().get("db.instance"));
assertEquals("databaseServer:1234", spanEvent.getIntrinsics().get("peer.address"));
assertEquals("YourSQL", spanEvent.getAgentAttributes().get("db.system"));
assertEquals("databaseServer", spanEvent.getAgentAttributes().get("peer.hostname"));
assertEquals("dbName", spanEvent.getAgentAttributes().get("db.instance"));
assertEquals("databaseServer:1234", spanEvent.getAgentAttributes().get("peer.address"));
assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
assertClm(tracer, spanEvent);
}
Expand Down Expand Up @@ -829,10 +829,10 @@ public void testParent() {
assertEquals(rootSpan.getGuid(), siblingSpan.getParentId());
assertNull(siblingSpan.getIntrinsics().get("nr.entryPoint"));

assertEquals("YourSQL", child2Span.getIntrinsics().get("component"));
assertEquals("databaseServer", child2Span.getIntrinsics().get("peer.hostname"));
assertEquals("dbName", child2Span.getIntrinsics().get("db.instance"));
assertEquals("databaseServer:1234", child2Span.getIntrinsics().get("peer.address"));
assertEquals("YourSQL", child2Span.getAgentAttributes().get("db.system"));
assertEquals("databaseServer", child2Span.getAgentAttributes().get("peer.hostname"));
assertEquals("dbName", child2Span.getAgentAttributes().get("db.instance"));
assertEquals("databaseServer:1234", child2Span.getAgentAttributes().get("peer.address"));
assertEquals("client", child2Span.getIntrinsics().get("span.kind"));

assertEquals("library", siblingSpan.getIntrinsics().get("component"));
Expand Down
Loading

0 comments on commit 9b4c2fe

Please sign in to comment.