-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HikariCP library instrumentation (open-telemetry#6023)
- Loading branch information
Showing
14 changed files
with
249 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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
24 changes: 24 additions & 0 deletions
24
...t/src/main/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariSingletons.java
This file contains 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,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hikaricp; | ||
|
||
import com.zaxxer.hikari.metrics.MetricsTrackerFactory; | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.hikaricp.HikariTelemetry; | ||
import javax.annotation.Nullable; | ||
|
||
public final class HikariSingletons { | ||
|
||
private static final HikariTelemetry hikariTelemetry = | ||
HikariTelemetry.create(GlobalOpenTelemetry.get()); | ||
|
||
public static MetricsTrackerFactory createMetricsTrackerFactory( | ||
@Nullable MetricsTrackerFactory delegate) { | ||
return hikariTelemetry.createMetricsTrackerFactory(delegate); | ||
} | ||
|
||
private HikariSingletons() {} | ||
} |
32 changes: 32 additions & 0 deletions
32
...t/java/io/opentelemetry/javaagent/instrumentation/hikaricp/HikariInstrumentationTest.java
This file contains 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,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hikaricp; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.metrics.MetricsTrackerFactory; | ||
import io.opentelemetry.instrumentation.hikaricp.AbstractHikariInstrumentationTest; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import javax.annotation.Nullable; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class HikariInstrumentationTest extends AbstractHikariInstrumentationTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected InstrumentationExtension testing() { | ||
return testing; | ||
} | ||
|
||
@Override | ||
protected void configure(HikariConfig poolConfig, @Nullable MetricsTrackerFactory userTracker) { | ||
if (userTracker != null) { | ||
poolConfig.setMetricsTrackerFactory(userTracker); | ||
} | ||
} | ||
} |
This file contains 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,41 @@ | ||
# Manual Instrumentation for HikariCP | ||
|
||
Provides OpenTelemetry instrumentation for [HikariCP](https://github.com/brettwooldridge/HikariCP). | ||
|
||
## Quickstart | ||
|
||
### Add these dependencies to your project: | ||
|
||
Replace `OPENTELEMETRY_VERSION` with the latest stable | ||
[release](https://mvnrepository.com/artifact/io.opentelemetry). `Minimum version: 1.14.0` | ||
|
||
For Maven, add to your `pom.xml` dependencies: | ||
|
||
```xml | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.opentelemetry.instrumentation</groupId> | ||
<artifactId>opentelemetry-hikaricp-3.0</artifactId> | ||
<version>OPENTELEMETRY_VERSION</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
|
||
For Gradle, add to your dependencies: | ||
|
||
```groovy | ||
implementation("io.opentelemetry.instrumentation:opentelemetry-hikaricp-3.0:OPENTELEMETRY_VERSION") | ||
``` | ||
|
||
### Usage | ||
|
||
The instrumentation library provides a `MetricsTrackerFactory` implementation that can be added to | ||
an instance of the `HikariConfig` (or `HikariDataSource`) to provide OpenTelemetry-based metrics. | ||
|
||
```java | ||
void configure(OpenTelemetry openTelemetry, HikariConfig connectionPoolConfig) { | ||
HikariTelemetry telemetry = HikariTelemetry.create(openTelemetry); | ||
connectionPoolConfig.setMetricsTrackerFactory(telemetry.createMetricsTrackerFactory()); | ||
} | ||
``` |
This file contains 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,10 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
id("otel.nullaway-conventions") | ||
} | ||
|
||
dependencies { | ||
library("com.zaxxer:HikariCP:3.0.0") | ||
|
||
testImplementation(project(":instrumentation:hikaricp-3.0:testing")) | ||
} |
45 changes: 45 additions & 0 deletions
45
...-3.0/library/src/main/java/io/opentelemetry/instrumentation/hikaricp/HikariTelemetry.java
This file contains 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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.hikaricp; | ||
|
||
import com.zaxxer.hikari.metrics.MetricsTrackerFactory; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import javax.annotation.Nullable; | ||
|
||
/** Entrypoint for instrumenting Hikari database connection pools. */ | ||
public final class HikariTelemetry { | ||
|
||
/** Returns a new {@link HikariTelemetry} configured with the given {@link OpenTelemetry}. */ | ||
public static HikariTelemetry create(OpenTelemetry openTelemetry) { | ||
return new HikariTelemetry(openTelemetry); | ||
} | ||
|
||
private final OpenTelemetry openTelemetry; | ||
|
||
private HikariTelemetry(OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
/** | ||
* Returns a new {@link MetricsTrackerFactory} that can be registered using {@link | ||
* com.zaxxer.hikari.HikariConfig#setMetricsTrackerFactory(MetricsTrackerFactory)}. | ||
*/ | ||
public MetricsTrackerFactory createMetricsTrackerFactory() { | ||
return createMetricsTrackerFactory(null); | ||
} | ||
|
||
/** | ||
* Returns a new {@link MetricsTrackerFactory} that can be registered using {@link | ||
* com.zaxxer.hikari.HikariConfig#setMetricsTrackerFactory(MetricsTrackerFactory)}. The {@link | ||
* com.zaxxer.hikari.metrics.IMetricsTracker} objects created by the returned factory will | ||
* delegate to trackers created by the {@code delegate} metrics tracker factory, if it is not | ||
* null. | ||
*/ | ||
public MetricsTrackerFactory createMetricsTrackerFactory( | ||
@Nullable MetricsTrackerFactory delegate) { | ||
return new OpenTelemetryMetricsTrackerFactory(openTelemetry, delegate); | ||
} | ||
} |
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
...ry/src/test/java/io/opentelemetry/instrumentation/hikaricp/HikariInstrumentationTest.java
This file contains 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,31 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.hikaricp; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.metrics.MetricsTrackerFactory; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import javax.annotation.Nullable; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class HikariInstrumentationTest extends AbstractHikariInstrumentationTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected InstrumentationExtension testing() { | ||
return testing; | ||
} | ||
|
||
@Override | ||
protected void configure(HikariConfig poolConfig, @Nullable MetricsTrackerFactory userTracker) { | ||
poolConfig.setMetricsTrackerFactory( | ||
HikariTelemetry.create(testing().getOpenTelemetry()) | ||
.createMetricsTrackerFactory(userTracker)); | ||
} | ||
} |
This file contains 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,11 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
} | ||
|
||
dependencies { | ||
api(project(":testing-common")) | ||
api("org.mockito:mockito-core") | ||
api("org.mockito:mockito-junit-jupiter") | ||
|
||
compileOnly("com.zaxxer:HikariCP:3.0.0") | ||
} |
Oops, something went wrong.