Skip to content

Commit

Permalink
LifecycleObserver examples: clarify server-side ordering options (#3087)
Browse files Browse the repository at this point in the history
Motivation:

Server-side has 2 options on how to configure an observer.

Modifications:

- Demonstrate both options;
- Add more clarification comments and point to javadoc for more info;
- Make HTTP and gRPC observer examples consistent;

Result:

Observer examples show all possible configuration options and clarify
differences.
  • Loading branch information
idelpivnitskiy authored Nov 5, 2024
1 parent 99371cf commit 681cf76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ public static void main(String... args) throws Exception {
GrpcLifecycleObserver observer =
GrpcLifecycleObservers.logging("servicetalk-examples-grpc-observer-logger", TRACE);
GrpcServers.forPort(8080)
// Option 1: apply an observer for entire server to capture processing by all filters:
// There are a few ways how to configure an observer depending on the desired scope of its visibility.
// 1. Configuring it at the builder gives maximum visibility and captures entire request-response state,
// including all filters and exception mappers.
.lifecycleObserver(observer)
// Option 2: apply an observer using a filter to move it later in a filter chain (before/after tracing
// info is available or retry/timeout/authentication/exception-mapping filters applied), or to apply it
// conditionally:
// 2. Configuring it as a filter allows users to change the ordering of the observer compare to other
// filters or make it conditional. This might be helpful in a few scenarios such as when the tracking
// scope should be limited or when logging should include tracing/MDC context set by other preceding
// filters. See javadoc of GrpcLifecycleObserverServiceFilter for more details.
// .initializeHttp(builder -> builder
// .appendNonOffloadingServiceFilter(tracingFilter)
// .appendNonOffloadingServiceFilter(new GrpcLifecycleObserverServiceFilter(observer)))
// 2.a. At any position compare to other filters before offloading:
// .appendNonOffloadingServiceFilter(new GrpcLifecycleObserverServiceFilter(observer))
// 2.b. At any position compare to other filters after offloading:
// .appendServiceFilter(new GrpcLifecycleObserverServiceFilter(observer)))
.listenAndAwait((GreeterService) (ctx, request) ->
succeeded(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build()))
.awaitShutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@
*/
public final class LifecycleObserverServer {
public static void main(String[] args) throws Exception {
HttpLifecycleObserver observer = HttpLifecycleObservers.logging(
"servicetalk-examples-http-observer-logger", TRACE);
HttpServers.forPort(8080)
.lifecycleObserver(HttpLifecycleObservers.logging("servicetalk-examples-http-observer-logger", TRACE))
// Note: this example demonstrates only blocking-aggregated programming paradigm, for asynchronous and
// streaming API see helloworld examples.
// There are a few ways how to configure an observer depending on the desired scope of its visibility.
// 1. Configuring it at the builder gives maximum visibility and captures entire request-response state,
// including all filters and exception mappers.
.lifecycleObserver(observer)
// 2. Configuring it as a filter allows users to change the ordering of the observer compare to other
// filters or make it conditional. This might be helpful in a few scenarios such as when the tracking
// scope should be limited or when logging should include tracing/MDC context set by other preceding
// filters. See javadoc of HttpLifecycleObserverServiceFilter for more details.
// 2.a. At any position compare to other filters before offloading:
// .appendNonOffloadingServiceFilter(new HttpLifecycleObserverServiceFilter(observer))
// 2.b. At any position compare to other filters after offloading:
// .appendServiceFilter(new HttpLifecycleObserverServiceFilter(observer))
.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok()
.payloadBody("Hello LifecycleObserver!", textSerializerUtf8()))
.awaitShutdown();
Expand Down

0 comments on commit 681cf76

Please sign in to comment.