Skip to content

Commit

Permalink
add MdcTagFactory (#328)
Browse files Browse the repository at this point in the history
TagFactory implementation that pulls values from the
slf4j MDC data.
  • Loading branch information
pstout authored and brharrington committed Oct 12, 2016
1 parent f301832 commit d1488ec
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
5 changes: 5 additions & 0 deletions spectator-ext-placeholders/build.gradle
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ abstract class AbstractDefaultPlaceholderMeter<T extends Meter> implements Meter

/**
* Creates a new dynamic meter.
* @param id the dynamic id for the meter
*
* @param id the dynamic id for the meter
* @param meterResolver the function to map a resolved id to concrete metric
*/
AbstractDefaultPlaceholderMeter(PlaceholderId id, Function<Id, T> meterResolver) {
Expand Down
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;
}
}
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());
}
}

0 comments on commit d1488ec

Please sign in to comment.