Skip to content

Commit 462b383

Browse files
committed
rework tracing and instrumentation
1 parent 6811d95 commit 462b383

30 files changed

+647
-491
lines changed

build.sbt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import sbt.Keys.cleanFiles
44
ThisBuild / scalaVersion := Dependencies.scalaVersion
55
scalaVersion := Dependencies.scalaVersion
66

7-
val releaseVersion = sys.env.getOrElse("TAG", "2.0.108")
7+
val releaseVersion = sys.env.getOrElse("TAG", "2.0.0-alpha.rc.4")
88
addCommandAlias("packageSmithy4Play", "smithy4play/package")
9-
addCommandAlias("publishSmithy4Play", "smithy4play/publish")
9+
addCommandAlias("publishSmithy4Play", "smithy4play/publish;smithy4playInstrumentation/publish")
1010
addCommandAlias("publishLocalWithInstrumentation", "publishLocalSmithy4PlayInstrumentation;publishLocalSmithy4Play")
1111
addCommandAlias("publishLocalSmithy4PlayInstrumentation", "smithy4playInstrumentation/publishLocal")
1212
addCommandAlias("publishLocalSmithy4Play", "smithy4play/publishLocal")

project/plugins.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ addSbtPlugin("com.codecommit" %% "sbt-github-packages" % "0.5.3")
22

33
addSbtPlugin("org.scalameta" %% "sbt-scalafmt" % "2.5.2")
44

5-
addSbtPlugin("org.playframework" %% "sbt-plugin" % "3.0.4")
5+
addSbtPlugin("org.playframework" %% "sbt-plugin" % "3.0.5")
66
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "2.0.12")
77
addSbtPlugin("com.disneystreaming.smithy4s" %% "smithy4s-sbt-codegen" % "0.18.22")
88

smithy4play-instrumentation/src/main/scala/de/innfactory/smithy4play/instrumentation/Smithy4PlaySingleton.scala

+1-29
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,5 @@ object Smithy4PlaySingleton {
1212
private val SPAN_NAME = "play.request"
1313

1414
private val spanKindExtractor = SpanKindExtractor.alwaysServer()
15-
private val INSTRUMENTER = Instrumenter
16-
.builder[Void, Void](GlobalOpenTelemetry.get, "io.opentelemetry.smtihyt4play", (s) => SPAN_NAME)
17-
.setEnabled(true)
18-
.buildInstrumenter(spanKindExtractor)
19-
20-
.pipe { (v: Instrumenter[Void, Void]) =>
21-
println(v.toString)
22-
println("shouldstart: " + v.shouldStart(Context.root(), null))
23-
v
24-
}
25-
26-
def test() = {
27-
println("Smithy4PlaySingleton test")
28-
}
29-
30-
def shouldStart(context: Context): Boolean = {
31-
try {
32-
INSTRUMENTER.shouldStart(context, null)
33-
} catch
34-
case e: Exception => {
35-
e.printStackTrace()
36-
println(e.getMessage)
37-
println(e.getCause)
38-
false
39-
}
40-
41-
}
42-
43-
def instrumenter() = INSTRUMENTER
15+
4416
}

smithy4play-instrumentation/src/main/scala/de/innfactory/smithy4play/instrumentation/TestInstrumentation.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static net.bytebuddy.matcher.ElementMatchers.named;
77
import static net.bytebuddy.matcher.ElementMatchers.returns;
88
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
9-
import static de.innfactory.smithy4play.instrumentation.Smithy4PlaySingleton.test;
109

1110
import io.opentelemetry.api.GlobalOpenTelemetry;
1211
import io.opentelemetry.api.trace.SpanBuilder;
@@ -54,17 +53,15 @@ public static void onEnter(
5453
@Advice.Local("otelContext") Context context,
5554
@Advice.Local("otelScope") Scope scope) {
5655

57-
System.out.println("TestInstrumentation ApplyAdvice onEnter " + test);
56+
//System.out.println("TestInstrumentation ApplyAdvice onEnter " + test);
5857
// span.addEvent("ADVICE smithy4play " + test);
59-
test();
6058
Context parentContext = currentContext();
61-
Boolean shouldStart = Smithy4PlaySingleton.shouldStart(parentContext);
62-
System.out.println("TestInstrumentation ApplyAdvice shouldStart " + shouldStart);
59+
//System.out.println("TestInstrumentation ApplyAdvice shouldStart " + shouldStart);
6360
Span mySpan = GlobalOpenTelemetry.get().getTracer("smithy4play").spanBuilder("test span").startSpan();
64-
System.out.println("TestInstrumentation ApplyAdvice mySpan " + mySpan.getSpanContext().getSpanId());
61+
//System.out.println("TestInstrumentation ApplyAdvice mySpan " + mySpan.getSpanContext().getSpanId());
6562
context = mySpan.storeInContext(parentContext);
6663
Scope myScope = mySpan.makeCurrent();
67-
System.out.println("TestInstrumentation ApplyAdvice should start");
64+
//System.out.println("TestInstrumentation ApplyAdvice should start");
6865
scope = myScope;
6966
}
7067

@@ -91,7 +88,7 @@ public static void stopTraceOnResponse(
9188
throwable.printStackTrace();
9289
}
9390
if (context != null) {
94-
System.out.println("TestInstrumentation ApplyAdvice onExit update update Span name");
91+
// System.out.println("TestInstrumentation ApplyAdvice onExit update update Span name");
9592
// Span.fromContext(context).updateName(testout);
9693
}
9794
}

smithy4play/src/main/scala/de/innfactory/smithy4play/client/Smithy4PlayWsClient.scala

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.innfactory.smithy4play.client
22

3-
import cats.data.{ EitherT, Kleisli }
3+
import cats.data.EitherT
44
import play.api.libs.ws.{ writeableOf_ByteArray, WSClient, WSResponse }
55
import smithy4s.client.UnaryLowLevelClient
66
import smithy4s.http.{ CaseInsensitive, HttpRequest, HttpResponse }
@@ -16,22 +16,22 @@ class Smithy4PlayWsClient[Alg[_[_, _, _, _, _]]](
1616
requestIsSuccessful: (Hints, HttpResponse[Blob]) => Boolean = matchStatusCodeForResponse,
1717
explicitDefaultsEncoding: Boolean = true
1818
)(implicit ec: ExecutionContext, wsClient: WSClient)
19-
extends UnaryLowLevelClient[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]] {
19+
extends UnaryLowLevelClient[FinishedClientResponse, HttpRequest[Blob], HttpResponse[Blob]] {
2020

2121
val underlyingClient = new SmithyPlayClient[Alg, Smithy4PlayWsClient[Alg]](
2222
baseUri = baseUri,
2323
service = service,
2424
client = this,
2525
middleware = middleware,
26-
requestIsSuccessful = (_, _) => true,
26+
requestIsSuccessful = requestIsSuccessful,
2727
toSmithy4sClient = x => x
2828
)
2929

30-
def transformer(): Alg[Kind1[ClientFinishedResponse]#toKind5] =
30+
def transformer(): Alg[Kind1[RunnableClientResponse]#toKind5] =
3131
underlyingClient.service.algebra(underlyingClient.compiler)
3232

3333
private def buildPath(req: HttpRequest[Blob]): String =
34-
baseUri + req.uri.path.mkString("/")
34+
baseUri + req.uri.path.mkString("/", "/", "")
3535

3636
private def toHeaders(request: HttpRequest[Blob]): List[(String, String)] =
3737
request.headers.flatMap { case (insensitive, strings) =>
@@ -52,7 +52,7 @@ class Smithy4PlayWsClient[Alg[_[_, _, _, _, _]]](
5252

5353
override def run[Output](
5454
request: HttpRequest[Blob]
55-
)(responseCB: HttpResponse[Blob] => RunnableClientRequest[Output]): RunnableClientRequest[Output] = Kleisli { _ =>
55+
)(responseCB: HttpResponse[Blob] => FinishedClientResponse[Output]): FinishedClientResponse[Output] = {
5656
val clientResponse = wsClient
5757
.url(buildPath(request))
5858
.withQueryStringParameters(toQueryParameters(request): _*)
@@ -63,13 +63,7 @@ class Smithy4PlayWsClient[Alg[_[_, _, _, _, _]]](
6363

6464
val httpResponse = clientResponse.map(wsRequestToResponse)
6565

66-
println("run ws client")
67-
68-
EitherT(httpResponse.flatMap { httpResponse =>
69-
println("httpresponse present and running internally")
70-
val v = responseCB(httpResponse).run(() => httpResponse).value
71-
v
72-
})
66+
EitherT(httpResponse.flatMap(responseCB(_).value))
7367

7468
}
7569
}
@@ -81,6 +75,6 @@ object Smithy4PlayWsClient {
8175
middleware: Endpoint.Middleware[Smithy4PlayWsClient[Alg]],
8276
requestIsSuccessful: (Hints, HttpResponse[Blob]) => Boolean = matchStatusCodeForResponse,
8377
explicitDefaultsEncoding: Boolean = true
84-
)(implicit ec: ExecutionContext, wsClient: WSClient): Alg[Kind1[ClientFinishedResponse]#toKind5] =
78+
)(implicit ec: ExecutionContext, wsClient: WSClient): Alg[Kind1[RunnableClientResponse]#toKind5] =
8579
new Smithy4PlayWsClient(baseUri, service, middleware, requestIsSuccessful, explicitDefaultsEncoding).transformer()
8680
}

smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClient.scala

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package de.innfactory.smithy4play.client
22

33
import cats.implicits.catsSyntaxApplicativeId
4-
import com.github.plokhotnyuk.jsoniter_scala.core.ReaderConfig
5-
import de.innfactory.smithy4play.client.RunnableClientRequest
64
import de.innfactory.smithy4play.client.core.Smithy4PlayClientCompiler
75
import de.innfactory.smithy4play.codecs.{ Codec, EndpointContentTypes }
8-
import de.innfactory.smithy4play.routing.internal.toSmithy4sHttpUri
9-
import smithy4s.capability.MonadThrowLike
106
import smithy4s.{ Blob, Endpoint, Hints, Service }
117
import smithy4s.http.{
12-
CaseInsensitive,
138
HttpDiscriminator,
14-
HttpEndpoint,
159
HttpMethod,
1610
HttpRequest,
1711
HttpResponse,
@@ -20,7 +14,7 @@ import smithy4s.http.{
2014
HttpUriScheme,
2115
Metadata
2216
}
23-
import smithy4s.client.{ UnaryClientCodecs, UnaryClientCompiler, UnaryClientEndpoint, UnaryLowLevelClient }
17+
import smithy4s.client.UnaryLowLevelClient
2418
import smithy4s.interopcats.monadThrowShim
2519

2620
import scala.concurrent.{ ExecutionContext, Future }
@@ -30,7 +24,7 @@ class SmithyPlayClient[Alg[_[_, _, _, _, _]], Client](
3024
val service: smithy4s.Service[Alg],
3125
client: Client,
3226
middleware: Endpoint.Middleware[Client],
33-
toSmithy4sClient: Client => UnaryLowLevelClient[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]],
27+
toSmithy4sClient: Client => UnaryLowLevelClient[FinishedClientResponse, HttpRequest[Blob], HttpResponse[Blob]],
3428
requestIsSuccessful: (Hints, HttpResponse[Blob]) => Boolean,
3529
explicitDefaultsEncoding: Boolean = true
3630
)(implicit executionContext: ExecutionContext)
@@ -48,21 +42,21 @@ class SmithyPlayClient[Alg[_[_, _, _, _, _]], Client](
4842
smithy4s.http.amazonErrorTypeHeader
4943
)
5044

51-
val clientCodecBuilder: HttpUnaryClientCodecs.Builder[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]] =
45+
val clientCodecBuilder: HttpUnaryClientCodecs.Builder[ClientResponse, HttpRequest[Blob], HttpResponse[Blob]] =
5246
HttpUnaryClientCodecs
53-
.builder[RunnableClientRequest]
54-
.withErrorDiscriminator(HttpDiscriminator.fromResponse(errorHeaders, _).pure[RunnableClientRequest])
47+
.builder[ClientResponse]
48+
.withErrorDiscriminator(HttpDiscriminator.fromResponse(errorHeaders, _).pure[ClientResponse])
5549
.withMetadataDecoders(Metadata.Decoder)
5650
.withMetadataEncoders(
5751
Metadata.Encoder.withExplicitDefaultsEncoding(explicitDefaultsEncoding)
5852
)
59-
.withBaseRequest(_ => baseRequest.pure[RunnableClientRequest])
53+
.withBaseRequest(_ => baseRequest.pure[ClientResponse])
6054

6155
val compiledClientCodec
62-
: EndpointContentTypes => HttpUnaryClientCodecs.Builder[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]] =
56+
: EndpointContentTypes => HttpUnaryClientCodecs.Builder[ClientResponse, HttpRequest[Blob], HttpResponse[Blob]] =
6357
buildClientCodecFromBase(clientCodecBuilder)
6458

65-
val compiler: service.FunctorEndpointCompiler[ClientFinishedResponse] = Smithy4PlayClientCompiler[Alg, Client](
59+
val compiler: service.FunctorEndpointCompiler[RunnableClientResponse] = Smithy4PlayClientCompiler[Alg, Client](
6660
service = service,
6761
client = client,
6862
toSmithy4sClient = toSmithy4sClient,

smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayTestUtils.scala

+29-26
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,50 @@ import scala.concurrent.{ Await, ExecutionContext }
99

1010
object SmithyPlayTestUtils {
1111

12-
implicit class EnhancedResponse[O](response: RunnableClientRequest[O]) {
12+
implicit class EnhancedResponse[O](response: RunnableClientResponse[O]) {
1313
def awaitRight(implicit
1414
ec: ExecutionContext,
1515
timeout: Duration = 5.seconds
1616
): HttpResponse[O] = {
17-
val result = Await.result(
18-
response.tapWith((ctx, o) => ctx.apply().copy(body = o)).run(null)
19-
.bimap(
20-
throwable =>
21-
logger.error(
22-
s"Expected Right, got Left: ${throwable.toString} Error: ${throwable.underlying.getMessage}"
23-
),
24-
res => res
25-
).value,
26-
timeout
27-
).toOption.get
28-
17+
val result = Await
18+
.result(
19+
response.run(identity)
20+
.bimap(
21+
throwable =>
22+
logger.error(
23+
s"Expected Right, got Left: ${throwable.toString} ${throwable.getMessage} ${throwable.httpResponse.statusCode} Error: ${throwable.underlying.getMessage} ${throwable.underlying.getCause}"
24+
),
25+
identity
26+
)
27+
.value,
28+
timeout
29+
)
30+
.toOption
31+
.get
32+
2933
result
3034
}
31-
3235

3336
def awaitLeft(implicit
3437
ec: ExecutionContext,
3538
timeout: Duration = 5.seconds
3639
): HttpResponse[Throwable] = {
37-
val result = Await.result(
38-
response.run(null)
39-
.bimap(
40-
throwable => throwable
41-
,
42-
res => logger.error(
43-
s"Expected Left, got Right: ${res.toString}"
40+
val result = Await
41+
.result(
42+
response.run(identity)
43+
.bimap(
44+
identity,
45+
res => logger.error(s"Expected Left, got Right: ${res.toString}")
4446
)
45-
).value,
46-
timeout
47-
).swap.toOption.get
47+
.value,
48+
timeout
49+
)
50+
.swap
51+
.toOption
52+
.get
4853

4954
result.httpResponse.copy(body = result.underlying)
5055
}
5156
}
5257

53-
54-
5558
}

smithy4play/src/main/scala/de/innfactory/smithy4play/client/core/Smithy4PlayClientCompiler.scala

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package de.innfactory.smithy4play.client.core
22

3-
import de.innfactory.smithy4play.client.{ClientFinishedResponse, OWrapper, RunnableClientRequest}
3+
import de.innfactory.smithy4play.client.{ClientResponse, FinishedClientResponse, RunnableClientResponse}
44
import de.innfactory.smithy4play.codecs.EndpointContentTypes
55
import smithy4s.{Blob, Endpoint, Hints}
66
import smithy4s.capability.MonadThrowLike
7-
import smithy4s.client.{UnaryClientCodecs, UnaryClientEndpoint, UnaryLowLevelClient}
7+
import smithy4s.client.UnaryLowLevelClient
88
import smithy4s.http.{HttpRequest, HttpResponse, HttpUnaryClientCodecs}
9-
import smithy4s.kinds.{Kind1, PolyFunction5}
10-
import smithy4s.server.UnaryServerCodecs
119

1210
import scala.concurrent.ExecutionContext
1311

@@ -27,16 +25,16 @@ object Smithy4PlayClientCompiler {
2725
def apply[Alg[_[_, _, _, _, _]], Client](
2826
service: smithy4s.Service[Alg],
2927
client: Client,
30-
toSmithy4sClient: Client => UnaryLowLevelClient[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]],
31-
codecs: EndpointContentTypes => HttpUnaryClientCodecs.Builder[RunnableClientRequest, HttpRequest[Blob], HttpResponse[Blob]],
28+
toSmithy4sClient: Client => UnaryLowLevelClient[FinishedClientResponse, HttpRequest[Blob], HttpResponse[Blob]],
29+
codecs: EndpointContentTypes => HttpUnaryClientCodecs.Builder[ClientResponse, HttpRequest[Blob], HttpResponse[Blob]],
3230
middleware: Endpoint.Middleware[Client],
3331
isSuccessful: (Hints, HttpResponse[Blob]) => Boolean
34-
)(implicit F: MonadThrowLike[RunnableClientRequest], ec: ExecutionContext): service.FunctorEndpointCompiler[ClientFinishedResponse] = {
32+
)(implicit F: MonadThrowLike[ClientResponse], ec: ExecutionContext): service.FunctorEndpointCompiler[RunnableClientResponse] = {
3533

36-
new service.FunctorEndpointCompiler[ClientFinishedResponse] {
34+
new service.FunctorEndpointCompiler[RunnableClientResponse] {
3735
def apply[I, E, O, SI, SO](
3836
endpoint: service.Endpoint[I, E, O, SI, SO]
39-
): I => ClientFinishedResponse[O] = {
37+
): I => RunnableClientResponse[O] = {
4038

4139
val transformedClient =
4240
middleware.prepare(service)(endpoint).apply(client)
@@ -46,15 +44,13 @@ object Smithy4PlayClientCompiler {
4644
val contentType = resolveContentType(endpoint.hints, service.hints, Seq.empty, None)
4745
val codec = codecs(contentType).build()
4846

49-
val clientEndpoint: I => RunnableClientRequest[O] = UnaryClientEndpoint(
47+
val clientEndpoint: I => RunnableClientResponse[O] = Smithy4PlayClientEndpoint(
5048
adaptedClient,
5149
codec.apply(endpoint.schema),
5250
isSuccessful.curried(endpoint.hints)
5351
)
5452

55-
clientEndpoint.andThen(q => {
56-
q.tapWith((ctx, o) => ctx.apply().copy(body = o))
57-
})
53+
clientEndpoint
5854
}
5955
}
6056
}

0 commit comments

Comments
 (0)