diff options
Diffstat (limited to 'contrib/log_correlation/log4j2/src/test/java')
6 files changed, 0 insertions, 692 deletions
diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/AbstractOpenCensusLog4jLogCorrelationTest.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/AbstractOpenCensusLog4jLogCorrelationTest.java deleted file mode 100644 index 93ad85e0..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/AbstractOpenCensusLog4jLogCorrelationTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import io.opencensus.common.Function; -import io.opencensus.common.Scope; -import io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection; -import io.opencensus.trace.SpanContext; -import io.opencensus.trace.Tracer; -import io.opencensus.trace.Tracestate; -import io.opencensus.trace.Tracing; -import java.io.StringWriter; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.Logger; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.StringLayout; -import org.apache.logging.log4j.core.appender.WriterAppender; -import org.apache.logging.log4j.core.layout.PatternLayout; - -/** - * Superclass for all Log4j log correlation test classes. - * - * <p>The tests are split into multiple classes so that each one can be run with a different value - * for the system property {@link OpenCensusTraceContextDataInjector#SPAN_SELECTION_PROPERTY_NAME}. - * The property must be set when Log4j initializes a static variable, and running each test class in - * a separate JVM causes the static variable to be reinitialized. - */ -abstract class AbstractOpenCensusLog4jLogCorrelationTest { - private static final Tracer tracer = Tracing.getTracer(); - - static final String TEST_PATTERN = - "traceId=%X{opencensusTraceId} spanId=%X{opencensusSpanId} " - + "sampled=%X{opencensusTraceSampled} %-5level - %msg"; - - static final Tracestate EMPTY_TRACESTATE = Tracestate.builder().build(); - - private static Logger logger; - - // This method initializes Log4j after setting the SpanSelection, which means that Log4j - // initializes a static variable with a ContextDataInjector that is constructed with the proper - // SpanSelection. This method should be called from a @BeforeClass method in each subclass. - static void initializeLog4j(SpanSelection spanSelection) { - System.setProperty( - OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME, spanSelection.toString()); - logger = (Logger) LogManager.getLogger(AbstractOpenCensusLog4jLogCorrelationTest.class); - } - - // Reconfigures Log4j using the given arguments and runs the function with the given SpanContext - // in scope. - String logWithSpanAndLog4jConfiguration( - String log4jPattern, SpanContext spanContext, Function<Logger, Void> loggingFunction) { - StringWriter output = new StringWriter(); - StringLayout layout = PatternLayout.newBuilder().withPattern(log4jPattern).build(); - Appender appender = - WriterAppender.newBuilder() - .setTarget(output) - .setLayout(layout) - .setName("TestAppender") - .build(); - ((LoggerContext) LogManager.getContext(false)).updateLoggers(); - appender.start(); - logger.addAppender(appender); - logger.setLevel(Level.ALL); - try { - logWithSpan(spanContext, loggingFunction, logger); - return output.toString(); - } finally { - logger.removeAppender(appender); - } - } - - private static void logWithSpan( - SpanContext spanContext, Function<Logger, Void> loggingFunction, Logger logger) { - Scope scope = tracer.withSpan(new TestSpan(spanContext)); - try { - loggingFunction.apply(logger); - } finally { - scope.close(); - } - } -} diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationAllSpansTest.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationAllSpansTest.java deleted file mode 100644 index 355c9b67..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationAllSpansTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import static com.google.common.truth.Truth.assertThat; - -import io.opencensus.common.Function; -import io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection; -import io.opencensus.trace.SpanContext; -import io.opencensus.trace.SpanId; -import io.opencensus.trace.TraceId; -import io.opencensus.trace.TraceOptions; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.Logger; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for Log4j log correlation with {@link - * OpenCensusTraceContextDataInjector#SPAN_SELECTION_PROPERTY_NAME} set to {@link - * SpanSelection#ALL_SPANS}. - */ -@RunWith(JUnit4.class) -public final class OpenCensusLog4jLogCorrelationAllSpansTest - extends AbstractOpenCensusLog4jLogCorrelationTest { - - @BeforeClass - public static void setUp() { - initializeLog4j(SpanSelection.ALL_SPANS); - } - - @Test - public void addSampledSpanToLogEntryWithAllSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("b9718fe3d82d36fce0e6a1ada1c21db0"), - SpanId.fromLowerBase16("75159dde8c503fee"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.warn("message #1"); - return null; - } - }); - assertThat(log) - .isEqualTo( - "traceId=b9718fe3d82d36fce0e6a1ada1c21db0 spanId=75159dde8c503fee " - + "sampled=true WARN - message #1"); - } - - @Test - public void addNonSampledSpanToLogEntryWithAllSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("cd7061dfa9d312cdcc42edab3feab51b"), - SpanId.fromLowerBase16("117d42d4c7acd066"), - TraceOptions.builder().setIsSampled(false).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.info("message #2"); - return null; - } - }); - assertThat(log) - .isEqualTo( - "traceId=cd7061dfa9d312cdcc42edab3feab51b spanId=117d42d4c7acd066 sampled=false INFO " - + "- message #2"); - } - - @Test - public void addBlankSpanToLogEntryWithAllSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.INVALID, - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.fatal("message #3"); - return null; - } - }); - assertThat(log) - .isEqualTo( - "traceId=00000000000000000000000000000000 spanId=0000000000000000 sampled=false FATAL " - + "- message #3"); - } - - @Test - public void preserveOtherKeyValuePairs() { - String log = - logWithSpanAndLog4jConfiguration( - "%X{opencensusTraceId} %X{myTestKey} %-5level - %msg", - SpanContext.create( - TraceId.fromLowerBase16("c95329bb6b7de41afbc51a231c128f97"), - SpanId.fromLowerBase16("bf22ea74d38eddad"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - String key = "myTestKey"; - ThreadContext.put(key, "myTestValue"); - try { - logger.error("message #4"); - } finally { - ThreadContext.remove(key); - } - return null; - } - }); - assertThat(log).isEqualTo("c95329bb6b7de41afbc51a231c128f97 myTestValue ERROR - message #4"); - } - - @Test - public void overwriteExistingTracingKey() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("18e4ae44273a0c44e0c9ea4380792c66"), - SpanId.fromLowerBase16("199a7e16daa000a7"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - ThreadContext.put( - OpenCensusTraceContextDataInjector.TRACE_ID_CONTEXT_KEY, "existingTraceId"); - try { - logger.error("message #5"); - } finally { - ThreadContext.remove(OpenCensusTraceContextDataInjector.TRACE_ID_CONTEXT_KEY); - } - return null; - } - }); - assertThat(log) - .isEqualTo( - "traceId=18e4ae44273a0c44e0c9ea4380792c66 spanId=199a7e16daa000a7 " - + "sampled=true ERROR - message #5"); - } -} diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationNoSpansTest.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationNoSpansTest.java deleted file mode 100644 index 1205924e..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationNoSpansTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import static com.google.common.truth.Truth.assertThat; - -import io.opencensus.common.Function; -import io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection; -import io.opencensus.trace.SpanContext; -import io.opencensus.trace.SpanId; -import io.opencensus.trace.TraceId; -import io.opencensus.trace.TraceOptions; -import org.apache.logging.log4j.core.Logger; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for Log4j log correlation with {@link - * OpenCensusTraceContextDataInjector#SPAN_SELECTION_PROPERTY_NAME} set to {@link - * SpanSelection#NO_SPANS}. - */ -@RunWith(JUnit4.class) -public final class OpenCensusLog4jLogCorrelationNoSpansTest - extends AbstractOpenCensusLog4jLogCorrelationTest { - - @BeforeClass - public static void setUp() { - initializeLog4j(SpanSelection.NO_SPANS); - } - - @Test - public void doNotAddSampledSpanToLogEntryWithNoSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("03d2ada98f6eb8330605a45a88c7e67d"), - SpanId.fromLowerBase16("ce5b1cf09fe58bcb"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.trace("message #1"); - return null; - } - }); - assertThat(log).isEqualTo("traceId= spanId= sampled= TRACE - message #1"); - } - - @Test - public void doNotAddNonSampledSpanToLogEntryWithNoSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("09664283d189791de5218ffe3be88d54"), - SpanId.fromLowerBase16("a7203a50089a4029"), - TraceOptions.builder().setIsSampled(false).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.warn("message #2"); - return null; - } - }); - assertThat(log).isEqualTo("traceId= spanId= sampled= WARN - message #2"); - } -} diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationSampledSpansTest.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationSampledSpansTest.java deleted file mode 100644 index bbce4135..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationSampledSpansTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import static com.google.common.truth.Truth.assertThat; - -import io.opencensus.common.Function; -import io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection; -import io.opencensus.trace.SpanContext; -import io.opencensus.trace.SpanId; -import io.opencensus.trace.TraceId; -import io.opencensus.trace.TraceOptions; -import org.apache.logging.log4j.core.Logger; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests for Log4j log correlation with {@link - * OpenCensusTraceContextDataInjector#SPAN_SELECTION_PROPERTY_NAME} set to {@link - * SpanSelection#SAMPLED_SPANS}. - */ -@RunWith(JUnit4.class) -public final class OpenCensusLog4jLogCorrelationSampledSpansTest - extends AbstractOpenCensusLog4jLogCorrelationTest { - - @BeforeClass - public static void setUp() { - initializeLog4j(SpanSelection.SAMPLED_SPANS); - } - - @Test - public void addSampledSpanToLogEntryWithSampledSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("0af7a7bef890695f1c5e85a8e7290164"), - SpanId.fromLowerBase16("d3f07c467ec2fbb2"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.error("message #1"); - return null; - } - }); - assertThat(log) - .isEqualTo( - "traceId=0af7a7bef890695f1c5e85a8e7290164 spanId=d3f07c467ec2fbb2 sampled=true ERROR " - + "- message #1"); - } - - @Test - public void doNotAddNonSampledSpanToLogEntryWithSampledSpans() { - String log = - logWithSpanAndLog4jConfiguration( - TEST_PATTERN, - SpanContext.create( - TraceId.fromLowerBase16("9e09b559ebb8f7f7ed7451aff68cf441"), - SpanId.fromLowerBase16("0fc9ef54c50a1816"), - TraceOptions.builder().setIsSampled(false).build(), - EMPTY_TRACESTATE), - new Function<Logger, Void>() { - @Override - public Void apply(Logger logger) { - logger.debug("message #2"); - return null; - } - }); - assertThat(log).isEqualTo("traceId= spanId= sampled= DEBUG - message #2"); - } -} diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusTraceContextDataInjectorTest.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusTraceContextDataInjectorTest.java deleted file mode 100644 index 3b704058..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusTraceContextDataInjectorTest.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.collect.Lists; -import io.opencensus.common.Scope; -import io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection; -import io.opencensus.trace.SpanContext; -import io.opencensus.trace.SpanId; -import io.opencensus.trace.TraceId; -import io.opencensus.trace.TraceOptions; -import io.opencensus.trace.Tracer; -import io.opencensus.trace.Tracestate; -import io.opencensus.trace.Tracing; -import java.util.Collections; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.config.Property; -import org.apache.logging.log4j.util.SortedArrayStringMap; -import org.apache.logging.log4j.util.StringMap; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for {@link OpenCensusTraceContextDataInjector}. */ -@RunWith(JUnit4.class) -public final class OpenCensusTraceContextDataInjectorTest { - static final Tracestate EMPTY_TRACESTATE = Tracestate.builder().build(); - - private final Tracer tracer = Tracing.getTracer(); - - @Test - @SuppressWarnings("TruthConstantAsserts") - public void spanSelectionPropertyName() { - assertThat(OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME) - .isEqualTo(OpenCensusTraceContextDataInjector.class.getName() + ".spanSelection"); - } - - @Test - public void traceIdKey() { - assertThat(OpenCensusTraceContextDataInjector.TRACE_ID_CONTEXT_KEY) - .isEqualTo("opencensusTraceId"); - } - - @Test - public void spanIdKey() { - assertThat(OpenCensusTraceContextDataInjector.SPAN_ID_CONTEXT_KEY) - .isEqualTo("opencensusSpanId"); - } - - @Test - public void traceSampledKey() { - assertThat(OpenCensusTraceContextDataInjector.TRACE_SAMPLED_CONTEXT_KEY) - .isEqualTo("opencensusTraceSampled"); - } - - @Test - public void spanSelectionDefaultIsAllSpans() { - assertThat(new OpenCensusTraceContextDataInjector().getSpanSelection()) - .isEqualTo(SpanSelection.ALL_SPANS); - } - - @Test - public void setSpanSelectionWithSystemProperty() { - try { - System.setProperty( - OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME, "NO_SPANS"); - assertThat(new OpenCensusTraceContextDataInjector().getSpanSelection()) - .isEqualTo(SpanSelection.NO_SPANS); - } finally { - System.clearProperty(OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME); - } - } - - @Test - public void useDefaultValueForInvalidSpanSelection() { - try { - System.setProperty( - OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME, - "INVALID_SPAN_SELECTION"); - assertThat(new OpenCensusTraceContextDataInjector().getSpanSelection()) - .isEqualTo(SpanSelection.ALL_SPANS); - } finally { - System.clearProperty(OpenCensusTraceContextDataInjector.SPAN_SELECTION_PROPERTY_NAME); - } - } - - @Test - public void insertConfigurationProperties() { - assertThat( - new OpenCensusTraceContextDataInjector(SpanSelection.ALL_SPANS) - .injectContextData( - Lists.newArrayList( - Property.createProperty("property1", "value1"), - Property.createProperty("property2", "value2")), - new SortedArrayStringMap()) - .toMap()) - .containsExactly( - "property1", - "value1", - "property2", - "value2", - "opencensusTraceId", - "00000000000000000000000000000000", - "opencensusSpanId", - "0000000000000000", - "opencensusTraceSampled", - "false"); - } - - @Test - public void handleEmptyConfigurationProperties() { - assertContainsOnlyDefaultTracingEntries( - new OpenCensusTraceContextDataInjector(SpanSelection.ALL_SPANS) - .injectContextData(Collections.<Property>emptyList(), new SortedArrayStringMap())); - } - - @Test - public void handleNullConfigurationProperties() { - assertContainsOnlyDefaultTracingEntries( - new OpenCensusTraceContextDataInjector(SpanSelection.ALL_SPANS) - .injectContextData(null, new SortedArrayStringMap())); - } - - private static void assertContainsOnlyDefaultTracingEntries(StringMap stringMap) { - assertThat(stringMap.toMap()) - .containsExactly( - "opencensusTraceId", - "00000000000000000000000000000000", - "opencensusSpanId", - "0000000000000000", - "opencensusTraceSampled", - "false"); - } - - @Test - public void rawContextDataWithTracingData() { - OpenCensusTraceContextDataInjector plugin = - new OpenCensusTraceContextDataInjector(SpanSelection.ALL_SPANS); - SpanContext spanContext = - SpanContext.create( - TraceId.fromLowerBase16("e17944156660f55b8cae5ce3f45d4a40"), - SpanId.fromLowerBase16("fc3d2ba0d283b66a"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE); - Scope scope = tracer.withSpan(new TestSpan(spanContext)); - try { - String key = "myTestKey"; - ThreadContext.put(key, "myTestValue"); - try { - assertThat(plugin.rawContextData().toMap()) - .containsExactly( - "myTestKey", - "myTestValue", - "opencensusTraceId", - "e17944156660f55b8cae5ce3f45d4a40", - "opencensusSpanId", - "fc3d2ba0d283b66a", - "opencensusTraceSampled", - "true"); - } finally { - ThreadContext.remove(key); - } - } finally { - scope.close(); - } - } - - @Test - public void rawContextDataWithoutTracingData() { - OpenCensusTraceContextDataInjector plugin = - new OpenCensusTraceContextDataInjector(SpanSelection.NO_SPANS); - SpanContext spanContext = - SpanContext.create( - TraceId.fromLowerBase16("ea236000f6d387fe7c06c5a6d6458b53"), - SpanId.fromLowerBase16("f3b39dbbadb73074"), - TraceOptions.builder().setIsSampled(true).build(), - EMPTY_TRACESTATE); - Scope scope = tracer.withSpan(new TestSpan(spanContext)); - try { - String key = "myTestKey"; - ThreadContext.put(key, "myTestValue"); - try { - assertThat(plugin.rawContextData().toMap()).containsExactly("myTestKey", "myTestValue"); - } finally { - ThreadContext.remove(key); - } - } finally { - scope.close(); - } - } -} diff --git a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/TestSpan.java b/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/TestSpan.java deleted file mode 100644 index 7af46064..00000000 --- a/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/TestSpan.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * 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 io.opencensus.contrib.logcorrelation.log4j2; - -import io.opencensus.trace.Annotation; -import io.opencensus.trace.AttributeValue; -import io.opencensus.trace.EndSpanOptions; -import io.opencensus.trace.Link; -import io.opencensus.trace.Span; -import io.opencensus.trace.SpanContext; -import java.util.EnumSet; -import java.util.Map; - -// Simple test Span that holds a SpanContext. The tests cannot use Span directly, since it is -// abstract. -final class TestSpan extends Span { - TestSpan(SpanContext context) { - super(context, EnumSet.of(Options.RECORD_EVENTS)); - } - - @Override - public void end(EndSpanOptions options) {} - - @Override - public void addLink(Link link) {} - - @Override - public void addAnnotation(Annotation annotation) {} - - @Override - public void addAnnotation(String description, Map<String, AttributeValue> attributes) {} -} |