-
Notifications
You must be signed in to change notification settings - Fork 667
Add Caffeine plugin as optional #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
43b9a65
01f2090
c7ffa1d
7dd32cc
06059fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> |
| 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); | ||
| } | ||
| } |
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? In which case,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to add judgment, not sure all inputs meet the expectations before this function is processed by upstream. |
||
| String keys = StreamSupport | ||
| .stream(((Iterable<?>) allArguments[0]).spliterator(), false) | ||
| .map(String::valueOf) | ||
| .collect(Collectors.joining(",")); | ||
| Tags.CACHE_KEY.set(span, keys); | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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" | ||
| )); | ||
| } | ||
| } | ||
| } |
| 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()); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.