-
Notifications
You must be signed in to change notification settings - Fork 832
fix: hide HTTP scrape error details #2332
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
zeitlinger
wants to merge
4
commits into
main
Choose a base branch
from
agent/hide-http-error-details
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.
+418
−52
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
19 changes: 18 additions & 1 deletion
19
docs/apidiffs/current_vs_latest/prometheus-metrics-exporter-httpserver.txt
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
11 changes: 11 additions & 0 deletions
11
...r/it-exporter-test/src/test/java/io/prometheus/metrics/it/exporter/test/HttpServerIT.java
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 |
|---|---|---|
| @@ -1,10 +1,21 @@ | ||
| package io.prometheus.metrics.it.exporter.test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
|
|
||
| class HttpServerIT extends ExporterIT { | ||
| public HttpServerIT() throws IOException, URISyntaxException { | ||
| super("exporter-httpserver-sample"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void assertErrorResponseBody(String body) { | ||
| assertThat(body) | ||
| .isEqualTo( | ||
| "An internal error occurred while scraping metrics. " | ||
| + "Configure an HTTP error reporter for details.\n") | ||
| .doesNotContain("Simulating an error."); | ||
| } | ||
| } |
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
103 changes: 103 additions & 0 deletions
103
...rver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpErrorHandlingPolicy.java
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,103 @@ | ||
| package io.prometheus.metrics.exporter.httpserver; | ||
|
|
||
| import io.prometheus.metrics.annotations.StableApi; | ||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.function.Consumer; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Controls how the {@link HTTPServer} handles exceptions raised while scraping metrics. | ||
| * | ||
| * <p>The default policy built by {@link #builder()} does not expose exception details and does not | ||
| * report the exception. Configure the builder to route diagnostic details to an | ||
| * application-appropriate sink. | ||
| */ | ||
| @StableApi | ||
| public final class HttpErrorHandlingPolicy { | ||
|
|
||
| private static final byte[] GENERIC_RESPONSE = | ||
| ("An internal error occurred while scraping metrics. " | ||
| + "Configure an HTTP error reporter for details.\n") | ||
| .getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| private final boolean unsafeDebugResponse; | ||
| @Nullable private final Consumer<Throwable> errorReporter; | ||
|
|
||
| private HttpErrorHandlingPolicy( | ||
| boolean unsafeDebugResponse, @Nullable Consumer<Throwable> errorReporter) { | ||
| this.unsafeDebugResponse = unsafeDebugResponse; | ||
| this.errorReporter = errorReporter; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a builder for configuring scrape error handling. | ||
| * | ||
| * <p>The builder defaults to a generic HTTP 500 response with no error reporter. This avoids | ||
| * exposing exception details to scrape clients or adding an implicit dependency on an | ||
| * application's logging configuration. | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| byte[] getErrorResponse(Exception exception) { | ||
| if (!unsafeDebugResponse) { | ||
| return GENERIC_RESPONSE; | ||
| } | ||
| StringWriter stringWriter = new StringWriter(); | ||
| PrintWriter printWriter = new PrintWriter(stringWriter); | ||
| printWriter.write("An Exception occurred while scraping metrics: "); | ||
| exception.printStackTrace(printWriter); | ||
| return stringWriter.toString().getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| void report(Throwable error) { | ||
| if (errorReporter != null) { | ||
| errorReporter.accept(error); | ||
| } | ||
| } | ||
|
|
||
| /** Builder for {@link HttpErrorHandlingPolicy}. */ | ||
| public static final class Builder { | ||
|
|
||
| private boolean unsafeDebugResponse = false; | ||
| @Nullable private Consumer<Throwable> errorReporter; | ||
|
|
||
| private Builder() {} | ||
|
|
||
| /** | ||
| * Pass scrape exceptions to {@code errorReporter}. | ||
| * | ||
| * <p>The reporter runs synchronously on the HTTP request thread. It should return promptly and | ||
| * must be safe to call concurrently. Runtime exceptions thrown by the reporter are isolated | ||
| * from HTTP response handling. | ||
| */ | ||
| public Builder errorReporter(Consumer<Throwable> errorReporter) { | ||
| if (errorReporter == null) { | ||
| throw new NullPointerException("errorReporter"); | ||
| } | ||
| this.errorReporter = errorReporter; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configure whether the HTTP 500 response includes the full exception stack trace. | ||
| * | ||
| * <p><strong>Security warning:</strong> Setting this to {@code true} exposes internal exception | ||
| * information to scrape clients. Do not enable it for endpoints reachable by untrusted clients. | ||
| * | ||
| * <p>This setting is independent of {@link #errorReporter(Consumer)}. | ||
| */ | ||
| public Builder unsafeDebugResponse(boolean unsafeDebugResponse) { | ||
| this.unsafeDebugResponse = unsafeDebugResponse; | ||
| return this; | ||
| } | ||
|
|
||
| /** Build the policy. */ | ||
| public HttpErrorHandlingPolicy build() { | ||
| return new HttpErrorHandlingPolicy(unsafeDebugResponse, errorReporter); | ||
| } | ||
| } | ||
| } |
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
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.