-
Notifications
You must be signed in to change notification settings - Fork 782
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
Instrument ConnectionSource in Akka/Pekko HTTP Servers #11103
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3eed1a7
Update Akka to instrument the one missing method
zackman0010 984bf37
Add version restriction to Akka ForkJoin
zackman0010 84f2196
Add tests for ServerSource, copy instrumentation to Pekko
zackman0010 2f9ff44
Ran spotlessApply
zackman0010 efce0e7
Missed changing an Akka->Pekko after copy/paste
zackman0010 441a73b
Updating Muzzle versions for akka-fork-join
zackman0010 6ff9d69
Spotless fix
zackman0010 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
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
42 changes: 42 additions & 0 deletions
42
...emetry/javaagent/instrumentation/akkahttp/server/AkkaHttpServerSourceInstrumentation.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,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.akkahttp.server; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import akka.http.scaladsl.model.HttpRequest; | ||
import akka.http.scaladsl.model.HttpResponse; | ||
import akka.stream.scaladsl.Flow; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class AkkaHttpServerSourceInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("akka.http.scaladsl.Http$IncomingConnection"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("handleWith").and(takesArgument(0, named("akka.stream.scaladsl.Flow"))), | ||
this.getClass().getName() + "$AkkaBindAndHandleAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class AkkaBindAndHandleAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void wrapHandler( | ||
@Advice.Argument(value = 0, readOnly = false) Flow<HttpRequest, HttpResponse, ?> handler) { | ||
handler = AkkaFlowWrapper.wrap(handler); | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
.../opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpTestServerSourceWebServer.scala
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,124 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.akkahttp | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.Http.ServerBinding | ||
import akka.http.scaladsl.model.StatusCodes.Found | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.scaladsl.Sink | ||
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest | ||
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint._ | ||
|
||
import java.util.function.Supplier | ||
import scala.concurrent.Await | ||
|
||
object AkkaHttpTestServerSourceWebServer { | ||
implicit val system = ActorSystem("my-system") | ||
implicit val materializer = ActorMaterializer() | ||
// needed for the future flatMap/onComplete in the end | ||
implicit val executionContext = system.dispatcher | ||
|
||
var route = get { | ||
concat( | ||
path(SUCCESS.rawPath()) { | ||
complete( | ||
AbstractHttpServerTest.controller(SUCCESS, supplier(SUCCESS.getBody)) | ||
) | ||
}, | ||
path(INDEXED_CHILD.rawPath()) { | ||
parameterMap { map => | ||
val supplier = new Supplier[String] { | ||
def get(): String = { | ||
INDEXED_CHILD.collectSpanAttributes(new UrlParameterProvider { | ||
override def getParameter(name: String): String = | ||
map.get(name).orNull | ||
}) | ||
"" | ||
} | ||
} | ||
complete(AbstractHttpServerTest.controller(INDEXED_CHILD, supplier)) | ||
} | ||
}, | ||
path(QUERY_PARAM.rawPath()) { | ||
extractUri { uri => | ||
complete( | ||
AbstractHttpServerTest | ||
.controller(INDEXED_CHILD, supplier(uri.queryString().orNull)) | ||
) | ||
} | ||
}, | ||
path(REDIRECT.rawPath()) { | ||
redirect( | ||
AbstractHttpServerTest | ||
.controller(REDIRECT, supplier(REDIRECT.getBody)), | ||
Found | ||
) | ||
}, | ||
path(ERROR.rawPath()) { | ||
complete( | ||
500 -> AbstractHttpServerTest | ||
.controller(ERROR, supplier(ERROR.getBody)) | ||
) | ||
}, | ||
path("path" / LongNumber / "param") { id => | ||
complete( | ||
AbstractHttpServerTest.controller(PATH_PARAM, supplier(id.toString)) | ||
) | ||
}, | ||
path( | ||
"test1" / IntNumber / HexIntNumber / LongNumber / HexLongNumber / | ||
DoubleNumber / JavaUUID / Remaining | ||
) { (_, _, _, _, _, _, _) => | ||
complete(SUCCESS.getBody) | ||
}, | ||
pathPrefix("test2") { | ||
concat( | ||
path("first") { | ||
complete(SUCCESS.getBody) | ||
}, | ||
path("second") { | ||
complete(SUCCESS.getBody) | ||
} | ||
) | ||
} | ||
) | ||
} | ||
|
||
private var binding: ServerBinding = null | ||
|
||
def start(port: Int): Unit = synchronized { | ||
if (null == binding) { | ||
import scala.concurrent.duration._ | ||
binding = Await.result( | ||
Http() | ||
.bind("localhost", port) | ||
.map(_.handleWith(route)) | ||
.to(Sink.ignore) | ||
.run(), | ||
10.seconds | ||
) | ||
} | ||
} | ||
|
||
def stop(): Unit = synchronized { | ||
if (null != binding) { | ||
binding.unbind() | ||
system.terminate() | ||
binding = null | ||
} | ||
} | ||
|
||
def supplier(string: String): Supplier[String] = { | ||
new Supplier[String] { | ||
def get(): String = { | ||
string | ||
} | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...javaagent/instrumentation/pekkohttp/v1_0/server/PekkoHttpServerSourceInstrumentation.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,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.pekko.http.scaladsl.model.HttpRequest; | ||
import org.apache.pekko.http.scaladsl.model.HttpResponse; | ||
import org.apache.pekko.stream.scaladsl.Flow; | ||
|
||
public class PekkoHttpServerSourceInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.pekko.http.scaladsl.Http$IncomingConnection"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("handleWith").and(takesArgument(0, named("org.apache.pekko.stream.scaladsl.Flow"))), | ||
this.getClass().getName() + "$PekkoBindAndHandleAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class PekkoBindAndHandleAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void wrapHandler( | ||
@Advice.Argument(value = 0, readOnly = false) Flow<HttpRequest, HttpResponse, ?> handler) { | ||
handler = PekkoFlowWrapper.wrap(handler); | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...lemetry/javaagent/instrumentation/pekkohttp/v1_0/PekkoHttpTestServerSourceWebServer.scala
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,124 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0 | ||
|
||
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest | ||
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint._ | ||
import org.apache.pekko.actor.ActorSystem | ||
import org.apache.pekko.http.scaladsl.Http | ||
import org.apache.pekko.http.scaladsl.Http.ServerBinding | ||
import org.apache.pekko.http.scaladsl.model.StatusCodes.Found | ||
import org.apache.pekko.http.scaladsl.server.Directives._ | ||
import org.apache.pekko.stream.ActorMaterializer | ||
import org.apache.pekko.stream.scaladsl.Sink | ||
|
||
import java.util.function.Supplier | ||
import scala.concurrent.Await | ||
|
||
object PekkoHttpTestServerSourceWebServer { | ||
implicit val system = ActorSystem("my-system") | ||
implicit val materializer = ActorMaterializer() | ||
// needed for the future flatMap/onComplete in the end | ||
implicit val executionContext = system.dispatcher | ||
|
||
var route = get { | ||
concat( | ||
path(SUCCESS.rawPath()) { | ||
complete( | ||
AbstractHttpServerTest.controller(SUCCESS, supplier(SUCCESS.getBody)) | ||
) | ||
}, | ||
path(INDEXED_CHILD.rawPath()) { | ||
parameterMap { map => | ||
val supplier = new Supplier[String] { | ||
def get(): String = { | ||
INDEXED_CHILD.collectSpanAttributes(new UrlParameterProvider { | ||
override def getParameter(name: String): String = | ||
map.get(name).orNull | ||
}) | ||
"" | ||
} | ||
} | ||
complete(AbstractHttpServerTest.controller(INDEXED_CHILD, supplier)) | ||
} | ||
}, | ||
path(QUERY_PARAM.rawPath()) { | ||
extractUri { uri => | ||
complete( | ||
AbstractHttpServerTest | ||
.controller(INDEXED_CHILD, supplier(uri.queryString().orNull)) | ||
) | ||
} | ||
}, | ||
path(REDIRECT.rawPath()) { | ||
redirect( | ||
AbstractHttpServerTest | ||
.controller(REDIRECT, supplier(REDIRECT.getBody)), | ||
Found | ||
) | ||
}, | ||
path(ERROR.rawPath()) { | ||
complete( | ||
500 -> AbstractHttpServerTest | ||
.controller(ERROR, supplier(ERROR.getBody)) | ||
) | ||
}, | ||
path("path" / LongNumber / "param") { id => | ||
complete( | ||
AbstractHttpServerTest.controller(PATH_PARAM, supplier(id.toString)) | ||
) | ||
}, | ||
path( | ||
"test1" / IntNumber / HexIntNumber / LongNumber / HexLongNumber / | ||
DoubleNumber / JavaUUID / Remaining | ||
) { (_, _, _, _, _, _, _) => | ||
complete(SUCCESS.getBody) | ||
}, | ||
pathPrefix("test2") { | ||
concat( | ||
path("first") { | ||
complete(SUCCESS.getBody) | ||
}, | ||
path("second") { | ||
complete(SUCCESS.getBody) | ||
} | ||
) | ||
} | ||
) | ||
} | ||
|
||
private var binding: ServerBinding = null | ||
|
||
def start(port: Int): Unit = synchronized { | ||
if (null == binding) { | ||
import scala.concurrent.duration._ | ||
binding = Await.result( | ||
Http() | ||
.bind("localhost", port) | ||
.map(_.handleWith(route)) | ||
.to(Sink.ignore) | ||
.run(), | ||
10.seconds | ||
) | ||
} | ||
} | ||
|
||
def stop(): Unit = synchronized { | ||
if (null != binding) { | ||
binding.unbind() | ||
system.terminate() | ||
binding = null | ||
} | ||
} | ||
|
||
def supplier(string: String): Supplier[String] = { | ||
new Supplier[String] { | ||
def get(): String = { | ||
string | ||
} | ||
} | ||
} | ||
} |
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.
akka-actor_2.11
doesn't look like it goes above 2.5.xmaybe instead (or in addition), add
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.
Good point! Pushed a new commit with that change.