aboutsummaryrefslogtreecommitdiff
path: root/contrib/log_correlation/log4j2/src/test/java/io/opencensus/contrib/logcorrelation/log4j2/OpenCensusLog4jLogCorrelationAllSpansTest.java
blob: 355c9b6745ac9922cae5f458d6b7eb394e548883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * 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");
  }
}