-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
366 add http route
- Loading branch information
Showing
26 changed files
with
792 additions
and
49 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,6 @@ on: | |
types: | ||
- published | ||
|
||
env: | ||
SBT_NATIVE_CLIENT: true | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
|
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
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
127 changes: 127 additions & 0 deletions
127
otel-extension/src/it/scala/io/scalac/mesmer/instrumentation/akka/http/HttpRouteTest.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,127 @@ | ||
package io.scalac.mesmer.instrumentation.akka.http | ||
|
||
import akka.http.scaladsl.model.StatusCodes | ||
import io.scalac.mesmer.agent.utils.{ OtelAgentTest, SafeLoadSystem } | ||
import io.scalac.mesmer.core.util.ReceptionistOps | ||
import org.scalatest.OptionValues | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.flatspec.AnyFlatSpecLike | ||
import org.scalatest.matchers.should.Matchers | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.server._ | ||
import akka.http.scaladsl.testkit.ScalatestRouteTest | ||
import akka.util.Timeout | ||
import scala.util.Try | ||
import io.opentelemetry.context.{ Context, ContextKey } | ||
import io.scalac.mesmer.otelextension.instrumentations.akka.http.{ RouteContext, RouteTemplateHolder } | ||
|
||
import scala.concurrent.Promise | ||
import scala.concurrent.duration._ | ||
|
||
class HttpRouteTest | ||
extends OtelAgentTest | ||
with AnyFlatSpecLike | ||
with Matchers | ||
with ScalaFutures | ||
with ScalatestRouteTest | ||
with OptionValues | ||
with ReceptionistOps { | ||
|
||
implicit val timeout: Timeout = 5.seconds | ||
|
||
it should ("add a uuid template") in { | ||
val promise = Promise[String]() | ||
|
||
val route: Route = (pathPrefix("api" / "v1" / JavaUUID) & pathEndOrSingleSlash) { _ => | ||
val template = RouteContext.retrieveFromCurrent.get() | ||
promise.success(template) | ||
complete(StatusCodes.OK) | ||
|
||
} | ||
|
||
Get("/api/v1/84a5c573-4643-4bb5-a50d-776a8fca0a5b") ~!> route ~> check { | ||
status should be(StatusCodes.OK) | ||
} | ||
|
||
whenReady(promise.future) { template => | ||
template should be("/api/v1/<uuid>") | ||
} | ||
} | ||
|
||
it should ("add a number template") in { | ||
val promise = Promise[String]() | ||
|
||
val route: Route = (pathPrefix("api" / "v1" / IntNumber) & pathEndOrSingleSlash) { _ => | ||
val template = RouteContext.retrieveFromCurrent.get() | ||
promise.success(template) | ||
complete(StatusCodes.OK) | ||
|
||
} | ||
|
||
Get("/api/v1/100") ~!> route ~> check { | ||
status should be(StatusCodes.OK) | ||
} | ||
|
||
whenReady(promise.future) { template => | ||
template should be("/api/v1/<number>") | ||
} | ||
} | ||
|
||
it should ("add a wildcard for segment template") in { | ||
val promise = Promise[String]() | ||
|
||
val route: Route = (pathPrefix("api" / "v1" / Segment) & pathEndOrSingleSlash) { _ => | ||
val template = RouteContext.retrieveFromCurrent.get() | ||
promise.success(template) | ||
complete(StatusCodes.OK) | ||
|
||
} | ||
|
||
Get("/api/v1/anu-thing") ~!> route ~> check { | ||
status should be(StatusCodes.OK) | ||
} | ||
|
||
whenReady(promise.future) { template => | ||
template should be("/api/v1/*") | ||
} | ||
} | ||
|
||
it should ("add a uuid template for or matcher") in { | ||
val promise = Promise[String]() | ||
|
||
val route: Route = (pathPrefix("api" / "v1" / (JavaUUID | IntNumber)) & pathEndOrSingleSlash) { _ => | ||
val template = RouteContext.retrieveFromCurrent.get() | ||
promise.success(template) | ||
complete(StatusCodes.OK) | ||
|
||
} | ||
|
||
Get("/api/v1/84a5c573-4643-4bb5-a50d-776a8fca0a5b") ~!> route ~> check { | ||
status should be(StatusCodes.OK) | ||
} | ||
|
||
whenReady(promise.future) { template => | ||
template should be("/api/v1/<uuid>") | ||
} | ||
} | ||
|
||
it should ("add a number template for or matcher") in { | ||
val promise = Promise[String]() | ||
|
||
val route: Route = (pathPrefix("api" / "v1" / (JavaUUID | LongNumber)) & pathEndOrSingleSlash) { _ => | ||
val template = RouteContext.retrieveFromCurrent.get() | ||
promise.success(template) | ||
complete(StatusCodes.OK) | ||
|
||
} | ||
|
||
Get("/api/v1/100") ~!> route ~> check { | ||
status should be(StatusCodes.OK) | ||
} | ||
|
||
whenReady(promise.future) { template => | ||
template should be("/api/v1/<number>") | ||
} | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...rc/main/java/io/scalac/mesmer/instrumentation/http/impl/AndThenMatchedMatchingAdvice.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,26 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class AndThenMatchedMatchingAdvice { | ||
|
||
@Advice.OnMethodExit | ||
public static void onExit( | ||
@Advice.This PathMatcher.Matching<?> self, @Advice.Return PathMatcher.Matching<?> result) { | ||
|
||
String value = VirtualField.find(PathMatcher.Matching.class, String.class).get(self); | ||
|
||
if (value != null) { | ||
|
||
String innerValue = VirtualField.find(PathMatcher.Matching.class, String.class).get(result); | ||
if (innerValue != null) { | ||
|
||
VirtualField.find(PathMatcher.Matching.class, String.class).set(result, value + innerValue); | ||
} else { | ||
VirtualField.find(PathMatcher.Matching.class, String.class).set(result, value); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...xtension/src/main/java/io/scalac/mesmer/instrumentation/http/impl/AsyncHandlerAdvice.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,20 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.model.HttpRequest; | ||
import akka.http.scaladsl.model.HttpResponse; | ||
import akka.stream.Materializer; | ||
import io.scalac.mesmer.otelextension.instrumentations.akka.http.UpdateHttpRouteWrapper; | ||
import net.bytebuddy.asm.Advice; | ||
import scala.Function1; | ||
import scala.concurrent.Future; | ||
|
||
public class AsyncHandlerAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void wrapHandler( | ||
@Advice.Argument(value = 0, readOnly = false) | ||
Function1<HttpRequest, Future<HttpResponse>> handler, | ||
@Advice.Argument(7) Materializer materialzier) { | ||
handler = new UpdateHttpRouteWrapper(handler, materialzier.executionContext()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ension/src/main/java/io/scalac/mesmer/instrumentation/http/impl/DoubleTemplateAdvice.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,14 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class DoubleTemplateAdvice { | ||
|
||
@Advice.OnMethodEnter | ||
public static void onEnter(@Advice.Argument(value = 0) PathMatcher<?> result) { | ||
|
||
VirtualField.find(PathMatcher.class, String.class).set(result, "<double>"); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...tension/src/main/java/io/scalac/mesmer/instrumentation/http/impl/EmptyTemplateAdvice.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,13 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class EmptyTemplateAdvice { | ||
|
||
@Advice.OnMethodExit | ||
public static void onExit(@Advice.This PathMatcher<?> self) { | ||
VirtualField.find(PathMatcher.class, String.class).set(self, ""); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...on/src/main/java/io/scalac/mesmer/instrumentation/http/impl/MapMatchedMatchingAdvice.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,19 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class MapMatchedMatchingAdvice { | ||
|
||
@Advice.OnMethodExit | ||
public static void onExit( | ||
@Advice.This PathMatcher.Matching<?> self, @Advice.Return PathMatcher.Matching<?> result) { | ||
|
||
String value = VirtualField.find(PathMatcher.Matching.class, String.class).get(self); | ||
if (value != null) { | ||
|
||
VirtualField.find(PathMatcher.Matching.class, String.class).set(result, value); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...nsion/src/main/java/io/scalac/mesmer/instrumentation/http/impl/NeutralTemplateAdvice.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,14 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class NeutralTemplateAdvice { | ||
|
||
@Advice.OnMethodEnter | ||
public static void onEnter(@Advice.Argument(value = 0) PathMatcher<?> result) { | ||
|
||
VirtualField.find(PathMatcher.class, String.class).set(result, ""); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ension/src/main/java/io/scalac/mesmer/instrumentation/http/impl/NumberTemplateAdvice.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,13 @@ | ||
package io.scalac.mesmer.instrumentation.http.impl; | ||
|
||
import akka.http.scaladsl.server.PathMatcher; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class NumberTemplateAdvice { | ||
|
||
@Advice.OnMethodExit | ||
public static void onExit(@Advice.This PathMatcher<?> self) { | ||
VirtualField.find(PathMatcher.class, String.class).set(self, "<number>"); | ||
} | ||
} |
Oops, something went wrong.