Skip to content

Commit 93b118a

Browse files
committed
WIP MORE WIP WIP WIP
1 parent c9227d7 commit 93b118a

File tree

11 files changed

+382
-381
lines changed

11 files changed

+382
-381
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,62 @@
11
package kamon.armeria.instrumentation;
22

33
import com.linecorp.armeria.common.HttpRequest;
4+
import com.linecorp.armeria.server.RequestConverter;
45
import com.linecorp.armeria.server.ServiceRequestContext;
56
import io.netty.util.AttributeKey;
67
import kamon.Kamon;
78
import kamon.context.Storage;
9+
import kamon.instrumentation.http.HttpMessage;
10+
import kamon.instrumentation.http.HttpServerInstrumentation;
811
import kamon.trace.Span;
912
import kamon.trace.SpanPropagation.B3;
1013
import kanela.agent.libs.net.bytebuddy.asm.Advice;
14+
import scala.Int;
15+
import scala.Option;
1116

12-
// why is this good?
13-
// i mean, the serve method could still spawn some shit off
14-
// and go to another thread?
15-
// ok, so this is about context propagation, right?
16-
// when I store the context, how do other threads know about that?
17-
// how does it FORWARD THAT INFORMATION to other threads?
18-
// the information that yeah, there's a current trace in progress
19-
// and you're a part of it
17+
import java.util.Map;
2018

2119
public class ArmeriaHttpServiceInstrumentation {
2220
@Advice.OnMethodEnter()
23-
private static void enter(
24-
@Advice.Local("scope") Storage.Scope scope,
25-
@Advice.Local("span") Span span,
26-
@Advice.This Object service,
21+
private static Storage.Scope enter(
2722
@Advice.Argument(0) ServiceRequestContext context,
2823
@Advice.Argument(1) HttpRequest request) {
29-
// what can I do with the context?
30-
// What can i extract out of it?
3124

25+
// make sure that this _is_ truly available here
26+
long startTime = context.log().partial().requestStartTimeNanos();
3227

33-
// I add a header here with the traceID right?
34-
// I mean not here, but at the beginning of the pipeline
35-
// is toString needed here?
36-
// the the elastic-armeria shit without is too
37-
// context.log().context().setAttr(AttributeKey.newInstance("Kamon.Context"), Kamon.currentContext());
38-
// System.out.println("Context:");
39-
// System.out.println("Context:");
40-
// System.out.println(context);
41-
// System.out.println("Request");
42-
// System.out.println("Request");
43-
// System.out.println(request);
44-
// System.out.println("Service??");
45-
// System.out.println("Service??");
46-
// System.out.println(service);
47-
// System.out.println(service.getClass().getEnclosingClass().getSimpleName());
48-
// System.out.println("Context again");
49-
// System.out.println("Context again");
50-
// System.out.println(context.log().context());
28+
// figure out how to get the host and the port
29+
// also, this handler will need to end this span at the end of the pipeline
30+
// meaning, we need to propagate it there somehow
31+
HttpServerInstrumentation httpServerInstrumentation = HttpServerInstrumentation.from(Kamon.config().getConfig("kamon.instrumentation.armeria.http-server"),
32+
"armeria.http.server", "0.0.0.0", 1234);
33+
HttpServerInstrumentation.RequestHandler handler = httpServerInstrumentation
34+
.createHandler(RequestConverter.toRequest(request, "whatever", 1234), true);
35+
// context.log().whenComplete().thenAccept(requestLog -> {
36+
// // shit
37+
// handler.responseSent();
38+
// // this takes a time
39+
// // Kamon.Clock to convert it
40+
// // but which time to use?
41+
//// handler.span().finish();
42+
// });
43+
// add a decorator
44+
// which has acces to this same context
45+
// context.attr, modify response
46+
// context.respo
47+
context.setAttr(AttributeKey.newInstance("myHandler"), handler);
5148

52-
// this is wrong
53-
// i should be starting a span here
54-
// what tags do I add to it?
55-
span = Kamon.serverSpanBuilder("some.smart.operation.name", "armeria???").start();
56-
// parentSpan?
57-
// wrong context to store?
58-
scope = Kamon.storeContext(Kamon.currentContext());
49+
// figure out a nice name
50+
handler.span().name("CoolName").takeSamplingDecision();
51+
return Kamon.storeContext(handler.context());
5952
}
6053

6154
@Advice.OnMethodExit()
62-
private static void exit(@Advice.Local("scope") Storage.Scope scope,
63-
@Advice.Local("span") Span span,
64-
@Advice.This Object service,
65-
@Advice.Argument(0) ServiceRequestContext context,
66-
@Advice.Argument(1) HttpRequest request) {
67-
// how do i tell here if the thing finished correctly
68-
// or if I should fail the span?
69-
System.out.println("Context:");
70-
System.out.println("Context:");
71-
System.out.println(context);
72-
System.out.println("Request");
73-
System.out.println("Request");
74-
System.out.println(request);
75-
System.out.println("Service??");
76-
System.out.println("Service??");
77-
System.out.println(service);
78-
System.out.println(service.getClass().getEnclosingClass().getSimpleName());
79-
span.finish();
55+
// Advice.Enter will bind the return from the enter to the exit :O
56+
// :O :O :O :O :O
57+
private static void exit(@Advice.Enter Storage.Scope scope) {
8058
scope.close();
8159
}
8260
}
61+
62+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.linecorp.armeria.server
2+
3+
import com.linecorp.armeria.common.HttpRequest
4+
import kamon.instrumentation.http.HttpMessage
5+
6+
import scala.collection.JavaConverters.iterableAsScalaIterableConverter
7+
8+
object RequestConverter {
9+
def toRequest(request: HttpRequest, serverHost: String, serverPort: Int): HttpMessage.Request = new HttpMessage.Request {
10+
11+
override def url: String = request.uri().toString
12+
13+
override def path: String = request.path()
14+
15+
override def method: String = request.method().name()
16+
17+
override def host: String = serverHost
18+
19+
override def port: Int = serverPort
20+
21+
override def read(header: String): Option[String] =
22+
Option(request.headers().get(header))
23+
24+
override def readAll(): Map[String, String] =
25+
request.headers().asScala.map(e => e.getKey.toString() -> e.getValue).toMap
26+
}
27+
}

instrumentation/kamon-armeria/src/main/resources/reference.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ kamon.instrumentation.armeria {
134134
# - default: Uses the set default operation name
135135
# - method: Uses the request HTTP method as the operation name.
136136
#
137-
name-generator = "kamon.armeria.instrumentation.server.KamonArmeriaOperationNameGenerator"
137+
# name-generator = "kamon.armeria.instrumentation.server.KamonArmeriaOperationNameGenerator"
138138

139139
# Provides custom mappings from HTTP paths into operation names. Meant to be used in cases where the bytecode
140140
# instrumentation is not able to provide a sensible operation name that is free of high cardinality values.

instrumentation/kamon-armeria/src/main/scala/kamon/armeria/instrumentation/server/ArmeriaHttpServerInstrumentation.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class ArmeriaHttpServerInstrumentation extends InstrumentationBuilder {
3030
// onSubTypesOf("io.netty.channel.Channel")
3131
// .mixin(classOf[HasRequestProcessingContextMixin])
3232
//
33-
// onType("com.linecorp.armeria.server.HttpServerPipelineConfigurator")
34-
// .bridge(classOf[HttpPipelineConfiguratorInternalState])
35-
// .advise(method("configureHttp"), classOf[ConfigureMethodAdvisor])
33+
onType("com.linecorp.armeria.server.HttpServerPipelineConfigurator")
34+
.bridge(classOf[HttpPipelineConfiguratorInternalState])
35+
.advise(method("configureHttp"), classOf[ConfigureMethodAdvisor])
3636
//
3737
// onType("com.linecorp.armeria.server.FallbackService")
3838
// .advise(method("serve"), classOf[ServeMethodAdvisor])

instrumentation/kamon-armeria/src/main/scala/kamon/armeria/instrumentation/server/handlers/ArmeriaHttpServerResponseHandler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class ArmeriaHttpServerResponseHandler(serverInstrumentation: HttpServerIn
2828
else {
2929
val response = msg.asInstanceOf[HttpResponse]
3030
val processingContext = ctx.channel().asInstanceOf[HasRequestProcessingContext].getRequestProcessingContext
31+
response.
3132

3233
/**
3334
* processingContext.requestHandler.span.operationName() will be empty if an HttpStatusException is thrown

instrumentation/kamon-armeria/src/test/resources/application.conf

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ kamon.instrumentation.armeria {
134134
# - default: Uses the set default operation name
135135
# - method: Uses the request HTTP method as the operation name.
136136
#
137-
name-generator = "kamon.armeria.instrumentation.server.KamonArmeriaOperationNameGenerator"
137+
# name-generator = "kamon.armeria.instrumentation.server.KamonArmeriaOperationNameGenerator"
138138

139139
# Provides custom mappings from HTTP paths into operation names. Meant to be used in cases where the bytecode
140140
# instrumentation is not able to provide a sensible operation name that is free of high cardinality values.
@@ -153,7 +153,7 @@ kamon.instrumentation.armeria {
153153
# The patterns are expressed as globs and the operation names are free form.
154154
#
155155
mappings {
156-
"/dummy-resources/*/other-resources/*" = "dummy-resources/{}/other-resources/{}"
156+
# "/dummy-resources/*/other-resources/*" = "dummy-resources/{}/other-resources/{}"
157157
}
158158
}
159159
}
@@ -232,24 +232,10 @@ kamon.instrumentation.armeria {
232232
# - hostname: Uses the request Host as the operation name.
233233
# - method: Uses the request HTTP method as the operation name.
234234
#
235-
name-generator = "kamon.armeria.instrumentation.client.KamonArmeriaOperationNameGenerator"
235+
# name-generator = "kamon.armeria.instrumentation.client.KamonArmeriaOperationNameGenerator"
236236
}
237237
}
238238
}
239239

240240
}
241241

242-
kanela {
243-
show-banner = false
244-
modules {
245-
armeria {
246-
name = "Armeria instrumentation"
247-
stoppable = true
248-
instrumentations = [
249-
"kamon.armeria.instrumentation.server.ArmeriaHttpServerInstrumentation",
250-
"kamon.armeria.instrumentation.client.ArmeriaHttpClientInstrumentation"
251-
]
252-
within = [ "io.netty..*", "com.linecorp.armeria..*"]
253-
}
254-
}
255-
}

0 commit comments

Comments
 (0)