Opentelemetry-java: Extending HashMap may be a problem for AttributesMap

Created on 13 May 2020  路  6Comments  路  Source: open-telemetry/opentelemetry-java

Currently AttributesMap extends HashMap which may cause troubles.

We need to re-evaluate if this is a real problem or we can live with this anti-pattern. Wrapping the hash-map adds an extra unnecessary allocation.

Bug after-ga

Most helpful comment

Only if we put "ACCEPTABLE" in quotes.

All 6 comments

Just my 2c, I'm not opposed to extending HashMap if it's not for a public class, and would personally prefer that over the extra allocation of wrapping the HashMap.

An example I ran into recently while looking at Reactor Context does the same:
https://github.com/reactor/reactor-core/blob/master/reactor-core/src/main/java/reactor/util/context/ContextN.java#L31

I'm not opposed to extending HashMap if it's not for a public class

Same feeling here. I'd be more worried if this indeed was a public class.

If we want to keep this, let's be very sure we don't let it proliferate into public APIs.

maybe a comment? 馃槃

// EXTENDING HASHMAP IS VERY BAD FOR A PUBLIC API
// THIS CLASS IS NOT PUBLIC SO IT IS ACCEPTABLE HERE AS AN OPTIMIZATION
// DO NOT MAKE THIS CLASS PUBLIC WITHOUT CHANGING ITS SUPER TYPE
final class AttributesMap extends HashMap<String, AttributeValue> {

Only if we put "ACCEPTABLE" in quotes.

this has been fixed and the AttributesMap now wraps the map, rather than exposing it. closing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

songy23 picture songy23  路  3Comments

jkwatson picture jkwatson  路  5Comments

poogurgaon picture poogurgaon  路  4Comments

z1c0 picture z1c0  路  3Comments

Oberon00 picture Oberon00  路  3Comments