-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from brharrington/0.14.x-guice-module
0.14.x guice module
- Loading branch information
Showing
6 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/SpectatorModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/TestModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/SpectatorModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/TestModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |