aboutsummaryrefslogtreecommitdiff
path: root/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java
blob: dd32e448f700dbd46bc6b185a4ab83d541d19591 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * 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.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.SpanSelection;
import io.opencensus.trace.Span;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.unsafe.ContextUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
import org.apache.logging.log4j.util.BiConsumer;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import org.apache.logging.log4j.util.SortedArrayStringMap;
import org.apache.logging.log4j.util.StringMap;
import org.apache.logging.log4j.util.TriConsumer;

// Implementation of the methods inherited from ContextDataInjector.
//
// This class uses "shareable" to mean that a method's return value can be passed to another
// thread.
final class ContextDataUtils {
  private ContextDataUtils() {}

  // The implementation of this method is based on the example in the Javadocs for
  // ContextDataInjector.injectContextData.
  static StringMap injectContextData(
      SpanSelection spanSelection, @Nullable List<Property> properties, StringMap reusable) {
    if (properties == null || properties.isEmpty()) {
      return shareableRawContextData(spanSelection);
    }
    // Context data has precedence over configuration properties.
    putProperties(properties, reusable);
    // TODO(sebright): The following line can be optimized. See
    //     https://github.com/census-instrumentation/opencensus-java/pull/1422/files#r216425494.
    reusable.putAll(nonShareableRawContextData(spanSelection));
    return reusable;
  }

  private static void putProperties(Collection<Property> properties, StringMap stringMap) {
    for (Property property : properties) {
      stringMap.putValue(property.getName(), property.getValue());
    }
  }

  private static StringMap shareableRawContextData(SpanSelection spanSelection) {
    SpanContext spanContext = shouldAddTracingDataToLogEvent(spanSelection);
    return spanContext == null
        ? getShareableContextData()
        : getShareableContextAndTracingData(spanContext);
  }

  static ReadOnlyStringMap nonShareableRawContextData(SpanSelection spanSelection) {
    SpanContext spanContext = shouldAddTracingDataToLogEvent(spanSelection);
    return spanContext == null
        ? getNonShareableContextData()
        : getShareableContextAndTracingData(spanContext);
  }

  // This method returns the current span context iff tracing data should be added to the LogEvent.
  // It avoids getting the current span when the feature is disabled, for efficiency.
  @Nullable
  private static SpanContext shouldAddTracingDataToLogEvent(SpanSelection spanSelection) {
    switch (spanSelection) {
      case NO_SPANS:
        return null;
      case SAMPLED_SPANS:
        SpanContext spanContext = getCurrentSpanContext();
        if (spanContext.getTraceOptions().isSampled()) {
          return spanContext;
        } else {
          return null;
        }
      case ALL_SPANS:
        return getCurrentSpanContext();
    }
    throw new AssertionError("Unknown spanSelection: " + spanSelection);
  }

  private static StringMap getShareableContextData() {
    ReadOnlyThreadContextMap context = ThreadContext.getThreadContextMap();

    // Return a new object, since StringMap is modifiable.
    return context == null
        ? new SortedArrayStringMap(ThreadContext.getImmutableContext())
        : new SortedArrayStringMap(context.getReadOnlyContextData());
  }

  private static ReadOnlyStringMap getNonShareableContextData() {
    ReadOnlyThreadContextMap context = ThreadContext.getThreadContextMap();
    if (context != null) {
      return context.getReadOnlyContextData();
    } else {
      Map<String, String> contextMap = ThreadContext.getImmutableContext();
      return contextMap.isEmpty()
          ? UnmodifiableReadOnlyStringMap.EMPTY
          : new UnmodifiableReadOnlyStringMap(contextMap);
    }
  }

  private static StringMap getShareableContextAndTracingData(SpanContext spanContext) {
    ReadOnlyThreadContextMap context = ThreadContext.getThreadContextMap();
    SortedArrayStringMap stringMap;
    if (context == null) {
      stringMap = new SortedArrayStringMap(ThreadContext.getImmutableContext());
    } else {
      StringMap contextData = context.getReadOnlyContextData();
      stringMap = new SortedArrayStringMap(contextData.size() + 3);
      stringMap.putAll(contextData);
    }
    // TODO(sebright): Move the calls to TraceId.toLowerBase16() and SpanId.toLowerBase16() out of
    // the critical path by wrapping the trace and span IDs in objects that call toLowerBase16() in
    // their toString() methods, after there is a fix for
    // https://github.com/census-instrumentation/opencensus-java/issues/1436.
    stringMap.putValue(
        OpenCensusTraceContextDataInjector.TRACE_ID_CONTEXT_KEY,
        spanContext.getTraceId().toLowerBase16());
    stringMap.putValue(
        OpenCensusTraceContextDataInjector.SPAN_ID_CONTEXT_KEY,
        spanContext.getSpanId().toLowerBase16());
    stringMap.putValue(
        OpenCensusTraceContextDataInjector.TRACE_SAMPLED_CONTEXT_KEY,
        spanContext.getTraceOptions().isSampled() ? "true" : "false");
    return stringMap;
  }

  private static SpanContext getCurrentSpanContext() {
    Span span = ContextUtils.CONTEXT_SPAN_KEY.get();
    return span == null ? SpanContext.INVALID : span.getContext();
  }

  @Immutable
  private static final class UnmodifiableReadOnlyStringMap implements ReadOnlyStringMap {
    private static final long serialVersionUID = 0L;

    static final ReadOnlyStringMap EMPTY =
        new UnmodifiableReadOnlyStringMap(Collections.<String, String>emptyMap());

    private final Map<String, String> map;

    UnmodifiableReadOnlyStringMap(Map<String, String> map) {
      this.map = map;
    }

    @Override
    public boolean containsKey(String key) {
      return map.containsKey(key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <V> void forEach(BiConsumer<String, ? super V> action) {
      for (Entry<String, String> entry : map.entrySet()) {
        action.accept(entry.getKey(), (V) entry.getValue());
      }
    }

    @Override
    @SuppressWarnings("unchecked")
    public <V, S> void forEach(TriConsumer<String, ? super V, S> action, S state) {
      for (Entry<String, String> entry : map.entrySet()) {
        action.accept(entry.getKey(), (V) entry.getValue(), state);
      }
    }

    @Override
    @Nullable
    @SuppressWarnings({
      "unchecked",
      "TypeParameterUnusedInFormals" // This is an overridden method.
    })
    public <V> V getValue(String key) {
      return (V) map.get(key);
    }

    @Override
    public boolean isEmpty() {
      return map.isEmpty();
    }

    @Override
    public int size() {
      return map.size();
    }

    @Override
    public Map<String, String> toMap() {
      return map;
    }
  }
}