suneet-s commented on code in PR #14200:
URL: https://github.com/apache/druid/pull/14200#discussion_r1184107416
##########
server/src/main/java/org/apache/druid/server/emitter/ExtraServiceDimensions.java:
##########
@@ -29,12 +29,25 @@
/**
* Annotation to inject extra dimensions, added to all events, emitted via
{@link EmitterModule#getServiceEmitter}.
- *
+ * <p/>
* For example, write this in a body of {@link
com.google.inject.Module#configure} of your extension module):
- *
+ * <p/>
* MapBinder<String, String> extraDims =
* MapBinder.newMapBinder(binder, String.class, String.class,
ExtraServiceDimensions.class);
* extraDims.addBinding("foo").toInstance("bar");
+ * <p/>
+ * If a module wishes to optionally bind service dimensions they may do so by
using the binding to
+ * Map<String, Optional<String>. The key is only added to the service
dimensions that are emitted if the Optional is
+ * present.
+ * <p/>
+ * MapBinder<String, Optional<String>> extraDims =
+ * MapBinder.newMapBinder(
+ * binder,
+ * new TypeLiteral<String>() {},
+ * new TypeLiteral<Optional<String>>() {},
+ * ExtraServiceDimensions.class
+ * );
+ * extraDims.addBinding("foo").toInstance(Optional.fromNullable(bar));
Review Comment:
If you want to optionally bind the key to something that depends on an
injected value, you can not with the `String` -> `String` mapping. This is
because the binding needs to be done in the configure method, while you can
only look at the injected value after the configure method of the module, for
example in a Provider.
MapBinder does not allow binding to null, so you can't use a Provider that
returns null to indicate that you do not want the key to be bound.
https://github.com/google/guice/wiki/NULL_VALUE_IN_MAP#using-null-as-absent-value
Perhaps a better example that explains the usefulness of this is
```
extraDims.addBinding("foo").toProvider(
new Provider<Optional<String>()
{
@Inject
private DruidConfig config;
@Override
public Optional<String> get()
{
return config.isAdditionalServiceDimensionsEnabled() ?
Optional.of(config.getFoo()) : Optional.empty();
}
}
);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]