-
Notifications
You must be signed in to change notification settings - Fork 1k
Introduce BraveRpcService
#6115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
037585c
minimal impl
jrhee17 7f89976
fix test
jrhee17 45470ae
lint
jrhee17 42fa304
address comment by @ikhoon
jrhee17 8fc35e7
Merge branch 'main' into feat/brave-rpc
jrhee17 6d2fb8d
address comment by @ikhoon
jrhee17 4fd2f99
Merge branch 'main' into feat/brave-rpc
jrhee17 df596b5
Update brave/brave6/src/main/java/com/linecorp/armeria/server/brave/A…
jrhee17 ca8b036
Update brave/brave6/src/main/java/com/linecorp/armeria/server/brave/A…
jrhee17 af242c3
address comments by @minwoox and @trustin
jrhee17 15f5e09
address comment by @minwoox
jrhee17 23843d7
address comment by @trustin
jrhee17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
brave/brave6/src/main/java/com/linecorp/armeria/server/brave/AbstractBraveService.java
This file contains hidden or 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,93 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.server.brave; | ||
|
|
||
| import static com.linecorp.armeria.server.brave.ArmeriaServerParser.annotateWireSpan; | ||
| import static com.linecorp.armeria.server.brave.BraveService.SERVICE_REQUEST_DECORATING_SCOPE; | ||
|
|
||
| import com.linecorp.armeria.common.Request; | ||
| import com.linecorp.armeria.common.Response; | ||
| import com.linecorp.armeria.common.brave.RequestContextCurrentTraceContext; | ||
| import com.linecorp.armeria.common.logging.RequestLog; | ||
| import com.linecorp.armeria.internal.common.RequestContextExtension; | ||
| import com.linecorp.armeria.server.Service; | ||
| import com.linecorp.armeria.server.ServiceRequestContext; | ||
| import com.linecorp.armeria.server.SimpleDecoratingService; | ||
| import com.linecorp.armeria.server.TransientServiceOption; | ||
|
|
||
| import brave.Span; | ||
| import brave.Tracer; | ||
| import brave.Tracer.SpanInScope; | ||
|
|
||
| abstract class AbstractBraveService<BI extends brave.Request, BO extends brave.Response, | ||
| I extends Request, O extends Response> extends SimpleDecoratingService<I, O> { | ||
|
|
||
| private final Tracer tracer; | ||
| private final RequestContextCurrentTraceContext currentTraceContext; | ||
|
|
||
| /** | ||
| * Creates a new instance that decorates the specified {@link Service}. | ||
| */ | ||
| protected AbstractBraveService(Service<I, O> delegate, Tracer tracer, | ||
| RequestContextCurrentTraceContext currentTraceContext) { | ||
| super(delegate); | ||
| this.tracer = tracer; | ||
| this.currentTraceContext = currentTraceContext; | ||
| } | ||
|
|
||
| @Override | ||
| public final O serve(ServiceRequestContext ctx, I req) throws Exception { | ||
| if (!ctx.config().transientServiceOptions().contains(TransientServiceOption.WITH_TRACING)) { | ||
| return unwrap().serve(ctx, req); | ||
| } | ||
| final BI braveReq = braveRequest(ctx); | ||
| final Span span = handleReceive(braveReq); | ||
|
|
||
| final RequestContextExtension ctxExtension = ctx.as(RequestContextExtension.class); | ||
| if (currentTraceContext.scopeDecoratorAdded() && !span.isNoop() && ctxExtension != null) { | ||
| // Run the scope decorators when the ctx is pushed to the thread local. | ||
| ctxExtension.hook(() -> currentTraceContext.decorateScope(span.context(), | ||
| SERVICE_REQUEST_DECORATING_SCOPE)); | ||
| } | ||
|
|
||
| maybeAddTagsToSpan(ctx, braveReq, span); | ||
| try (SpanInScope ignored = tracer.withSpanInScope(span)) { | ||
| return unwrap().serve(ctx, req); | ||
| } | ||
| } | ||
|
|
||
| abstract BI braveRequest(ServiceRequestContext ctx); | ||
|
|
||
| abstract BO braveResponse(ServiceRequestContext ctx, RequestLog log, BI braveReq); | ||
|
|
||
| abstract Span handleReceive(BI braveReq); | ||
|
|
||
| abstract void handleSend(BO response, Span span); | ||
|
|
||
| void maybeAddTagsToSpan(ServiceRequestContext ctx, BI braveReq, Span span) { | ||
| if (span.isNoop()) { | ||
| // For no-op spans, nothing special to do. | ||
| return; | ||
| } | ||
|
|
||
| ctx.log().whenComplete().thenAccept(log -> { | ||
| annotateWireSpan(log, span); | ||
| final BO braveRes = braveResponse(ctx, log, braveReq); | ||
| handleSend(braveRes, span); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
105 changes: 105 additions & 0 deletions
105
brave/brave6/src/main/java/com/linecorp/armeria/server/brave/BraveRpcService.java
This file contains hidden or 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,105 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.server.brave; | ||
|
|
||
| import static com.linecorp.armeria.internal.common.brave.TraceContextUtil.ensureScopeUsesRequestContext; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| import com.linecorp.armeria.common.RpcRequest; | ||
| import com.linecorp.armeria.common.RpcResponse; | ||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
| import com.linecorp.armeria.common.brave.RequestContextCurrentTraceContext; | ||
| import com.linecorp.armeria.common.logging.RequestLog; | ||
| import com.linecorp.armeria.server.RpcService; | ||
| import com.linecorp.armeria.server.ServiceRequestContext; | ||
|
|
||
| import brave.Span; | ||
| import brave.Tracing; | ||
| import brave.rpc.RpcRequestParser; | ||
| import brave.rpc.RpcResponseParser; | ||
| import brave.rpc.RpcServerHandler; | ||
| import brave.rpc.RpcServerRequest; | ||
| import brave.rpc.RpcServerResponse; | ||
| import brave.rpc.RpcTracing; | ||
|
|
||
| /** | ||
| * Decorates an {@link RpcService} to trace inbound {@link RpcRequest}s using | ||
| * <a href="https://github.com/openzipkin/brave">Brave</a>. | ||
| */ | ||
| @UnstableApi | ||
| public final class BraveRpcService extends AbstractBraveService<RpcServerRequest, RpcServerResponse, | ||
| RpcRequest, RpcResponse> implements RpcService { | ||
|
|
||
| private static final RpcRequestParser defaultRequestParser = (request, context, span) -> { | ||
| RpcRequestParser.DEFAULT.parse(request, context, span); | ||
| BraveServerParsers.rpcRequestParser().parse(request, context, span); | ||
| }; | ||
|
|
||
| private static final RpcResponseParser defaultResponseParser = (response, context, span) -> { | ||
| RpcResponseParser.DEFAULT.parse(response, context, span); | ||
| BraveServerParsers.rpcResponseParser().parse(response, context, span); | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a new tracing {@link RpcService} decorator using the specified {@link Tracing} instance. | ||
| */ | ||
| public static Function<? super RpcService, BraveRpcService> | ||
| newDecorator(Tracing tracing) { | ||
| return newDecorator(RpcTracing.newBuilder(tracing) | ||
| .serverRequestParser(defaultRequestParser) | ||
| .serverResponseParser(defaultResponseParser) | ||
| .build()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new tracing {@link RpcService} decorator using the specified {@link RpcTracing} instance. | ||
| */ | ||
| public static Function<? super RpcService, BraveRpcService> | ||
| newDecorator(RpcTracing rpcTracing) { | ||
| ensureScopeUsesRequestContext(rpcTracing.tracing()); | ||
| return service -> new BraveRpcService(service, rpcTracing); | ||
| } | ||
|
|
||
| private final RpcServerHandler handler; | ||
|
|
||
| private BraveRpcService(RpcService delegate, RpcTracing rpcTracing) { | ||
| super(delegate, rpcTracing.tracing().tracer(), | ||
| (RequestContextCurrentTraceContext) rpcTracing.tracing().currentTraceContext()); | ||
| handler = RpcServerHandler.create(rpcTracing); | ||
| } | ||
|
|
||
| @Override | ||
| RpcServerRequest braveRequest(ServiceRequestContext ctx) { | ||
| return RpcServiceRequestContextAdapter.asRpcServerRequest(ctx); | ||
| } | ||
|
|
||
| @Override | ||
| RpcServerResponse braveResponse(ServiceRequestContext ctx, RequestLog log, RpcServerRequest braveReq) { | ||
| return RpcServiceRequestContextAdapter.asRpcServerResponse(ctx, log, braveReq); | ||
| } | ||
|
|
||
| @Override | ||
| Span handleReceive(RpcServerRequest braveReq) { | ||
| return handler.handleReceive(braveReq); | ||
| } | ||
|
|
||
| @Override | ||
| void handleSend(RpcServerResponse response, Span span) { | ||
| handler.handleSend(response, span); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.