Skip to content

Commit

Permalink
Merge pull request #66 from brharrington/0.14.x-guice-module
Browse files Browse the repository at this point in the history
0.14.x guice module
  • Loading branch information
brharrington committed Nov 15, 2014
2 parents 771ebae + cf22e51 commit 8e713c8
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ project(':spectator-nflx-plugin') {
compile project(':spectator-ext-gc')
compile project(':spectator-ext-jvm')
compile project(':spectator-reg-servo')
compile 'com.google.inject:guice:3.0'
compile 'com.netflix.archaius:archaius-core:0.6.3'
compile 'com.netflix.governator:governator:1.2.20'
compile 'com.netflix.ribbon:ribbon-transport:2.0-RC9'
testCompile 'com.netflix.governator:governator:1.2.20'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ public GcLogger() {
* log buffer is updated.
*/
public synchronized void start(GcEventListener listener) {
Preconditions.checkState(notifListener == null, "logger already started");
// TODO: this class has a bad mix of static fields used from an instance of the class. For now
// this has been changed not to throw to make the dependency injection use-cases work. A
// more general refactor of the GcLogger class is needed.
if (notifListener != null) {
LOGGER.warn("logger already started");
return;
}
eventListener = listener;
notifListener = new GcNotificationListener();
for (GarbageCollectorMXBean mbean : ManagementFactory.getGarbageCollectorMXBeans()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed 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 com.netflix.spectator.nflx;

import com.google.inject.AbstractModule;
import com.netflix.spectator.api.ExtendedRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Spectator;

/**
* Guice module to configure the appropriate bindings for running an application. Note that this
* will configure it to use the global registry bindings for reporting. This also means that test
* cases will not be completely isolated. For unit tests see
* {@link com.netflix.spectator.nflx.TestModule}. Typical usage:
*
* <p><b>User code</b></p>
* <pre>
* public class Foo {
* private final Counter counter;
*
* {@literal @}Inject
* public Foo(ExtendedRegistry registry) {
* counter = registry.counter("foo.doSomething");
* }
*
* public void doSomething() {
* counter.increment();
* }
* }
* </pre>
*
* <p><b>Governator</b></p>
* <p>One of the classes requires an {@link javax.annotation.PostConstruct} block to initialize
* so governator should be used to manage the lifecycle as guice doesn't support it directly.</p>
* <pre>
* Injector injector = LifecycleInjector.builder()
* .withModules(new SpectatorModule())
* .build()
* .createInjector();
* injector.getInstance(LifecycleManager.class).start();
* </pre>
*/
public class SpectatorModule extends AbstractModule {
@Override protected void configure() {
bind(Plugin.class).asEagerSingleton();
bind(ExtendedRegistry.class).toInstance(Spectator.registry());
bind(Registry.class).toInstance(Spectator.registry());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed 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 com.netflix.spectator.nflx;

import com.google.inject.AbstractModule;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.ExtendedRegistry;
import com.netflix.spectator.api.Registry;

/**
* Guice module to configure the appropriate bindings for unit tests. Note that this module will
* create a registry that only keeps data in-memory and is scoped to the injector. If used when
* running the application you will not be able to see the data and it will not get reported off
* the instance. In particular, it is completely independent of the main registry accessed by
* calling {@link com.netflix.spectator.api.Spectator#registry()}. Use the
* {@link com.netflix.spectator.nflx.SpectatorModule} when running code outside of unit tests.
*/
public class TestModule extends AbstractModule {
@Override protected void configure() {
final ExtendedRegistry registry = new ExtendedRegistry(new DefaultRegistry());
bind(ExtendedRegistry.class).toInstance(registry);
bind(Registry.class).toInstance(registry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed 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 com.netflix.spectator.nflx;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.netflix.governator.guice.LifecycleInjector;
import com.netflix.governator.lifecycle.LifecycleManager;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.ExtendedRegistry;
import com.netflix.spectator.api.Spectator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SpectatorModuleTest {

public static class Foo {
private final Counter counter;

@Inject
public Foo(ExtendedRegistry registry) {
counter = registry.counter("foo.doSomething");
}

public void doSomething() {
counter.increment();
}
}

private Injector injector;

@Before
public void setup() throws Exception {
injector = LifecycleInjector.builder()
.withModules(new SpectatorModule())
.build()
.createInjector();
injector.getInstance(LifecycleManager.class).start();
}

@After
public void cleanup() {
injector.getInstance(LifecycleManager.class).close();
}

@Test
public void checkCount() {
ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class);
Counter c = r.counter("foo.doSomething");
long before = c.count();

Foo f = injector.getInstance(Foo.class);
f.doSomething();
Assert.assertEquals(before + 1, c.count());
}

@Test
public void isGlobal() {
ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class);
Assert.assertSame(Spectator.registry(), r);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed 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 com.netflix.spectator.nflx;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.ExtendedRegistry;
import com.netflix.spectator.api.Spectator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TestModuleTest {

public static class Foo {
private final Counter counter;

@Inject
public Foo(ExtendedRegistry registry) {
counter = registry.counter("foo.doSomething");
}

public void doSomething() {
counter.increment();
}
}

private Injector injector;

@Before
public void setup() {
injector = Guice.createInjector(new TestModule());
}

@Test
public void checkCount() {
ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class);
Counter c = r.counter("foo.doSomething");
Assert.assertEquals(0, c.count());

Foo f = injector.getInstance(Foo.class);
f.doSomething();
Assert.assertEquals(1, c.count());
}

@Test
public void notGlobal() {
ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class);
Assert.assertNotSame(Spectator.registry(), r);
}
}

0 comments on commit 8e713c8

Please sign in to comment.