pinpoint-apm / pinpoint

APM, (Application Performance Management) tool for large-scale distributed systems.
https://pinpoint-apm.gitbook.io/
Apache License 2.0
13.43k stars 3.76k forks source link

Add version identification API to plugin #7078

Open emeroad opened 4 years ago

emeroad commented 4 years ago

https://github.com/naver/pinpoint/pull/7077

I plan to add a feature to identify the version in the plugin. This feature can separate plugins for each version of lib. It has the advantage of making compilation and maintenance easy. e.g) mongo-db-plugin-2.x.jar, mongo-db-plugin-3.x.jar,

// pseudocode
public class MongoPlugin implements ProfilerPlugin, TransformTemplateAware {
    context.addVerionMatcher(new VersionMatcher() {
            @Override
            public boolean isMatch(JarFile file) {
                // Manifest base matcher
                Manifest manifest = file.getManifest();
                Attributes attributes = manifest.getAttributes("PATH");
                String version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
                if (version.endsWith("2.0.0")) {
                    //~~
                    return true;
                }

                // File name base matcher
                if (file.getName().endsWith("2.0.0")) {
                    //~~
                    return true;
                }
                return true;
            }
        });

}

Jarfile can be obtained from ProtectionDomain of Transform API.

// pseudocode
@Override
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
     URL location = protectionDomain.getCodeSource().getLocation();
        if (!location.getProtocol("jar")) {
            //~~
        }
        JarFile jarFile = new JarFile(location.getFile());
        if (versionMatcher.match(jarFile)) {
            this.delegate.transform(...);
        }
}
emeroad commented 4 years ago

It would be nice to have a version matching policy similar to maven version scheme.

https://maven.apache.org/pom.html#dependency-version-requirement-specification

https://github.com/apache/maven-resolver/blob/master/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionRange.java

// pseudocode
public class MongoPlugin implements ProfilerPlugin, TransformTemplateAware {
    context.addVerionMatcher(new MavenManifestVersionMatcher("[2.0.0)"));

}
emeroad commented 4 years ago

Dependency Mediation and Conflict Resolution https://cwiki.apache.org/confluence/display/MAVENOLD/Dependency+Mediation+and+Conflict+Resolution#DependencyMediationandConflictResolution-DependencyVersionRanges

Version Range Specification https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html