Skip to content

Commit

Permalink
Merge pull request #27006 from pilhuhn/access_log_mdc_data
Browse files Browse the repository at this point in the history
Provide the ability to log MDC data in access_log
  • Loading branch information
gsmet authored Aug 9, 2022
2 parents 329f197 + 979883d commit 50ed212
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/http-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ include::{generated-dir}/config/quarkus-vertx-http-config-group-access-log-confi
|Request header | | `%{i,request_header_name}`
|Response header | | `%{o,response_header_name}`
|Vert.x Routing Context Internal Data | | `%{d,map_key}`
|Vert.x MDC data (e.g. 'traceId' for OpenTelemetry) | | `%{X,mdc-key}`
|===


Expand Down
5 changes: 5 additions & 0 deletions docs/src/main/asciidoc/opentelemetry.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ quarkus.opentelemetry.tracer.exporter.otlp.endpoint=http://localhost:4317 // <3>
quarkus.opentelemetry.tracer.exporter.otlp.headers=Authorization=Bearer my_secret // <4>
quarkus.log.console.format=%d{HH:mm:ss} %-5p traceId=%X{traceId}, parentId=%X{parentId}, spanId=%X{spanId}, sampled=%X{sampled} [%c{2.}] (%t) %s%e%n // <5>
# Alternative to the console log
quarkus.http.access-log.pattern="...traceId=%{X,traceId} spanId=%{X,spanId}" // <6>
----

<1> All spans created from the application will include an OpenTelemetry `Resource` indicating the span was created by the `myservice` application. If not set, it will default to the artifact id.
<2> Whether OpenTelemetry is enabled or not. The default is `true`, but shown here to indicate how it can be disabled
<3> gRPC endpoint for sending spans
<4> Optional gRPC headers commonly used for authentication
<5> Add tracing information into log message.
<6> You can also only put the trace info into the access log. In this case you must omit the info in the console log format.

== Run the application

The first step is to configure and start the https://opentelemetry.io/docs/collector/[OpenTelemetry Collector] to receive, process and export telemetry data to https://www.jaegertracing.io/[Jaeger] that will display the captured traces.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.vertx.http.runtime.attribute;

import io.quarkus.vertx.core.runtime.VertxMDC;
import io.vertx.ext.web.RoutingContext;

/**
* Provide entries from the MDC section of the RoutingContext.
* This is especially helpful to put OTel data 'traceId' and 'spanId'
* into the access log.
*/
public class VertxMDCDataAttribute implements ExchangeAttribute {

private final String dataKey;

public VertxMDCDataAttribute(String dataKey) {
this.dataKey = dataKey;
}

@Override
public String readAttribute(RoutingContext exchange) {
VertxMDC mdc = VertxMDC.INSTANCE;
return mdc.get(dataKey);
}

@Override
public void writeAttribute(RoutingContext exchange, String newValue) throws ReadOnlyAttributeException {
throw new ReadOnlyAttributeException();
}

public static final class Builder implements ExchangeAttributeBuilder {

@Override
public String name() {
return "OTel data";
}

@Override
public ExchangeAttribute build(final String token) {
if (token.startsWith("%{X,") && token.endsWith("}")) {
final String dataItemName = token.substring(4, token.length() - 1);
return new VertxMDCDataAttribute(dataItemName);
}
return null;
}

@Override
public int priority() {
return 0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
* <li><code>%{r,xxx}</code> xxx is an attribute in the ServletRequest
* <li><code>%{s,xxx}</code> xxx is an attribute in the HttpSession
* <li><code>%{d,xxx}</code> xxx is a data item in the exchange</li>
* <li><code>%{X,xxx}</code> xxx is a key in the Vert.X MDC</li>
* </ul>
*
* @author Stuart Douglas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ io.quarkus.vertx.http.runtime.attribute.RequestPathAttribute$Builder
io.quarkus.vertx.http.runtime.attribute.NullAttribute$Builder
io.quarkus.vertx.http.runtime.attribute.AllRequestHeadersAttribute$Builder
io.quarkus.vertx.http.runtime.attribute.ExchangeDataAttribute$Builder
io.quarkus.vertx.http.runtime.attribute.VertxMDCDataAttribute$Builder

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.vertx.http.runtime.attribute;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

import io.quarkus.vertx.core.runtime.VertxMDC;

class VertxMDCDataAttributeTest {

@Test
void returnsCorrectValue() {
VertxMDC mdc = VertxMDC.INSTANCE;
mdc.put("traceId", "123");

VertxMDCDataAttribute mdcDataAttribute = new VertxMDCDataAttribute("traceId");
assertThat(mdcDataAttribute.readAttribute(null)).isEqualTo("123");
}

@Test
void returnsNullOnKeyNotInMDC() {
VertxMDC mdc = VertxMDC.INSTANCE;
mdc.put("traceId", "123");

VertxMDCDataAttribute mdcDataAttribute = new VertxMDCDataAttribute("spanId");
assertNull(mdcDataAttribute.readAttribute(null));
}
}

0 comments on commit 50ed212

Please sign in to comment.