-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27006 from pilhuhn/access_log_mdc_data
Provide the ability to log MDC data in access_log
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
.../runtime/src/main/java/io/quarkus/vertx/http/runtime/attribute/VertxMDCDataAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...time/src/test/java/io/quarkus/vertx/http/runtime/attribute/VertxMDCDataAttributeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |