Skip to content

Commit

Permalink
Merge pull request #1964 from newrelic/hikaricp-24
Browse files Browse the repository at this point in the history
HikariCP 2.4.0 module
  • Loading branch information
jtduffy authored Jul 1, 2024
2 parents 85e2eea + 03fa992 commit 6bb98c4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
13 changes: 13 additions & 0 deletions instrumentation/hikaricp-2.4.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# hikaricp-2.4.0 Instrumentation Module
This module supports HikariCP version 2.4.0 to current.

### Important Note Regarding Use of the Legacy HikariCP Extension Module
This built in instrumentation module replaces the extension module that can be downloaded from the New Relic
[download site](https://download.newrelic.com/newrelic/java-agent/extensions/). The `hikaricp-2.4.0.jar`
artifact should be removed from the agent's `extension` folder if present.

### Reported Metrics
Every 5 seconds, the following metrics will be reported:
- `Database Connection/HikariCP/{PoolName}/Busy Count[connections]`: Retrieved from the [HikariPoolMXBean](https://www.javadoc.io/doc/com.zaxxer/HikariCP/2.4.6/com/zaxxer/hikari/HikariPoolMXBean.html) instance
- `Database Connection/HikariCP/{PoolName}/Idle Count[connections]`: Retrieved from the [HikariPoolMXBean](https://www.javadoc.io/doc/com.zaxxer/HikariCP/2.4.6/com/zaxxer/hikari/HikariPoolMXBean.html) instance
- `Database Connection/HikariCP/{PoolName}/Max Pool Size[connections]`: Retrieved from the [HikariConfig](https://www.javadoc.io/static/com.zaxxer/HikariCP/2.4.6/index.html?com/zaxxer/hikari/pool/HikariPool.html) instance
19 changes: 19 additions & 0 deletions instrumentation/hikaricp-2.4.0/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dependencies {
implementation(project(":agent-bridge"))
implementation("com.zaxxer:HikariCP:2.4.1")
}

jar {
manifest {
attributes 'Implementation-Title': 'com.newrelic.instrumentation.hikaricp-2.4.0'
}
}

verifyInstrumentation {
passesOnly 'com.zaxxer:HikariCP:[2.4.0,)'
}

site {
title 'HikariCP'
type 'Datastore'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.zaxxer.hikari.pool;

import java.util.concurrent.TimeUnit;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariPoolMXBean;

@Weave(originalName = "com.zaxxer.hikari.pool.HikariPool", type = MatchType.ExactClass)
public abstract class HikariPool_Instrumentation implements HikariPoolMXBean {

public HikariPool_Instrumentation(HikariConfig config) {
AgentBridge.privateApi.addSampler(new PooledDataSourceSampler(this, config), 5, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.zaxxer.hikari.pool;

import com.newrelic.api.agent.MetricAggregator;
import com.newrelic.api.agent.NewRelic;
import com.zaxxer.hikari.HikariConfig;

import java.lang.ref.WeakReference;

/**
* This sampler keeps a weak reference to a BaseHikariPool and
* reports metrics about it.
*/
public class PooledDataSourceSampler implements Runnable {

private final WeakReference<HikariPool_Instrumentation> hikariPoolRef;
private final WeakReference<HikariConfig> hikariConfigRef;
private final String baseName;

public PooledDataSourceSampler(HikariPool_Instrumentation hikariPool, HikariConfig config) {
this.hikariPoolRef = new WeakReference<>(hikariPool);
this.hikariConfigRef = new WeakReference<>(config);
this.baseName = "Database Connection/HikariCP/" + config.getPoolName() + '/';
}

@Override
public void run() {
HikariPool_Instrumentation hikariPool = hikariPoolRef.get();
HikariConfig config = hikariConfigRef.get();
if (null == hikariPool || null == config) {
return;
}

MetricAggregator metricAggregator = NewRelic.getAgent().getMetricAggregator();
metricAggregator.recordMetric(baseName + "Busy Count[connections]", hikariPool.getActiveConnections());
metricAggregator.recordMetric(baseName + "Idle Count[connections]", hikariPool.getIdleConnections());
metricAggregator.recordMetric(baseName + "Max Pool Size[connections]", config.getMaximumPoolSize());
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ include 'instrumentation:hibernate-3.5'
include 'instrumentation:hibernate-4.0'
include 'instrumentation:hibernate-4.2'
include 'instrumentation:hibernate-4.3'
include 'instrumentation:hikaricp-2.4.0'
include 'instrumentation:http4s-blaze-server-2.13_0.21'
include 'instrumentation:http4s-blaze-server-2.12_0.21'
include 'instrumentation:http4s-blaze-server-2.13_0.22'
Expand Down

0 comments on commit 6bb98c4

Please sign in to comment.