-
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.
TagFactory implementation that pulls values from the slf4j MDC data.
- Loading branch information
1 parent
f301832
commit d1488ec
Showing
4 changed files
with
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
dependencies { | ||
compileApi project(':spectator-api') | ||
|
||
// The MdcTagFactoryTest requires a logging implementation that supports the MDC | ||
// functionality, which the slf4j-nop (default) and slf4j-simple implementations do not. | ||
testCompile "org.slf4j:slf4j-log4j12" | ||
testCompile "log4j:log4j" | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...ator-ext-placeholders/src/main/java/com/netflix/spectator/placeholders/MdcTagFactory.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,55 @@ | ||
/** | ||
* Copyright 2016 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.placeholders; | ||
|
||
import com.netflix.spectator.api.BasicTag; | ||
import com.netflix.spectator.api.Tag; | ||
import com.netflix.spectator.impl.Preconditions; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* A TagFactory implementation that extracts information from the Sl4fj MDC data. | ||
* If the MDC for the current thread has no value associated with the specified | ||
* name at the time that the createTag method is invoked, then that method will | ||
* return null, which will result in the tag being omitted. | ||
*/ | ||
public class MdcTagFactory implements TagFactory { | ||
|
||
private final String name; | ||
|
||
/** | ||
* Construct a new instance that will return a Tag with the MDC value associated | ||
* with the specified name at the time that the createTag method is invoked. | ||
* | ||
* @param name | ||
* the non-null name of the MDC value to use for the tag | ||
*/ | ||
public MdcTagFactory(String name) { | ||
this.name = Preconditions.checkNotNull(name, "name"); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Tag createTag() { | ||
String value = MDC.get(name); | ||
|
||
return value != null ? new BasicTag(name, value) : null; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...-ext-placeholders/src/test/java/com/netflix/spectator/placeholders/MdcTagFactoryTest.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,68 @@ | ||
/** | ||
* Copyright 2016 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.placeholders; | ||
|
||
import com.netflix.spectator.api.BasicTag; | ||
import com.netflix.spectator.api.Tag; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* Unit tests for the MdcTagFactory class. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class MdcTagFactoryTest { | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testNullName() { | ||
new MdcTagFactory(null); | ||
} | ||
|
||
@Test | ||
public void testNameFromKey() throws Exception { | ||
String expected = "factoryName"; | ||
TagFactory factory = new MdcTagFactory(expected); | ||
|
||
Assert.assertEquals(expected, factory.name()); | ||
} | ||
|
||
@Test | ||
public void testNoValueInMdc() { | ||
TagFactory factory = new MdcTagFactory("unused"); | ||
|
||
Assert.assertNull(factory.createTag()); | ||
} | ||
|
||
@Test | ||
public void testValueInMdc() { | ||
String mdcName = "key"; | ||
String expectedValue = "value"; | ||
Tag expectedTag = new BasicTag(mdcName, expectedValue); | ||
TagFactory factory = new MdcTagFactory("key"); | ||
|
||
try (MDC.MDCCloseable closeable = MDC.putCloseable(mdcName, expectedValue)) { | ||
Tag actualTag = factory.createTag(); | ||
|
||
Assert.assertEquals(expectedTag, actualTag); | ||
} | ||
|
||
// Make sure that the factory returns null after the MDC has been cleaned up. | ||
Assert.assertNull(factory.createTag()); | ||
} | ||
} |