Skip to content

Commit e648ca2

Browse files
authored
Adapt Armeria's plugins to the latest version 1.22.x (#459)
1 parent 37aa3f8 commit e648ca2

13 files changed

Lines changed: 355 additions & 7 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Release Notes.
1717
* Fix OracleURLParser ignoring actual port when :SID is absent.
1818
* Change gRPC instrumentation point to fix plugin not working for server side.
1919
* Fix servicecomb plugin trace break.
20+
* Adapt Armeria's plugins to the latest version 1.22.x
2021

2122
#### Documentation
2223
* Update docs of Tracing APIs, reorganize the API docs into six parts.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>apm-armeria-plugins</artifactId>
23+
<groupId>org.apache.skywalking</groupId>
24+
<version>8.15.0-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>apm-armeria-1.0.x-plugin</artifactId>
29+
<name>apm-armeria-1.0.x-plugin</name>
30+
31+
<packaging>jar</packaging>
32+
<description>SkyWalking Agent Plugin for Armeria 1.0.x+</description>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.linecorp.armeria</groupId>
37+
<artifactId>armeria</artifactId>
38+
<version>1.0.0</version>
39+
<scope>provided</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.armeria;
19+
20+
import com.linecorp.armeria.client.Clients;
21+
import com.linecorp.armeria.common.HttpMethod;
22+
import com.linecorp.armeria.common.HttpRequest;
23+
import com.linecorp.armeria.common.util.SafeCloseable;
24+
import io.netty.util.AsciiString;
25+
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
26+
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
27+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
28+
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
29+
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
30+
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer;
31+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
32+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
33+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
34+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
35+
36+
import java.lang.reflect.Method;
37+
import java.net.URI;
38+
39+
public abstract class AbstractArmeriaClientInterceptor implements InstanceMethodsAroundInterceptor {
40+
private static final String KEY_SAFE_CLOSEABLE = "SAFE_CLOSEABLE";
41+
42+
protected abstract URI getUri(EnhancedInstance objInst);
43+
44+
protected abstract HttpMethod getHttpMethod(Object[] allArguments);
45+
46+
protected abstract String getPath(Object[] allArguments);
47+
48+
protected abstract HttpRequest getHttpRequest(Object[] allArguments);
49+
50+
@Override
51+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
52+
URI uri = getUri(objInst);
53+
HttpMethod httpMethod = getHttpMethod(allArguments);
54+
String path = getPath(allArguments);
55+
56+
final ContextCarrier contextCarrier = new ContextCarrier();
57+
final String remotePeer = uri.getPort() > 0 ? uri.getHost() + ":" + uri.getPort() : uri.getHost();
58+
59+
final AbstractSpan exitSpan = ContextManager.createExitSpan(path, contextCarrier, remotePeer);
60+
61+
exitSpan.setComponent(ComponentsDefine.ARMERIA);
62+
exitSpan.setLayer(SpanLayer.HTTP);
63+
Tags.HTTP.METHOD.set(exitSpan, httpMethod.name());
64+
65+
ContextManager.getRuntimeContext().put(KEY_SAFE_CLOSEABLE, Clients.withHeaders(builder -> {
66+
for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
67+
item = item.next();
68+
builder.add(AsciiString.of(item.getHeadKey()), item.getHeadValue());
69+
}
70+
}));
71+
}
72+
73+
@Override
74+
public Object afterMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
75+
final Class<?>[] argumentsTypes, final Object ret) {
76+
HttpRequest req = getHttpRequest(allArguments);
77+
if (req != null && ContextManager.isActive()) {
78+
ContextManager.stopSpan();
79+
}
80+
81+
SafeCloseable safeCloseable = ContextManager.getRuntimeContext().get(KEY_SAFE_CLOSEABLE, SafeCloseable.class);
82+
if (safeCloseable != null) {
83+
safeCloseable.close();
84+
}
85+
return ret;
86+
}
87+
88+
@Override
89+
public void handleMethodException(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
90+
final Class<?>[] argumentsTypes, final Throwable t) {
91+
if (ContextManager.isActive()) {
92+
ContextManager.activeSpan().log(t);
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.armeria;
19+
20+
import com.linecorp.armeria.client.UserClient;
21+
import com.linecorp.armeria.common.HttpMethod;
22+
import com.linecorp.armeria.common.HttpRequest;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
24+
25+
import java.net.URI;
26+
27+
public class Armeria100ClientInterceptor extends AbstractArmeriaClientInterceptor {
28+
29+
@Override
30+
protected URI getUri(EnhancedInstance objInst) {
31+
UserClient userClient = (UserClient) objInst;
32+
return userClient.uri();
33+
}
34+
35+
@Override
36+
protected HttpMethod getHttpMethod(Object[] allArguments) {
37+
return (HttpMethod) allArguments[2];
38+
}
39+
40+
@Override
41+
protected String getPath(Object[] allArguments) {
42+
return (String) allArguments[3];
43+
}
44+
45+
@Override
46+
protected HttpRequest getHttpRequest(Object[] allArguments) {
47+
return (HttpRequest) allArguments[6];
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.armeria.define;
19+
20+
import net.bytebuddy.description.method.MethodDescription;
21+
import net.bytebuddy.matcher.ElementMatcher;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
25+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
29+
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
30+
31+
/**
32+
* Instruments Armeria client 1.0.x
33+
*/
34+
public class Armeria100ClientInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
35+
36+
private static final String ENHANCE_CLASS = "com.linecorp.armeria.client.UserClient";
37+
private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.armeria.Armeria100ClientInterceptor";
38+
39+
@Override
40+
protected ClassMatch enhanceClass() {
41+
return byName(ENHANCE_CLASS);
42+
}
43+
44+
@Override
45+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
46+
return new ConstructorInterceptPoint[0];
47+
}
48+
49+
@Override
50+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
51+
return new InstanceMethodsInterceptPoint[] {
52+
new InstanceMethodsInterceptPoint() {
53+
@Override
54+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
55+
return named("execute").and(takesArgument(0, named("com.linecorp.armeria.common.SessionProtocol"))).and(takesArgument(1, named("com.linecorp.armeria.client.endpoint.EndpointGroup"))).and(takesArgument(2, named("com.linecorp.armeria.common.HttpMethod"))).and(takesArgument(3, named("java.lang.String"))).and(takesArgument(4, named("java.lang.String"))).and(takesArgument(5, named("java.lang.String")));
56+
}
57+
58+
@Override
59+
public String getMethodsInterceptor() {
60+
return INTERCEPTOR_CLASS;
61+
}
62+
63+
@Override
64+
public boolean isOverrideArgs() {
65+
return false;
66+
}
67+
}
68+
};
69+
}
70+
71+
@Override
72+
protected String[] witnessClasses() {
73+
return new String[]{"com.linecorp.armeria.common.AbstractHttpHeadersBuilder"};
74+
}
75+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
armeria-100=org.apache.skywalking.apm.plugin.armeria.define.Armeria100ClientInstrumentation
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<parent>
21+
<artifactId>apm-sdk-plugin</artifactId>
22+
<groupId>org.apache.skywalking</groupId>
23+
<version>8.15.0-SNAPSHOT</version>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
<packaging>pom</packaging>
27+
<artifactId>apm-armeria-plugins</artifactId>
28+
<name>apm-armeria-plugins</name>
29+
30+
<modules>
31+
<module>apm-armeria-1.0.x-plugin</module>
32+
</modules>
33+
34+
<properties>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<sdk.plugin.related.dir>/..</sdk.plugin.related.dir>
37+
</properties>
38+
39+
</project>

apm-sniffer/apm-sdk-plugin/armeria-0.85.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/armeria/Armeria085ServerInterceptor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
package org.apache.skywalking.apm.plugin.armeria;
1919

20-
import com.linecorp.armeria.common.DefaultHttpRequest;
2120
import com.linecorp.armeria.common.HttpHeaders;
21+
import com.linecorp.armeria.common.HttpRequest;
2222
import io.netty.util.AsciiString;
23-
import java.lang.reflect.Method;
2423
import org.apache.skywalking.apm.agent.core.context.CarrierItem;
2524
import org.apache.skywalking.apm.agent.core.context.ContextCarrier;
2625
import org.apache.skywalking.apm.agent.core.context.ContextManager;
@@ -32,13 +31,15 @@
3231
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
3332
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
3433

34+
import java.lang.reflect.Method;
35+
3536
@SuppressWarnings("unused") // actually used
3637
public class Armeria085ServerInterceptor implements InstanceMethodsAroundInterceptor {
3738
@Override
3839
public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments,
3940
final Class<?>[] argumentsTypes, final MethodInterceptResult result) {
4041

41-
DefaultHttpRequest httpRequest = (DefaultHttpRequest) allArguments[1];
42+
HttpRequest httpRequest = (HttpRequest) allArguments[1];
4243
HttpHeaders headers = httpRequest.headers();
4344

4445
ContextCarrier carrier = new ContextCarrier();

apm-sniffer/apm-sdk-plugin/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<module>nats-2.14.x-2.15.x-plugin</module>
128128
<module>jedis-plugins</module>
129129
<module>impala-jdbc-2.6.x-plugin</module>
130+
<module>apm-armeria-plugins</module>
130131
</modules>
131132
<packaging>pom</packaging>
132133

docs/en/setup/service-agent/java-agent/Plugin-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- armeria-085
55
- armeria-086
66
- armeria-098
7+
- armeria-100
78
- async-http-client-2.x
89
- avro-1.x
910
- brpc-java

0 commit comments

Comments
 (0)