Skip to content

Commit 28fcf17

Browse files
authored
Support grizzly web server trace (apache#528)
1 parent 4eb07e3 commit 28fcf17

File tree

26 files changed

+1337
-147
lines changed

26 files changed

+1337
-147
lines changed

.github/workflows/plugins-jdk17-test.0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ jobs:
6060
- jersey-3.x-scenario
6161
- jetty-thread-pool-scenario
6262
- jetty-11.x-thread-pool-scenario
63+
- grizzly-2.3.x-4.x-scenario
6364
steps:
6465
- uses: actions/checkout@v2
6566
with:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Release Notes.
1515
* [Chore] Exclude proto files in the generated jar
1616
* Fix Jedis-2.x plugin can not get host info in jedis 3.3.x+
1717
* Change the classloader to locate the agent path in AgentPackagePath, from `SystemClassLoader` to AgentPackagePath's loader.
18+
* Support Grizzly Trace
1819

1920
#### Documentation
2021

apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,6 @@ public class ComponentsDefine {
235235

236236
public static final OfficialComponent JERSEY = new OfficialComponent(146, "Jersey");
237237

238+
public static final OfficialComponent GRIZZLY = new OfficialComponent(147, "Grizzly");
239+
238240
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<groupId>org.apache.skywalking</groupId>
24+
<artifactId>apm-sdk-plugin</artifactId>
25+
<version>8.16.0-SNAPSHOT</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>apm-grizzly-2.x-plugin</artifactId>
30+
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<grizzly.version>2.4.0</grizzly.version>
35+
</properties>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.glassfish.grizzly</groupId>
39+
<artifactId>grizzly-http-server</artifactId>
40+
<version>${grizzly.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.v2;
20+
21+
import java.lang.reflect.Method;
22+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
23+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
24+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
25+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
26+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
27+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
29+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2;
30+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext;
31+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
32+
import org.glassfish.grizzly.http.server.Request;
33+
34+
public class HttpHandlerInterceptor implements InstanceMethodsAroundInterceptorV2 {
35+
36+
public static final String GRIZZLY_CONTEXT = "SW_GRIZZLY_CONTEXT";
37+
38+
@Override
39+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInvocationContext context) throws Throwable {
40+
// entry span
41+
Request request = (Request) allArguments[0];
42+
request.getHeaderNames();
43+
final ContextCarrier carrier = new ContextCarrier();
44+
CarrierItem items = carrier.items();
45+
while (items.hasNext()) {
46+
items = items.next();
47+
items.setHeadValue(request.getHeader(items.getHeadKey()));
48+
}
49+
final AbstractSpan span = ContextManager.createEntrySpan(request.getMethod().getMethodString() + ":" + request.getRequestURI(), carrier);
50+
Tags.URL.set(span, request.getRequestURL().toString());
51+
Tags.HTTP.METHOD.set(span, request.getMethod().getMethodString());
52+
span.setComponent(ComponentsDefine.GRIZZLY);
53+
SpanLayer.asHttp(span);
54+
span.prepareForAsync();
55+
Object[] grizzlyContext = new Object[]{span, ContextManager.capture()};
56+
request.setAttribute(GRIZZLY_CONTEXT, grizzlyContext);
57+
}
58+
59+
@Override
60+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret, MethodInvocationContext context) throws Throwable {
61+
ContextManager.stopSpan();
62+
return ret;
63+
}
64+
65+
@Override
66+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) {
67+
ContextManager.activeSpan().log(t);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.v2;
20+
21+
import java.lang.reflect.Method;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
24+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
25+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
26+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
27+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2;
28+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext;
29+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
30+
import org.glassfish.grizzly.http.server.Request;
31+
import org.glassfish.grizzly.http.server.Response;
32+
33+
import static org.apache.skywalking.apm.plugin.grizzly.v2.HttpHandlerInterceptor.GRIZZLY_CONTEXT;
34+
35+
public class HttpServiceInterceptor implements InstanceMethodsAroundInterceptorV2 {
36+
37+
@Override
38+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInvocationContext context) throws Throwable {
39+
Request request = (Request) allArguments[0];
40+
Object[] grizzlyContext = (Object[]) request.getAttribute(GRIZZLY_CONTEXT);
41+
context.setContext(grizzlyContext);
42+
ContextSnapshot contextSnapshot = (ContextSnapshot) grizzlyContext[1];
43+
AbstractSpan span = ContextManager.createLocalSpan("GrizzlyRunService");
44+
span.setComponent(ComponentsDefine.GRIZZLY);
45+
ContextManager.continued(contextSnapshot);
46+
}
47+
48+
@Override
49+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret, MethodInvocationContext context) throws Throwable {
50+
Object[] grizzlyContext = (Object[]) context.getContext();
51+
AbstractSpan abstractSpan = (AbstractSpan) grizzlyContext[0];
52+
ContextManager.stopSpan();
53+
Response response = (Response) allArguments[1];
54+
Tags.HTTP_RESPONSE_STATUS_CODE.set(abstractSpan, response.getStatus());
55+
if (response.getStatus() >= 400) {
56+
abstractSpan.errorOccurred();
57+
}
58+
abstractSpan.asyncFinish();
59+
return ret;
60+
}
61+
62+
@Override
63+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) {
64+
ContextManager.activeSpan().log(t);
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.grizzly.v2.define;
19+
20+
import java.util.Collections;
21+
import java.util.List;
22+
import org.apache.skywalking.apm.agent.core.plugin.WitnessMethod;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.ClassInstanceMethodsEnhancePluginDefineV2;
24+
25+
import static net.bytebuddy.matcher.ElementMatchers.named;
26+
27+
public abstract class AbstractWitnessInstrumentation extends ClassInstanceMethodsEnhancePluginDefineV2 {
28+
29+
@Override
30+
protected List<WitnessMethod> witnessMethods() {
31+
return Collections.singletonList(new WitnessMethod(
32+
"org.glassfish.grizzly.http.server.HttpHandler",
33+
named("runService")
34+
));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.v2.define;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.v2.InstanceMethodsInterceptV2Point;
25+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
29+
30+
public class HttpHandlerInstrumentation extends AbstractWitnessInstrumentation {
31+
32+
private static final String ENHANCE_CLASS = "org.glassfish.grizzly.http.server.HttpHandler";
33+
private static final String DO_HANDLER_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.grizzly.v2.HttpHandlerInterceptor";
34+
35+
@Override
36+
protected ClassMatch enhanceClass() {
37+
return byName(ENHANCE_CLASS);
38+
}
39+
40+
@Override
41+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
42+
return new ConstructorInterceptPoint[0];
43+
}
44+
45+
@Override
46+
public InstanceMethodsInterceptV2Point[] getInstanceMethodsInterceptV2Points() {
47+
return new InstanceMethodsInterceptV2Point[]{
48+
new InstanceMethodsInterceptV2Point() {
49+
@Override
50+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
51+
return named("doHandle");
52+
}
53+
54+
@Override
55+
public String getMethodsInterceptorV2() {
56+
return DO_HANDLER_METHOD_INTERCEPTOR;
57+
}
58+
59+
@Override
60+
public boolean isOverrideArgs() {
61+
return false;
62+
}
63+
}
64+
};
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.v2.define;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.v2.InstanceMethodsInterceptV2Point;
25+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch;
29+
30+
public class HttpServiceInstrumentation extends AbstractWitnessInstrumentation {
31+
32+
private static final String SERVICE_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.grizzly.v2.HttpServiceInterceptor";
33+
34+
@Override
35+
protected ClassMatch enhanceClass() {
36+
return byHierarchyMatch("org.glassfish.grizzly.http.server.HttpHandler");
37+
}
38+
39+
@Override
40+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
41+
return new ConstructorInterceptPoint[0];
42+
}
43+
44+
@Override
45+
public InstanceMethodsInterceptV2Point[] getInstanceMethodsInterceptV2Points() {
46+
return new InstanceMethodsInterceptV2Point[]{
47+
new InstanceMethodsInterceptV2Point() {
48+
@Override
49+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
50+
return named("service");
51+
}
52+
53+
@Override
54+
public String getMethodsInterceptorV2() {
55+
return SERVICE_METHOD_INTERCEPTOR;
56+
}
57+
58+
@Override
59+
public boolean isOverrideArgs() {
60+
return false;
61+
}
62+
}
63+
};
64+
}
65+
66+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
grizzly-2.3.x-4.x=org.apache.skywalking.apm.plugin.grizzly.v2.define.HttpHandlerInstrumentation
18+
grizzly-2.3.x-4.x=org.apache.skywalking.apm.plugin.grizzly.v2.define.HttpServiceInstrumentation
19+

0 commit comments

Comments
 (0)