Repository navigation
feat(maven-plugin): add posthog-maven-plugin to upload jvm sources with hog #1011
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
Open
ablaszkiewicz
wants to merge
15
commits into
main
Choose a base branch
from
ab/feat/maven-plugin-upload-sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,593
−1
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cc812b2
feat(maven-plugin): add posthog-maven-plugin to upload jvm sources
ablaszkiewicz f1b79e6
fix(maven-plugin): upload generated and non-utf-8 sources, avoid cmd
ablaszkiewicz 13ffa60
fix(maven-plugin): allow parentheses in arguments that run through cmd
ablaszkiewicz af0a6fc
fix(maven-plugin): skip a windows npm shim whose binary is missing
ablaszkiewicz e7ab104
docs(maven-plugin): describe the release id in posthog-meta.properties
ablaszkiewicz 56d12b4
fix(maven-plugin): upload only from ci, and never fail an optional up…
ablaszkiewicz f82d858
fix(maven-plugin): address review of the source upload goal
ablaszkiewicz d4cec74
Merge remote-tracking branch 'origin/main' into ab/feat/maven-plugin-…
rafaeelaudibert 7c30ced
feat(maven-plugin): upload jvm sources with hog
rafaeelaudibert fcfe6ab
refactor(maven-plugin): name the hog runner after hog
rafaeelaudibert 29cd104
docs(maven-plugin): name hog the same way everywhere
rafaeelaudibert 91aac0e
refactor(maven-plugin): simplify the hog runner and its tests
rafaeelaudibert 23f6cec
feat(maven-plugin): require a hog new enough for jvm uploads
rafaeelaudibert bf46023
fix(maven-plugin): leave out of the utf-8 copies what hog would skip
rafaeelaudibert 211887e
fix(maven-plugin): keep uploading from apps that other modules depend on
ablaszkiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "posthog-maven-plugin": minor | ||
| --- | ||
|
|
||
| Add `posthog-maven-plugin` with an `upload-sources` goal. It runs `hog error-tracking process --kind jvm` to upload the project's sources and writes the id they were uploaded under and the id of the build's release into `posthog-meta.properties`, so error tracking shows source code on the frames of exceptions `posthog-server` captures, and reports the build's release. By default only a CI build of an application module uploads, and a missing or outdated hog, missing credentials or a failed upload only logs a warning. Set `requireUpload` to upload from every build and fail it when the upload can't run. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,96 @@ | ||
| # posthog-maven-plugin | ||
|
|
||
| Uploads your application's Java and Kotlin sources to PostHog, so error tracking shows the source code around each frame of the exceptions your app captures. | ||
|
|
||
| On each CI build, the `upload-sources` goal runs [hog](https://github.com/PostHog/posthog/tree/master/hog), PostHog's command line, as `hog error-tracking process --kind jvm`. hog bundles the sources, uploads them under an id derived from their content, and writes that id and the id of the build's release into `posthog-meta.properties` in the classes directory, so it is packaged with your classes. At startup, [`posthog-server`](../posthog-server) reads the file. It sends the first id on every exception frame, and PostHog looks up the uploaded sources by it. It sends the release id with every exception, and PostHog reports that release. | ||
|
|
||
| A build of unchanged code reuses the existing upload and sends no source files, also in a new release. Changed code gets a new id and a new upload. A generated source that holds the time of the build changes on every build, so turn that time off, as `-Amapstruct.suppressGeneratorTimestamp=true` does for MapStruct mappers. The goal rewrites or deletes the file on every build, so it always matches the code that was compiled and the release it belongs to. | ||
|
|
||
| ## Requirements | ||
|
|
||
| - [hog](https://github.com/PostHog/posthog/tree/master/hog) 1.1.0 or newer. Before the upload, the plugin runs `hog --version`, and skips the upload with a warning when it finds posthog-cli or an older hog. Install hog with `curl -fsSL https://releases.posthog.com/hog/install.sh | sh`, `brew install posthog/tap/hog` or `npm install -g hog`, or on Windows with `irm https://releases.posthog.com/hog/install.ps1 | iex`. Update an older hog with `hog upgrade`. | ||
| - `posthog-server` with `posthog-meta.properties` support, and no `releaseIdentifier` set in code (an explicit value wins over the file). | ||
| - PostHog credentials with error tracking write access, for the upload. See [Credentials](#credentials). | ||
|
|
||
| ## Usage | ||
|
|
||
| ```xml | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.posthog</groupId> | ||
| <artifactId>posthog-maven-plugin</artifactId> | ||
| <version>0.1.0</version> | ||
| <executions> | ||
| <execution> | ||
| <goals> | ||
| <goal>upload-sources</goal> | ||
| </goals> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| ``` | ||
|
|
||
| The goal binds to the `process-classes` phase. It runs after compilation, so the upload includes the sources that annotation processors such as MapStruct generate, and `mvn package` uploads before it packages. A command that stops before that phase uploads nothing: run `mvn package jib:build` or `mvn process-classes jib:build`, not `mvn compile jib:build`. | ||
|
|
||
| hog reads sources as UTF-8, and shows a replacement character for each byte that isn't valid UTF-8. When `project.build.sourceEncoding` or the compiler's `encoding` names another encoding, the plugin uploads UTF-8 copies of the Java sources from `target/posthog/utf8-sources`, so their non-ASCII characters show. A file that is valid UTF-8 already, such as one from another module, is copied as it is. | ||
|
|
||
| ### Releases | ||
|
|
||
| Each build belongs to a release, and its exceptions are attributed to it. The release name defaults to the artifact id, and the version defaults to the current git commit: hog reads it from the repository, or from the CI environment on GitHub Actions. So every deployed commit is its own release, even when the project version stays at `0.0.1-SNAPSHOT`. A build with no git commit, such as a Docker build that copies only the sources, has no release unless you set `releaseVersion`. Its exceptions still show source code. | ||
|
|
||
| Every build that uploads creates its release, also a build of a pull request. To keep your releases to what you deploy, pass `-Dposthog.skip=true` in CI jobs that don't deploy. | ||
|
|
||
| ### Credentials | ||
|
|
||
| hog reads them from the environment, from a dotenv file, from the CI job's OIDC token, or from `hog auth login`. See [hog's credentials](https://github.com/PostHog/posthog/tree/master/hog#credentials) for the OIDC setup, which needs no stored key. | ||
|
|
||
| - `HOG_HOST`: your PostHog app host, for example `https://us.posthog.com` | ||
| - `HOG_PROJECT_ID`: your project id | ||
| - `HOG_PERSONAL_API_KEY`: a personal API key with error tracking write access | ||
|
|
||
| To use a dotenv file, set `<dotenvFile>.env</dotenvFile>` or pass `-Dposthog.dotenvFile=.env`. The plugin passes it to hog as `HOG_DOTENV_FILE`. A relative path resolves against the module directory, so in a multi-module build set `<dotenvFile>${maven.multiModuleProjectDirectory}/.env</dotenvFile>` for a file in the top directory. | ||
|
|
||
| ### When it uploads | ||
|
|
||
| By default, only a CI build uploads. A build counts as CI when the `CI` environment variable is set, as GitHub Actions, GitLab CI, CircleCI, Bitbucket Pipelines and most other services set it, or when it runs on Jenkins, Azure Pipelines, TeamCity or AWS CodeBuild. A local build skips the upload, so it doesn't upload work in progress or need the network. An offline build (`mvn -o`) skips it too. | ||
|
|
||
| In a multi-module build, a module is a library when another module of the build depends on it and runs the goal too, and the library skips the upload. The SDK reads the first `posthog-meta.properties` on the classpath, so a library's file could replace the application's. A module that depends on the application and doesn't upload, such as one for integration tests or a distribution, leaves the application uploading. A library that runs the goal in a build without its application, such as `mvn -pl billing deploy` or a build of its own repository, puts its file into its jar. So run the goal only in the application module, and add the library's source directories to its `additionalSourceRoots`: | ||
|
|
||
| ```xml | ||
| <configuration> | ||
| <additionalSourceRoots> | ||
| <additionalSourceRoot>${project.basedir}/../billing/src/main/java</additionalSourceRoot> | ||
| </additionalSourceRoots> | ||
| </configuration> | ||
| ``` | ||
|
|
||
| When the upload can't run, the plugin warns and the build continues. That happens when hog is missing or too old to upload JVM sources, the credentials are missing, or the upload fails, for example because PostHog can't be reached. That build ships no id, so its exceptions show no source code. Every build also deletes the `posthog-meta.properties` an earlier build left behind, and hog writes a new one only after a successful upload, so the app never reports an id that belongs to other code. | ||
|
|
||
| ### Requiring the upload | ||
|
|
||
| In the build that deploys your application, set `<requireUpload>true</requireUpload>` or pass `-Dposthog.requireUpload=true`. That build uploads wherever it runs, also outside CI, for example in a Dockerfile, and each of the problems above fails it instead of shipping a build without source code. A configured source root that doesn't exist fails it too. | ||
|
|
||
| Set it in the application module, not in a parent POM that library modules share: it also makes a library module upload. | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | User property | Default | Description | | ||
| |---|---|---|---| | ||
| | `skip` | `posthog.skip` | `false` | Skip the upload. | | ||
| | `requireUpload` | `posthog.requireUpload` | `false` | Upload from every build, also outside CI and in library modules, and fail the build when the upload can't run. | | ||
| | `hogExecutable` | `posthog.hogExecutable` | found automatically | Path to hog, relative to the project directory. Without it, the plugin checks `PATH`, then `~/.local/bin`, nvm, `/usr/local/bin` and `/opt/homebrew/bin`, then `node_modules/.bin` in the project directory and its parent, as the PostHog Android Gradle plugin does. On Windows, it checks `PATH` as cmd does, then `~/.local/bin/hog.exe`, and runs the binary of the npm package behind `hog.cmd`. | | ||
| | `dotenvFile` | `posthog.dotenvFile` | none | A dotenv file hog reads credentials from. | | ||
| | `releaseName` | `posthog.releaseName` | `${project.artifactId}` | The release the build belongs to. | | ||
| | `releaseVersion` | `posthog.releaseVersion` | the git commit | The version of that release. Set it to `${project.version}` if you bump the project version on every release. | | ||
| | `sourceRoots` | none | the project's compile source roots, plus the `<sourceDirs>` of `kotlin-maven-plugin` | The directories to upload. Setting it replaces the default. | | ||
| | `additionalSourceRoots` | none | none | More directories to upload with `sourceRoots`, such as those of other modules. | | ||
|
|
||
| hog uploads only `.java` and `.kt` files from these directories. It keys each file by the package it declares, and skips hidden folders, `node_modules`, test source sets such as `src/test`, `build`, `target` and `out` folders next to a build file or `src` folder, and files over 1 MiB. | ||
|
|
||
| ## What it doesn't cover yet | ||
|
|
||
| - The SDK sends one id, the one in the application's `posthog-meta.properties`. Frames from other modules show source code only when their sources are in that upload, and frames from a library built somewhere else show none. | ||
| - GraalVM native images don't include `posthog-meta.properties` unless you register it as a resource. |
This file contains hidden or 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,79 @@ | ||
| // The generated plugin descriptor takes its groupId from the project, and Maven refuses a plugin | ||
| // whose descriptor and coordinates disagree. | ||
| group = "com.posthog" | ||
| version = properties["mavenPluginVersion"].toString() | ||
|
|
||
| plugins { | ||
| `java-library` | ||
|
|
||
| // Generates META-INF/maven/plugin.xml from the @Mojo and @Parameter annotations. | ||
| id("org.gradlex.maven-plugin-development") version "1.0.3" | ||
|
|
||
| // publish | ||
| `maven-publish` | ||
| signing | ||
| } | ||
|
|
||
| java { | ||
| withSourcesJar() | ||
| withJavadocJar() | ||
| sourceCompatibility = PosthogBuildConfig.Build.JAVA_VERSION | ||
|
ablaszkiewicz marked this conversation as resolved.
Resolved
|
||
| targetCompatibility = PosthogBuildConfig.Build.JAVA_VERSION | ||
| } | ||
|
|
||
| mavenPlugin { | ||
|
ablaszkiewicz marked this conversation as resolved.
Resolved
|
||
| name.set("PostHog Maven Plugin") | ||
| description.set("Uploads JVM sources to PostHog so error tracking can show source code on exception frames") | ||
| goalPrefix.set("posthog") | ||
| } | ||
|
|
||
| // Maven supplies its own API at runtime, so these stay off the published POM. The plugin has no | ||
| // runtime dependencies of its own, which keeps it free of version clashes with the build it runs in. | ||
| val mavenVersion = "3.9.9" | ||
| val mavenPluginToolsVersion = "3.15.1" | ||
|
|
||
| dependencies { | ||
| compileOnly("org.apache.maven:maven-plugin-api:$mavenVersion") | ||
| compileOnly("org.apache.maven:maven-core:$mavenVersion") | ||
| compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations:$mavenPluginToolsVersion") | ||
|
|
||
| testImplementation("org.apache.maven:maven-plugin-api:$mavenVersion") | ||
| testImplementation("org.apache.maven:maven-core:$mavenVersion") | ||
| testCompileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations:$mavenPluginToolsVersion") | ||
| testImplementation("junit:junit:4.13.2") | ||
| } | ||
|
|
||
| publishing { | ||
| publications { | ||
| create<MavenPublication>("maven") { | ||
| from(components["java"]) | ||
|
|
||
| postHogConfig(project.name, project.version.toString()) | ||
| pom.postHogConfig( | ||
| project.name, | ||
| moduleDescription = "Official PostHog Maven plugin: uploads JVM sources for error tracking source context", | ||
| ) | ||
| pom.packaging = "maven-plugin" | ||
| } | ||
| } | ||
| signing.postHogConfig("maven", this) | ||
| } | ||
|
|
||
| // The plugin runs in the JVM of the Maven build, which can be Java 8. --release also rejects a call | ||
| // to an API newer than Java 8, which sourceCompatibility alone lets through. | ||
| tasks.named<JavaCompile>("compileJava") { | ||
| options.release.set(8) | ||
| } | ||
|
|
||
| // The descriptor test reads META-INF/maven/plugin.xml from the jar, as Maven does. | ||
| tasks.named<Test>("test") { | ||
| val pluginJar = tasks.named<Jar>("jar").flatMap { it.archiveFile } | ||
| inputs.file(pluginJar) | ||
| jvmArgumentProviders.add(CommandLineArgumentProvider { listOf("-Dposthog.pluginJar=${pluginJar.get().asFile}") }) | ||
| } | ||
|
|
||
| tasks.javadoc { | ||
| if (JavaVersion.current().isJava9Compatible) { | ||
| (options as StandardJavadocDocletOptions).addBooleanOption("html5", true) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.