rollbar / rollbar-java

Rollbar for Java and Android
https://docs.rollbar.com/docs/java
MIT License
71 stars 76 forks source link

Use generated class instead of jar manifest to populate notifier version #265

Closed diegov closed 3 years ago

diegov commented 3 years ago

Description of the change

This PR updates the method we use to find the notifier version, to use a generated Java class instead of the jar's manifest attributes.

This fixes the notifier version attribute when users shade rollbar-java, since until now we could end up reporting the version of the user's application as the notifier version instead of our own, due to the fact that our classes had been merged into the user's jar, and only one manifest resource (the user's) remained.

The approach we take now is to generate a static class at build time, with a single method returning the version. This removes the need for any dynamic resource loading, which could fail under relocation since the references are strings.

Automated test coverage isn't great on this change, but I ran a shading + relocation test using the com.github.johnrengelman.shadow gradle plugin, with relocated classes:

shadowJar {
    relocate 'com.rollbar', 'c.rb'
    archiveBaseName.set('shadow')
    archiveClassifier.set('')
    archiveVersion.set('')

    manifest {
        attributes(
                "Implementation-Title": project.name,
                "Implementation-Version": VERSION_NAME
        )
    }
}

I've verified that the classes are actually relocated in the Jar:

 $ jar tf shadow.jar  | grep c/rb/notifier/provider/notifier/VersionHelper
c/rb/notifier/provider/notifier/VersionHelper.class
c/rb/notifier/provider/notifier/VersionHelperResources.class

Using rollbar-java 1.7.6:

{
    "access_token": "token",
    "data": {
        "body": {
            "message": {
                "body": "test"
            }
        },
        "code_version": "0.9.55_APP",
        "language": "java",
        "level": "error",
        "notifier": {
            "name": "rollbar-java",
            "version": "0.9.55_APP"
        },
        "timestamp": 1621971709.178
    }
}

With this fix:

{
    "access_token": "token",
    "data": {
        "body": {
            "message": {
                "body": "test"
            }
        },
        "code_version": "0.9.55_APP",
        "language": "java",
        "level": "error",
        "notifier": {
            "name": "rollbar-java",
            "version": "1.7.7-SNAPSHOT"
        },
        "timestamp": 1621971744.911
    }
}

Type of change

Related issues

[ch85159]

Checklists

Development

Code review

shortcut-integration[bot] commented 3 years ago

This pull request has been linked to Clubhouse Story #85159: [L2][SEV 2] Notifier version shows code_version due to Gradle Shadow plugin.

diegov commented 3 years ago

@basoko requesting re-review since I had accidentally pushed the code review fix to a different branch earlier. Thanks!