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 @@ -93,6 +93,11 @@ public String getHost()
return serviceDims.get(HOST);
}

public Map<String, String> getServiceDims()
{
return serviceDims;
}

public Map<String, Object> getUserDims()
{
return userDims;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void testBuilder()
builderEvent.toMap()
);

Assert.assertEquals("test", builderEvent.getServiceDims().get("service"));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string literals "service" and "host" duplicate the key constants already defined in the class (used inside getService() and getHost()). If those constants (SERVICE, HOST) are accessible from the test, use them directly to avoid silent drift if the keys ever change:

Assert.assertEquals("test", builderEvent.getServiceDims().get(ServiceMetricEvent.SERVICE));
Assert.assertEquals("localhost", builderEvent.getServiceDims().get(ServiceMetricEvent.HOST));

If the constants are private, at minimum add a comment explaining why literals are used here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“service” and “host” are documented metric dimensions. Hence, they cannot change due to backward compatibility guarantees, and this test and other existing ones that assert on the literals would catch that unlikely drift.

Assert.assertEquals("localhost", builderEvent.getServiceDims().get("host"));

ServiceMetricEvent constructorEvent = ServiceMetricEvent
.builder()
.setDimension("user1", "a")
Expand Down Expand Up @@ -333,6 +336,8 @@ public void test_builder_createsImmutableEvents()
final ServiceMetricEvent event1 = eventBuilder.build("coordinator", "localhost");

Assert.assertEquals(Map.of("dim1", "v1"), event1.getUserDims());
Assert.assertEquals("coordinator", event1.getServiceDims().get("service"));
Assert.assertEquals("localhost", event1.getServiceDims().get("host"));

final ServiceMetricEvent event2 = eventBuilder
.setDimension("dim2", "v2")
Expand All @@ -342,6 +347,8 @@ public void test_builder_createsImmutableEvents()
// Verify that the original event gets changed dimensions
Assert.assertEquals(Map.of("dim1", "v1", "dim2", "v2"), event2.getUserDims());
Assert.assertEquals(Map.of("dim1", "v1"), event1.getUserDims());
Assert.assertEquals("coordinator", event2.getServiceDims().get("service"));
Assert.assertEquals("localhost", event2.getServiceDims().get("host"));
}

@Test
Expand Down Expand Up @@ -380,4 +387,15 @@ public void testSerializedServiceMetricEventIsOrdered() throws JsonProcessingExc
new DefaultObjectMapper().writeValueAsString(event.toMap())
);
}

@Test
public void testGetServiceDims()
{
final ServiceMetricEvent event = ServiceMetricEvent.builder()
.setMetric("test-metric", 100)
.setDimension("userDim", "value")
.build(ImmutableMap.of("serviceDim1", "dim1", "serviceDim2", "dim2"));

Assert.assertEquals(ImmutableMap.of("serviceDim1", "dim1", "serviceDim2", "dim2"), event.getServiceDims());
}
}
Loading