Add ability to inject extra dimensions for service emitter (#4982)

* Add ability to inject extra dimensions for service emitter

* Docs
This commit is contained in:
Roman Leventov 2017-10-26 13:27:01 -05:00 committed by Nishant Bangarwa
parent ee66db900e
commit 125a912067
2 changed files with 65 additions and 7 deletions

View File

@ -31,6 +31,7 @@ import com.google.inject.Module;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import com.google.inject.name.Names; import com.google.inject.name.Names;
import com.metamx.emitter.EmittingLogger; import com.metamx.emitter.EmittingLogger;
@ -45,6 +46,7 @@ import io.druid.server.DruidNode;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
/** /**
@ -76,24 +78,35 @@ public class EmitterModule implements Module
binder.install(new ComposingEmitterModule()); binder.install(new ComposingEmitterModule());
binder.bind(Emitter.class).toProvider(new EmitterProvider(emitterType)).in(LazySingleton.class); binder.bind(Emitter.class).toProvider(new EmitterProvider(emitterType)).in(LazySingleton.class);
MapBinder<String, String> extraServiceDimensions = MapBinder.newMapBinder(
binder,
String.class,
String.class,
ExtraServiceDimensions.class
);
String version = getClass().getPackage().getImplementationVersion();
extraServiceDimensions
.addBinding("version")
.toInstance(Strings.nullToEmpty(version)); // Version is null during `mvn test`.
} }
@Provides @Provides
@ManageLifecycle @ManageLifecycle
public ServiceEmitter getServiceEmitter(@Self Supplier<DruidNode> configSupplier, Emitter emitter) public ServiceEmitter getServiceEmitter(
@Self Supplier<DruidNode> configSupplier,
Emitter emitter,
@ExtraServiceDimensions Map<String, String> extraServiceDimensions
)
{ {
final DruidNode config = configSupplier.get(); final DruidNode config = configSupplier.get();
String version = getClass().getPackage().getImplementationVersion();
final ImmutableMap<String, String> otherServiceDimensions = ImmutableMap.of(
"version",
Strings.nullToEmpty(version) // Version is null during `mvn test`.
);
log.info("Underlying emitter for ServiceEmitter: %s", emitter); log.info("Underlying emitter for ServiceEmitter: %s", emitter);
log.info("Extra service dimensions: %s", extraServiceDimensions);
final ServiceEmitter retVal = new ServiceEmitter( final ServiceEmitter retVal = new ServiceEmitter(
config.getServiceName(), config.getServiceName(),
config.getHostAndPortToUse(), config.getHostAndPortToUse(),
emitter, emitter,
otherServiceDimensions ImmutableMap.copyOf(extraServiceDimensions)
); );
EmittingLogger.registerEmitter(retVal); EmittingLogger.registerEmitter(retVal);
return retVal; return retVal;

View File

@ -0,0 +1,45 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.server.emitter;
import com.google.inject.BindingAnnotation;
import io.druid.guice.annotations.PublicApi;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to inject extra dimensions, added to all events, emitted via {@link EmitterModule#getServiceEmitter}.
*
* For example, write this in a body of {@link com.google.inject.Module#configure} of your extension module):
*
* MapBinder<String, String> extraDims =
* MapBinder.newMapBinder(binder, String.class, String.class, ExtraServiceDimensions.class);
* extraDims.addBinding("foo").toInstance("bar");
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
@PublicApi
public @interface ExtraServiceDimensions
{
}