-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will eventually switch over to hikari.
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apply from: "$rootDir/gradle/kotlin.gradle" | ||
|
||
tasks.compileGroovy.enabled = false | ||
|
||
dependencies { | ||
implementation "org.mariadb.jdbc:mariadb-java-client:2.4.0" | ||
implementation "com.netflix.spinnaker.kork:kork-sql" | ||
implementation("org.jooq:jooq:3.9.6"){ | ||
force = true | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ql-mariadb/src/main/kotlin/com/netflix/spinnaker/config/MariaDbDataSourceConfiguration.kt
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,25 @@ | ||
package com.netflix.spinnaker.config | ||
|
||
import com.netflix.spectator.api.Registry | ||
import com.netflix.spinnaker.sql.MariaDbConnectionPoolMetricsExporter | ||
import com.netflix.spinnaker.sql.MariaDbDataSourceFactory | ||
import com.netflix.spinnaker.kork.sql.config.DataSourceFactory | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.ComponentScan | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@ConditionalOnProperty("sql.enabled") | ||
@ComponentScan("com.netflix.spinnaker.sql") | ||
class MariaDbDataSourceConfiguration { | ||
|
||
@Bean | ||
fun connectionPoolMetricsExporter(registry: Registry): MariaDbConnectionPoolMetricsExporter { | ||
return MariaDbConnectionPoolMetricsExporter(registry) | ||
} | ||
|
||
@Bean | ||
fun dataSourceFactory(connectionPoolMetricsExporter: MariaDbConnectionPoolMetricsExporter): DataSourceFactory = | ||
MariaDbDataSourceFactory(connectionPoolMetricsExporter) | ||
} |
58 changes: 58 additions & 0 deletions
58
...mariadb/src/main/kotlin/com/netflix/spinnaker/sql/MariaDbConnectionPoolMetricsExporter.kt
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,58 @@ | ||
package com.netflix.spinnaker.sql | ||
|
||
import com.netflix.spectator.api.Registry | ||
import com.netflix.spectator.api.patterns.PolledMeter | ||
import org.mariadb.jdbc.MariaDbPoolDataSource | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
class MariaDbConnectionPoolMetricsExporter( | ||
private val registry: Registry | ||
) { | ||
|
||
private val dataSourceMetrics: MutableList<DataSourceMetrics> = mutableListOf() | ||
|
||
fun track(dataSource: MariaDbPoolDataSource) { | ||
dataSourceMetrics.add( | ||
DataSourceMetrics(dataSource).apply { | ||
registerMeters(registry) | ||
} | ||
) | ||
} | ||
|
||
@Scheduled(fixedRate = 5_000) | ||
fun record() { | ||
dataSourceMetrics.forEach { | ||
// They tell me not to use test methods, but yolo. Way better than dealing with MBeans. | ||
it.dataSource.testGetPool()?.let { pool -> | ||
it.activeConnections.set(pool.activeConnections) | ||
it.totalConnections.set(pool.totalConnections) | ||
it.idleConnections.set(pool.idleConnections) | ||
it.blockedConnections.set(pool.connectionRequests) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private data class DataSourceMetrics( | ||
val dataSource: MariaDbPoolDataSource, | ||
val activeConnections: AtomicLong = AtomicLong(), | ||
val totalConnections: AtomicLong = AtomicLong(), | ||
val idleConnections: AtomicLong = AtomicLong(), | ||
val blockedConnections: AtomicLong = AtomicLong() | ||
) { | ||
|
||
fun registerMeters(registry: Registry) { | ||
monitorValue(registry, "active", activeConnections) | ||
monitorValue(registry, "total", totalConnections) | ||
monitorValue(registry, "idle", idleConnections) | ||
monitorValue(registry, "blocked", blockedConnections) | ||
} | ||
|
||
private fun monitorValue(registry: Registry, name: String, value: AtomicLong) { | ||
PolledMeter | ||
.using(registry) | ||
.withName("sql.pool.${dataSource.poolName}.$name") | ||
.monitorValue(value) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
front50-sql-mariadb/src/main/kotlin/com/netflix/spinnaker/sql/MariaDbDataSourceFactory.kt
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,43 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.sql | ||
|
||
import com.netflix.spinnaker.kork.sql.config.ConnectionPoolProperties | ||
import com.netflix.spinnaker.kork.sql.config.DataSourceFactory | ||
import org.mariadb.jdbc.MariaDbPoolDataSource | ||
import org.springframework.beans.factory.BeanCreationException | ||
import java.sql.SQLException | ||
import javax.sql.DataSource | ||
|
||
class MariaDbDataSourceFactory( | ||
private val metricsExporter: MariaDbConnectionPoolMetricsExporter | ||
) : DataSourceFactory { | ||
|
||
override fun build(poolName: String, connectionPoolProperties: ConnectionPoolProperties): DataSource { | ||
try { | ||
val dataSource = MariaDbPoolDataSource(connectionPoolProperties.jdbcUrl) | ||
dataSource.poolName = poolName | ||
dataSource.user = connectionPoolProperties.user | ||
dataSource.setPassword(connectionPoolProperties.password) | ||
|
||
metricsExporter.track(dataSource) | ||
|
||
return dataSource | ||
} catch (e: SQLException) { | ||
throw BeanCreationException("Failed creating pooled data source", e) | ||
} | ||
} | ||
} |
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