-
Notifications
You must be signed in to change notification settings - Fork 631
feat(client-v2,jdbc-v2): add metrics recorder SPI #3082
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
Merged
Merged
Changes from 1 commit
Commits
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
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
75 changes: 75 additions & 0 deletions
75
client-v2/src/main/java/com/clickhouse/client/api/observability/DefaultMetricsRecorder.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,75 @@ | ||
| package com.clickhouse.client.api.observability; | ||
|
|
||
| import com.clickhouse.client.api.insert.InsertSettings; | ||
| import com.clickhouse.client.api.metrics.OperationMetrics; | ||
| import com.clickhouse.client.api.query.QuerySettings; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * Base class for {@link MetricsRecorder} implementations. Every method records nothing, so a | ||
| * subclass overrides only what it wants to record and keeps working when the client starts reporting | ||
| * an event the subclass does not know about. | ||
| * <p> | ||
| * A subclass owns its instruments and decides what to report. To use the client's standard metric | ||
| * names, units and attributes it can hand the structures it is given to {@link #getMetricsSupport()}: | ||
| * <pre>{@code | ||
| * public void recordQuerySuccess(QuerySettings settings, OperationMetrics metrics) { | ||
| * MetricsSupport support = getMetricsSupport(); | ||
| * myHistogram.record(support.operationDuration(metrics), support.queryAttributes(settings, null)); | ||
| * } | ||
| * }</pre> | ||
| * Using it is optional - a recorder that reports something else, or in another form, ignores it, and | ||
| * one that wants other values overrides {@link #getMetricsSupport()} with its own subclass of | ||
| * {@link MetricsSupport}. | ||
| * <p> | ||
| * An instance of this class itself records nothing and is what the client uses when no recorder is | ||
| * registered. | ||
| */ | ||
| public class DefaultMetricsRecorder implements MetricsRecorder { | ||
|
|
||
| /** | ||
| * Shared instance that records nothing. | ||
| */ | ||
| public static final DefaultMetricsRecorder NOOP = new DefaultMetricsRecorder(); | ||
|
|
||
| /** | ||
| * Returns the helper a subclass can use to derive the client's standard metric names, units and | ||
| * attributes. Override to report other values. | ||
| * | ||
| * @return metrics support; never {@code null} | ||
| */ | ||
| protected MetricsSupport getMetricsSupport() { | ||
| return MetricsSupport.DEFAULT; | ||
| } | ||
|
|
||
| @Override | ||
| public void recordQuerySuccess(QuerySettings settings, OperationMetrics metrics) { | ||
| // records nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void recordInsertSuccess(InsertSettings settings, String tableName, OperationMetrics metrics) { | ||
| // records nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void recordQueryFailure(QuerySettings settings, Duration duration, Throwable t) { | ||
| // records nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void recordInsertFailure(InsertSettings settings, String tableName, Duration duration, Throwable t) { | ||
| // records nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void recordQueryRetry(QuerySettings settings, Throwable cause) { | ||
| // records nothing | ||
| } | ||
|
|
||
| @Override | ||
| public void recordInsertRetry(InsertSettings settings, String tableName, Throwable cause) { | ||
| // records nothing | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
client-v2/src/main/java/com/clickhouse/client/api/observability/MetricAttribute.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,62 @@ | ||
| package com.clickhouse.client.api.observability; | ||
|
|
||
| /** | ||
| * Attribute keys recorded on the metrics of {@link MetricName} by the client. | ||
| * <p> | ||
| * Keys follow the OpenTelemetry semantic conventions for database clients. They are defined here, on | ||
| * the SPI side, so that every {@link MetricsRecorder} implementation reports the same key for the | ||
| * same piece of information. | ||
| * <p> | ||
| * This is deliberately a smaller set than {@link SpanAttribute}: an attribute of a metric becomes a | ||
| * time series, so only low-cardinality values are reported. The statement text, the query id and the | ||
| * statement parameters are recorded on spans only. | ||
| */ | ||
| public enum MetricAttribute { | ||
|
|
||
| /** | ||
| * Database system name. Always {@code clickhouse}. | ||
| */ | ||
| DB_SYSTEM_NAME("db.system.name"), | ||
|
|
||
| /** | ||
| * Target database name. | ||
| */ | ||
| DB_NAMESPACE("db.namespace"), | ||
|
|
||
| /** | ||
| * Name of the client operation - {@code query} or {@code insert}. | ||
| */ | ||
| DB_OPERATION_NAME("db.operation.name"), | ||
|
|
||
| /** | ||
| * Table the operation targets. Recorded for an insert. | ||
| */ | ||
| DB_COLLECTION_NAME("db.collection.name"), | ||
|
|
||
| /** | ||
| * ClickHouse error code returned by the server. Recorded when an operation fails and the server | ||
| * reported one. | ||
| */ | ||
| DB_RESPONSE_STATUS_CODE("db.response.status_code"), | ||
|
|
||
| /** | ||
| * Type of the error that made an operation fail, usually an exception class name. Recorded only | ||
| * on a failure, so a time series without it is the successful one. | ||
| */ | ||
| ERROR_TYPE("error.type"); | ||
|
|
||
| private final String key; | ||
|
|
||
| MetricAttribute(String key) { | ||
| this.key = key; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the attribute key. | ||
| * | ||
| * @return attribute key | ||
| */ | ||
| public String getKey() { | ||
| return key; | ||
| } | ||
| } |
Oops, something went wrong.
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.