|
| 1 | +# Undertow Server Standalone Instrumentation |
| 2 | + |
| 3 | +__Note:__ This module is disabled by default in order to not conflict with any Wildfly instrumentation module. |
| 4 | +If you wish to enable the undertow-server module, add the following to your agent configuration file: |
| 5 | +```yaml |
| 6 | + class_transformer: |
| 7 | + com.newrelic.instrumentation.undertow-server-1.1.0: |
| 8 | + enabled: true |
| 9 | +``` |
| 10 | +
|
| 11 | +This module is intended for applications using Undertow in stand alone mode, using any of the following |
| 12 | +route handlers: |
| 13 | +- io.undertow.server.RoutingHandler |
| 14 | +- io.undertow.server.handlers.PathTemplateHandler |
| 15 | +- io.undertow.predicate.PathTemplatePredicate |
| 16 | +
|
| 17 | +If any other handler is used, the transaction will be named `{connectors_placeholder_name}/`. This is |
| 18 | +to prevent an explosion of unique transaction names when parameterized request paths are used. |
| 19 | + |
| 20 | +This instrumentation module is not compatible with any Wildfly instrumentation modules. |
| 21 | + |
| 22 | +Example code of supported handlers are below. |
| 23 | + |
| 24 | +## Example Code |
| 25 | + |
| 26 | +### RoutingHandler |
| 27 | + |
| 28 | +``` |
| 29 | + RoutingHandler routingHandler = new RoutingHandler(); |
| 30 | + |
| 31 | + routingHandler.get("/greet/{name}", exchange -> { |
| 32 | + String name = exchange.getQueryParameters().get("name").getFirst(); |
| 33 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 34 | + exchange.getResponseSender().send("Hello, " + name + "!"); |
| 35 | + }).get("/static/content", exchange -> { |
| 36 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 37 | + exchange.getResponseSender().send("Static content"); |
| 38 | + }); |
| 39 | +``` |
| 40 | +
|
| 41 | +### PathTemplateHandler |
| 42 | +``` |
| 43 | + PathTemplateHandler pathTemplateHandler = new PathTemplateHandler(); |
| 44 | + |
| 45 | + pathTemplateHandler.add("/greet/{name}/{count}", exchange -> { |
| 46 | + PathTemplateMatch pathMatch = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY); |
| 47 | + String name = pathMatch.getParameters().get("name"); |
| 48 | + int count = Integer.parseInt(pathMatch.getParameters().get("count")); |
| 49 | + |
| 50 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 51 | + exchange.getResponseSender().send("Hello " + name + String.valueOf("!").repeat(count)); |
| 52 | + }); |
| 53 | +``` |
| 54 | +
|
| 55 | +### PathTemplatePredicate (wrapped in a PredicateHandler Instance) |
| 56 | +``` |
| 57 | + HttpHandler matchedPathHandler = exchange -> { |
| 58 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 59 | + exchange.getResponseSender().send("Path matched!"); |
| 60 | + }; |
| 61 | + HttpHandler unmatchedPathHandler = exchange -> { |
| 62 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 63 | + exchange.getResponseSender().send("Path not matched."); |
| 64 | + }; |
| 65 | + PredicateHandler predicateHandler = new PredicateHandler( |
| 66 | + new PathTemplatePredicate("/greet4/{name}", ExchangeAttributes.relativePath()), |
| 67 | + matchedPathHandler, |
| 68 | + unmatchedPathHandler |
| 69 | + ); |
| 70 | +``` |
| 71 | +
|
| 72 | +### Full Example using RoutingHandler |
| 73 | +```java |
| 74 | +public class UndertowHttpServer { |
| 75 | +
|
| 76 | + public static void main(String[] args) throws IOException { |
| 77 | + RoutingHandler routingHandler = new RoutingHandler(); |
| 78 | + routingHandler.get("/greet/{name}", exchange -> { |
| 79 | + String name = exchange.getQueryParameters().get("name").getFirst(); |
| 80 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 81 | + exchange.setStatusCode(200); |
| 82 | + exchange.getResponseSender().send("Hello, " + name + "!"); |
| 83 | + }).get("/static", exchange -> { |
| 84 | + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); |
| 85 | + exchange.setStatusCode(200); |
| 86 | + exchange.getResponseSender().send("Static static static"); |
| 87 | + }); |
| 88 | +
|
| 89 | + System.out.println("Building Undertow server"); |
| 90 | + Undertow.Builder builder = Undertow.builder(); |
| 91 | + Undertow undertow = builder |
| 92 | + .addHttpListener(8080, "localhost") |
| 93 | + .setHandler(routingHandler) |
| 94 | + .build(); |
| 95 | +
|
| 96 | + System.out.println("Undertow started"); |
| 97 | + undertow.start(); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments