-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Caffeine plugin as optional (#743)
- Loading branch information
1 parent
b0d5bc1
commit d53f04b
Showing
31 changed files
with
1,158 additions
and
15 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Licensed to the Apache Software Foundation (ASF) under one or more | ||
~ contributor license agreements. See the NOTICE file distributed with | ||
~ this work for additional information regarding copyright ownership. | ||
~ The ASF licenses this file to You under the Apache License, Version 2.0 | ||
~ (the "License"); you may not use this file except in compliance with | ||
~ the License. You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~ | ||
--> | ||
|
||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.skywalking</groupId> | ||
<artifactId>optional-plugins</artifactId> | ||
<version>9.4.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>apm-caffeine-3.x-plugin</artifactId> | ||
<packaging>jar</packaging> | ||
<name>caffeine-3.x-plugin</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<caffeine.version>3.1.8</caffeine.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.ben-manes.caffeine</groupId> | ||
<artifactId>caffeine</artifactId> | ||
<version>${caffeine.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
71 changes: 71 additions & 0 deletions
71
...c/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/AbstractCaffeineInterceptor.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,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.lang.reflect.Method; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
|
||
import static org.apache.skywalking.apm.plugin.caffeine.v3.CaffeineOperationTransform.transformOperation; | ||
|
||
abstract public class AbstractCaffeineInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
||
protected AbstractSpan generateSpanInfo(String methodName) { | ||
AbstractSpan span = ContextManager.createLocalSpan("Caffeine/" + methodName); | ||
span.setComponent(ComponentsDefine.CAFFEINE); | ||
Tags.CACHE_TYPE.set(span, ComponentsDefine.CAFFEINE.getName()); | ||
Tags.CACHE_CMD.set(span, methodName); | ||
transformOperation(methodName).ifPresent(op -> Tags.CACHE_OP.set(span, op)); | ||
SpanLayer.asCache(span); | ||
return span; | ||
} | ||
|
||
@Override | ||
public void beforeMethod(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final MethodInterceptResult result) throws Throwable { | ||
} | ||
|
||
@Override | ||
public Object afterMethod(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final Object ret) throws Throwable { | ||
ContextManager.stopSpan(); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final Throwable t) { | ||
ContextManager.activeSpan().log(t); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...c/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/CaffeineIterableInterceptor.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,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
public class CaffeineIterableInterceptor extends AbstractCaffeineInterceptor { | ||
|
||
@Override | ||
public void beforeMethod(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final MethodInterceptResult result) throws Throwable { | ||
AbstractSpan span = generateSpanInfo(method.getName()); | ||
if (allArguments != null && allArguments.length > 0) { | ||
String keys = StreamSupport | ||
.stream(((Iterable<?>) allArguments[0]).spliterator(), false) | ||
.map(String::valueOf) | ||
.collect(Collectors.joining(",")); | ||
Tags.CACHE_KEY.set(span, keys); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...in/src/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/CaffeineMapInterceptor.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,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
public class CaffeineMapInterceptor extends AbstractCaffeineInterceptor { | ||
|
||
@Override | ||
public void beforeMethod(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final MethodInterceptResult result) throws Throwable { | ||
AbstractSpan span = generateSpanInfo(method.getName()); | ||
if (allArguments != null && allArguments.length > 0) { | ||
String keys = ((Map<?, ?>) allArguments[0]) | ||
.keySet().stream().map(String::valueOf) | ||
.collect(Collectors.joining(",")); | ||
Tags.CACHE_KEY.set(span, keys); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rc/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/CaffeineOperationTransform.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,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.util.Optional; | ||
|
||
public class CaffeineOperationTransform { | ||
|
||
public static Optional<String> transformOperation(String cmd) { | ||
if (CaffeinePluginConfig.Plugin.Caffeine.OPERATION_MAPPING_READ.contains(cmd)) { | ||
return Optional.of("read"); | ||
} | ||
if (CaffeinePluginConfig.Plugin.Caffeine.OPERATION_MAPPING_WRITE.contains(cmd)) { | ||
return Optional.of("write"); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ugin/src/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/CaffeinePluginConfig.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,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.apache.skywalking.apm.agent.core.boot.PluginConfig; | ||
|
||
/** | ||
* Operation represent a cache span is "write" or "read" action , and "op"(operation) is tagged with key "cache.op" | ||
* usually This config term define which command should be converted to write Operation . | ||
* | ||
* @see org.apache.skywalking.apm.agent.core.context.tag.Tags#CACHE_OP | ||
* @see CaffeineOperationTransform#transformOperation(String) | ||
*/ | ||
public class CaffeinePluginConfig { | ||
public static class Plugin { | ||
@PluginConfig(root = CaffeinePluginConfig.class) | ||
public static class Caffeine { | ||
public static Set<String> OPERATION_MAPPING_WRITE = new HashSet<>(Arrays.asList( | ||
"put", | ||
"putAll", | ||
"remove", | ||
"clear" | ||
)); | ||
public static Set<String> OPERATION_MAPPING_READ = new HashSet<>(Arrays.asList( | ||
"getIfPresent", | ||
"getAllPresent", | ||
"computeIfAbsent" | ||
)); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...src/main/java/org/apache/skywalking/apm/plugin/caffeine/v3/CaffeineStringInterceptor.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,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.caffeine.v3; | ||
|
||
import java.lang.reflect.Method; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
|
||
public class CaffeineStringInterceptor extends AbstractCaffeineInterceptor { | ||
|
||
@Override | ||
public void beforeMethod(final EnhancedInstance objInst, | ||
final Method method, | ||
final Object[] allArguments, | ||
final Class<?>[] argumentsTypes, | ||
final MethodInterceptResult result) throws Throwable { | ||
AbstractSpan span = generateSpanInfo(method.getName()); | ||
if (allArguments != null && allArguments.length > 0 && allArguments[0] instanceof String) { | ||
Tags.CACHE_KEY.set(span, allArguments[0].toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.