Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | - | - | ||||
src/ | 03-May-2024 | - | 1,089 | 726 | ||
README.md | D | 03-May-2024 | 3.6 KiB | 89 | 64 | |
build.gradle | D | 03-May-2024 | 635 | 27 | 19 |
README.md
1# OpenCensus Log4j 2 Log Correlation 2 3This subproject is currently experimental, so it may be redesigned or removed in the future. It 4will remain experimental until we have a specification for a log correlation feature in 5[opencensus-specs](https://github.com/census-instrumentation/opencensus-specs/) 6(issue [#123](https://github.com/census-instrumentation/opencensus-specs/issues/123)). 7 8The `opencensus-contrib-log-correlation-log4j2` artifact provides a 9[Log4j 2](https://logging.apache.org/log4j/2.x/) 10[`ContextDataInjector`](https://logging.apache.org/log4j/2.x/manual/extending.html#Custom_ContextDataInjector) 11that automatically adds tracing data to the context of Log4j 12[`LogEvent`](https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/LogEvent.html)s. 13The class name is 14`OpenCensusTraceContextDataInjector`. `OpenCensusTraceContextDataInjector` adds the current trace 15ID, span ID, and sampling decision to each `LogEvent`, so that they can be accessed with 16[`LogEvent.getContextData()`](https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/LogEvent.html#getContextData()) 17or included in a layout. 18 19See 20https://github.com/census-ecosystem/opencensus-experiments/tree/master/java/log_correlation/log4j2 21for a demo that uses this library to correlate logs and traces in Stackdriver. 22 23## Instructions 24 25### Add the dependencies to your project 26 27For Maven add to your `pom.xml`: 28```xml 29<dependencies> 30 <dependency> 31 <groupId>io.opencensus</groupId> 32 <artifactId>opencensus-contrib-log-correlation-log4j2</artifactId> 33 <version>0.16.1</version> 34 <scope>runtime</scope> 35 </dependency> 36</dependencies> 37``` 38 39For Gradle add to your dependencies: 40```groovy 41runtime 'io.opencensus:opencensus-contrib-log-correlation-log4j2:0.16.1' 42``` 43 44### Configure the `OpenCensusTraceContextDataInjector` 45 46#### Specify the `ContextDataInjector` override 47 48Override Log4j's default `ContextDataInjector` by setting the system property 49`log4j2.contextDataInjector` to the full name of the class, 50`io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector`. 51 52#### Choose when to add tracing data to log events 53 54The following system property controls the decision to add tracing data from the current span to a 55log event: 56 57`io.opencensus.contrib.logcorrelation.log4j2.OpenCensusTraceContextDataInjector.spanSelection` 58 59The allowed values are: 60 61* `ALL_SPANS`: adds tracing data to all log events (default) 62 63* `NO_SPANS`: disables the log correlation feature 64 65* `SAMPLED_SPANS`: adds tracing data to log events when the current span is sampled 66 67### Add the tracing data to log entries 68 69`opencensus-contrib-log-correlation-log4j2` adds the following key-value pairs to the `LogEvent` 70context: 71 72* `opencensusTraceId` - the lowercase base16 encoding of the current trace ID 73* `opencensusSpanId` - the lowercase base16 encoding of the current span ID 74* `opencensusTraceSampled` - the sampling decision of the current span ("true" or "false") 75 76These values can be accessed from layouts with 77[Context Map Lookup](http://logging.apache.org/log4j/2.x/manual/lookups.html#ContextMapLookup). For 78example, the trace ID can be accessed with `$${ctx:opencensusTraceId}`. The values can also be 79accessed with the `X` conversion character in 80[`PatternLayout`](http://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout), for 81example, `%X{opencensusTraceId}`. 82 83See an example Log4j configuration file in the demo: 84https://github.com/census-ecosystem/opencensus-experiments/tree/master/java/log_correlation/log4j2/src/main/resources/log4j2.xml 85 86### Java Versions 87 88Java 6 or above is required for using this artifact. 89