diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/service/analytics/SpanEventFactory.java b/newrelic-agent/src/main/java/com/newrelic/agent/service/analytics/SpanEventFactory.java index 6f7e381def..7581304fd4 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/service/analytics/SpanEventFactory.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/service/analytics/SpanEventFactory.java @@ -157,6 +157,7 @@ public SpanEventFactory putAllUserAttributes(Map userAttributes) { return this; } + public SpanEventFactory putAllUserAttributesIfAbsent(Map userAttributes) { builder.putAllUserAttributesIfAbsent(filter.filterUserAttributes(appName, userAttributes)); return this; @@ -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; @@ -238,19 +243,13 @@ 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; } @@ -258,22 +257,39 @@ public SpanEventFactory setHostName(String host) { 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; } @@ -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)); @@ -387,4 +407,3 @@ public SpanEvent build() { return builder.build(); } } - diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventFactoryTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventFactoryTest.java index 1dfa2a60f9..0ffaf80cf9 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventFactoryTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventFactoryTest.java @@ -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 @@ -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 @@ -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 @@ -262,7 +279,3 @@ public boolean shouldIncludeAgentAttribute(String appName, String attributeName) } } } - - - - diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventsServiceTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventsServiceTest.java index 7343a78a6e..0ef04bf543 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventsServiceTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/service/analytics/SpanEventsServiceTest.java @@ -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) diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerClmTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerClmTest.java index b15658a0a4..c47458fe77 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerClmTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerClmTest.java @@ -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()); @@ -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()); @@ -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()); diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerTest.java index 45dd239317..4e497e1bb0 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultSqlTracerTest.java @@ -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()); @@ -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()); @@ -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()); diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerClmTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerClmTest.java index 4b53bbac29..5365cc7675 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerClmTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerClmTest.java @@ -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); } @@ -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")); diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerTest.java index e361b46f38..f9ad55a733 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/tracers/DefaultTracerTest.java @@ -717,10 +717,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")); assertClmAbsent(spanEvent); } @@ -860,10 +860,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"));