-
Notifications
You must be signed in to change notification settings - Fork 327
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
+ kamon-armeria module #854
Open
lucasamoroso
wants to merge
34
commits into
kamon-io:master
Choose a base branch
from
lucasamoroso:kamon-armeria-instrumentation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
8436fb0
+ kamon-armeria module
lucasamoroso aa08c74
rename p to channelPipeline
lucasamoroso 5dde833
remove semicolons
lucasamoroso 2e45248
fix NPE when Armeria makes a redirect
lucasamoroso b395138
kamon-armeria reimplementation
lucasamoroso a2e880a
Operation name refactor and https tests
lucasamoroso 4a8dee2
Add gRPC tests
lucasamoroso 8d51bf9
Fix Kamon context propagation
lucasamoroso 3b5d587
Fix unwrap()
lucasamoroso 8b77a59
refactor route not found
lucasamoroso 5e34824
refactor kamon operation name
lucasamoroso c3b0a31
refactor armeria server instrumentation
lucasamoroso ecf6990
rename variables in tests
lucasamoroso d73aa92
update armeria dependency
lucasamoroso 531698e
rename http instrumentation component
lucasamoroso 80feea6
rename package
lucasamoroso 96cc3fd
update armeria version
lucasamoroso 6493f74
update test routes
lucasamoroso fb71f65
add blocking route test
lucasamoroso 32acb5b
remove runWithContext call
lucasamoroso 3641ad7
fix tests
lucasamoroso e2a1b04
apply new sbt syntax
lucasamoroso 3b9e856
kamon-armeria can be built from scala 2.11
lucasamoroso 3fd0f26
fix some warnings
lucasamoroso 54a714b
access armeria ports through reflection instead of bridge
lucasamoroso bfe41d8
add armeria module description
lucasamoroso 8a34a3d
upgrade armeria version
lucasamoroso 3cf5be7
remove scalapb compiler plugin
lucasamoroso c5c1571
cleanup context propagation
lucasamoroso 7f74090
move configs to improve reconfiguring Kamon
lucasamoroso 8d041ab
move configs to improve reconfiguring Kamon
lucasamoroso 901bb25
cleanup
lucasamoroso 241f2d2
bump up armeria version
lucasamoroso ee37133
use route pattern string as server side operation name
lucasamoroso 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 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
56 changes: 56 additions & 0 deletions
56
...rmeria/src/main/java/kamon/instrumentation/armeria/client/ArmeriaHttpClientDecorator.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,56 @@ | ||
/* ========================================================================================= | ||
* Copyright © 2013-2020 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 kamon.instrumentation.armeria.client; | ||
|
||
import com.linecorp.armeria.client.ClientRequestContext; | ||
import com.linecorp.armeria.client.HttpClient; | ||
import com.linecorp.armeria.client.SimpleDecoratingHttpClient; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import kamon.Kamon; | ||
import kamon.instrumentation.armeria.client.timing.Timing; | ||
import kamon.instrumentation.armeria.converters.KamonArmeriaMessageConverter; | ||
import kamon.instrumentation.http.HttpClientInstrumentation; | ||
|
||
public class ArmeriaHttpClientDecorator extends SimpleDecoratingHttpClient { | ||
private final HttpClientInstrumentation clientInstrumentation; | ||
|
||
protected ArmeriaHttpClientDecorator(HttpClient delegate, HttpClientInstrumentation clientInstrumentation) { | ||
super(delegate); | ||
this.clientInstrumentation = clientInstrumentation; | ||
} | ||
|
||
@Override | ||
public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) throws Exception { | ||
|
||
final HttpClientInstrumentation.RequestHandler<HttpRequest> requestHandler = | ||
clientInstrumentation.createHandler(KamonArmeriaMessageConverter.getRequestBuilder(req), Kamon.currentContext()); | ||
|
||
ctx.log() | ||
.whenComplete() | ||
.thenAccept(log -> { | ||
Timing.takeTimings(log, requestHandler.span()); | ||
requestHandler.processResponse(KamonArmeriaMessageConverter.toKamonResponse(log)); | ||
}); | ||
|
||
try { | ||
return unwrap().execute(ctx, requestHandler.request()); | ||
} catch (Exception exception) { | ||
requestHandler.span().fail(exception.getMessage(), exception).finish(); | ||
throw exception; | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ation/kamon-armeria/src/main/java/kamon/instrumentation/armeria/client/timing/Timing.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,73 @@ | ||
/* ========================================================================================= | ||
* Copyright © 2013-2020 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 kamon.instrumentation.armeria.client.timing; | ||
|
||
import com.linecorp.armeria.common.logging.ClientConnectionTimings; | ||
import com.linecorp.armeria.common.logging.RequestLog; | ||
import kamon.Kamon; | ||
import kamon.trace.Span; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Based on Armeria | ||
* <a href="https://github.com/line/armeria/blob/master/brave/src/main/java/com/linecorp/armeria/client/brave/BraveClient.java">BraveClient</a> | ||
* implementation. | ||
*/ | ||
public final class Timing { | ||
|
||
public static void takeTimings(RequestLog log, Span span) { | ||
ClientConnectionTimings timings = log.connectionTimings(); | ||
if (timings != null) { | ||
|
||
logTiming(span, "connection-acquire.start", "connection-acquire.end", | ||
timings.connectionAcquisitionStartTimeMicros(), | ||
timings.connectionAcquisitionDurationNanos()); | ||
|
||
if (timings.dnsResolutionDurationNanos() != -1) { | ||
logTiming(span, "dns-resolve.start", "dns-resolve.end", | ||
timings.dnsResolutionStartTimeMicros(), | ||
timings.dnsResolutionDurationNanos()); | ||
} | ||
|
||
if (timings.socketConnectDurationNanos() != -1) { | ||
logTiming(span, "socket-connect.start", "socket-connect.end", | ||
timings.socketConnectStartTimeMicros(), | ||
timings.socketConnectDurationNanos()); | ||
} | ||
|
||
if (timings.pendingAcquisitionDurationNanos() != -1) { | ||
logTiming(span, "connection-reuse.start", "connection-reuse.end", | ||
timings.pendingAcquisitionStartTimeMicros(), | ||
timings.pendingAcquisitionDurationNanos()); | ||
} | ||
} | ||
} | ||
|
||
private static void logTiming(Span span, | ||
String startName, | ||
String endName, | ||
long startTimeMicros, | ||
long durationNanos) { | ||
|
||
final long startTimeNanos = TimeUnit.NANOSECONDS.convert(startTimeMicros, TimeUnit.MICROSECONDS); | ||
|
||
span.mark(startName, Kamon.clock().toInstant(startTimeNanos)); | ||
span.mark(endName, Kamon.clock().toInstant(startTimeNanos + durationNanos)); | ||
} | ||
|
||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...rmeria/src/main/java/kamon/instrumentation/armeria/server/ArmeriaHttpServerDecorator.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,80 @@ | ||
/* ========================================================================================= | ||
* Copyright © 2013-2020 the kamon project <http://kamon.io/> | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 kamon.instrumentation.armeria.server; | ||
|
||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.server.HttpService; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
import com.linecorp.armeria.server.SimpleDecoratingHttpService; | ||
import io.netty.util.AttributeKey; | ||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage; | ||
import kamon.instrumentation.armeria.converters.KamonArmeriaMessageConverter; | ||
import kamon.instrumentation.http.HttpServerInstrumentation; | ||
|
||
import java.util.Map; | ||
|
||
public class ArmeriaHttpServerDecorator extends SimpleDecoratingHttpService { | ||
public static final AttributeKey<HttpServerInstrumentation.RequestHandler> REQUEST_HANDLER_TRACE_KEY = | ||
AttributeKey.valueOf(HttpServerInstrumentation.RequestHandler.class, "REQUEST_HANDLER_TRACE"); | ||
|
||
private final Map<Integer, HttpServerInstrumentation> serverInstrumentationMap; | ||
|
||
public ArmeriaHttpServerDecorator(HttpService delegate, Map<Integer, HttpServerInstrumentation> serverInstrumentations) { | ||
super(delegate); | ||
this.serverInstrumentationMap = serverInstrumentations; | ||
} | ||
|
||
@Override | ||
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { | ||
final HttpServerInstrumentation httpServerInstrumentation = serverInstrumentationMap.get(req.uri().getPort()); | ||
|
||
if (httpServerInstrumentation != null) { | ||
|
||
final HttpServerInstrumentation.RequestHandler requestHandler = | ||
httpServerInstrumentation.createHandler(KamonArmeriaMessageConverter.toRequest(req)); | ||
|
||
ctx.log() | ||
.whenComplete() | ||
.thenAccept(log -> { | ||
final Context context = ctx.attr(REQUEST_HANDLER_TRACE_KEY).context(); | ||
/** | ||
* This is true only when no configured Route was matched. | ||
* Cases where the route is fallback should be managed here {@link kamon.instrumentation.armeria.server.HandleNotFoundMethodAdvisor#around} | ||
*/ | ||
if (!ctx.config().route().isFallback()) { | ||
requestHandler.span().name(ctx.config().route().patternString()); | ||
} | ||
|
||
requestHandler.buildResponse(KamonArmeriaMessageConverter.toResponse(log), context); | ||
requestHandler.responseSent(); | ||
}); | ||
|
||
try (Storage.Scope ignored = Kamon.storeContext(requestHandler.context())) { | ||
ctx.setAttr(REQUEST_HANDLER_TRACE_KEY, requestHandler); | ||
return unwrap().serve(ctx, req); | ||
} | ||
} else { | ||
return unwrap().serve(ctx, req); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use
ctx.config().route().patternString()
to set the operation name on the Spans and completely eliminate the name generator. Using the pattern string ensures we don't get cardinality issues and users won't need to add any operation mappings on the configuration file.Plus, it works with all the possible service types as far as I could see!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to implement this suggested change like this
And I've some concerns about this:
ctx.config().route().patternString()
will return the request path even if the route does not exist. The current implementation ensures that the operation name will be set tounhandled
(because of this) in this case. Probably we can check if we already set this operation name to the configured value for unhandled requests. I was wondering which will be a better approach, what do you think?ClientRequestContext
, does not have a method likectx.config().route().patternString()
so we'll need the operation name generator, right? I understand that users could have cardinality issues with the current implementation if they don't set operation mappings but I think there is nothing we can do hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where we at here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using the
patternString()
method provided by Armeria to generate operation names only in the server side, and we set the operation name asunhandled
by default (it's configurable) if the route wasn't found.In the client side we still use the custom operation name generator because we don't have an equivalent
patternString()
method. at least that is true until the version of Armeria that we are currently using which is not the latest.