minor: add getServiceDims() getter to ServiceMetricEvent#19350
minor: add getServiceDims() getter to ServiceMetricEvent#19350abhishekrb19 merged 2 commits intoapache:masterfrom
Conversation
utafrali
left a comment
There was a problem hiding this comment.
The getter is functionally correct and well-tested, but returning ImmutableMap<String, String> is inconsistent with the existing getUserDims() which returns the Map interface — both fields are backed by ImmutableMap, yet the API surface is different. The return type should be Map<String, String> to keep the class API coherent.
| return serviceDims.get(HOST); | ||
| } | ||
|
|
||
| public ImmutableMap<String, String> getServiceDims() |
There was a problem hiding this comment.
The return type ImmutableMap<String, String> leaks the concrete implementation, which is inconsistent with getUserDims() in the same class — that method returns Map<String, Object> even though its backing field is also an ImmutableMap. For a public API, prefer returning the interface type:
public Map<String, String> getServiceDims()
{
return serviceDims;
}Callers who need the immutability guarantee can check instanceof ImmutableMap or the Javadoc can document it. Returning the concrete type here makes it harder to swap the implementation later.
There was a problem hiding this comment.
Yes, that's a good point - thanks for calling that out!
| builderEvent.toMap() | ||
| ); | ||
|
|
||
| Assert.assertEquals("test", builderEvent.getServiceDims().get("service")); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
“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.
Summary
Adds a
getServiceDims()getter method toServiceMetricEventto provide direct access to the immutable service dimensions map. This can be used by custom extensions along with the existing getters in the class.Motivation
Previously, callers could access individual service dimension values via
getService()andgetHost(), but had no way to retrieve the complete service dimensions map. This getter provides direct access to the immutable map of all servicedimensions by custom extensions.
This PR has: