Skip to content

Commit

Permalink
sql_exporter: adding package plus config, user and group (#869)
Browse files Browse the repository at this point in the history
* sql_exporter: adding package plus config, user and group

* app-metrics/sql_exporter: improve init script

* app-metrics/sql_exporter: rework ebuild

* sql_exporter: adding package plus config, user and group

---------

Co-authored-by: Patrice Clement <[email protected]>
  • Loading branch information
leomuso and monsieurp authored Apr 16, 2024
1 parent f136d49 commit f7ed2a0
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
7 changes: 7 additions & 0 deletions acct-group/sql_exporter/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
<pkgmetadata>
<maintainer type="person">
<email>[email protected]</email>
</maintainer>
</pkgmetadata>
8 changes: 8 additions & 0 deletions acct-group/sql_exporter/sql_exporter-0.ebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit acct-group

ACCT_GROUP_ID=-1
7 changes: 7 additions & 0 deletions acct-user/sql_exporter/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
<pkgmetadata>
<maintainer type="person">
<email>[email protected]</email>
</maintainer>
</pkgmetadata>
13 changes: 13 additions & 0 deletions acct-user/sql_exporter/sql_exporter-0.ebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit acct-user

DESCRIPTION="SQL Exporter for Prometheus"

ACCT_USER_ID=-1
ACCT_USER_GROUPS=( ${PN} )

acct-user_add_deps
1 change: 1 addition & 0 deletions app-metrics/sql_exporter/Manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DIST sql_exporter-0.5.3.tar.gz 11600200 BLAKE2B f249f54bae8c42984087f365530392d8dd3162a9314e4a58744de23463786833d452ed379b2f785871fbeca6c9feb312307d59f490677ce71df81b33c3f313f7 SHA512 e6f01bdd47e9cf43077662d42fb4500dc0dbfc2bd3462e8082595cfd810c104e6404334c272ecb3459dc604f85147089c88da73200b68543bc4473ab37c8c3e0
15 changes: 15 additions & 0 deletions app-metrics/sql_exporter/files/sql_exporter.init.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/sbin/openrc-run
# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="sql_exporter daemon"
description="SQL Exporter allows to run custom queries against a database"
command="/usr/bin/sql_exporter"
command_args="-config.file /etc/sql_exporter/sql_exporter.yml"
output_log="/var/log/sql_exporter/sql_exporter.log"
error_log="/var/log/sql_exporter/sql_exporter.log"
start_stop_daemon_args="--background --user sql_exporter"

depend() {
need net
}
47 changes: 47 additions & 0 deletions app-metrics/sql_exporter/files/sql_exporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# jobs is a map of jobs, define any number but please keep the connection usage on the DBs in mind
jobs:
# each job needs a unique name, it's used for logging and as an default label
- name: "postgres"
# interval defined the pause between the runs of this job
interval: '1m'
# cron_schedule when to execute the job in the standard CRON syntax
# if specified, the interval is ignored
#cron_schedule: "0 0 * * *"
# connections is an array of connection URLs
# each query will be executed on each connection
connections:
- 'postgres://sql_exporter@/?host=/run/postgresql&dbname=postgres&sslmode=disable'
# startup_sql is an array of SQL statements
# each statements is executed once after connecting
startup_sql:
- 'SET lock_timeout = 1000'
- 'SET idle_in_transaction_session_timeout = 100'
# queries is a map of Metric/Query mappings
queries:
# name is prefied with sql_ and used as the metric name
- name: "running_queries"
# help is a requirement of the Prometheus default registry, currently not
# used by the Prometheus server. Important: Must be the same for all metrics
# with the same name!
help: "Number of running queries"
# Optional: Column to use as a metric timestamp source.
# Leave unset if it's not needed
timestamp: "created_at"
# Labels is an array of columns which will be used as additional labels.
# Must be the same for all metrics with the same name!
# All labels columns should be of type text, varchar or string
labels:
- "datname"
- "usename"
# Values is an array of columns used as metric values. All values should be
# of type float
values:
- "count"
# Query is the SQL query that is run unalterted on the each of the connections
# for this job
query: |
SELECT now() as created_at, datname::text, usename::text, COUNT(*)::float AS count
FROM pg_stat_activity GROUP BY created_at, datname, usename;
# Consider the query failed if it returns zero rows
allow_zero_rows: false
11 changes: 11 additions & 0 deletions app-metrics/sql_exporter/metadata.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pkgmetadata SYSTEM "https://www.gentoo.org/dtd/metadata.dtd">
<pkgmetadata>
<maintainer type="person">
<email>[email protected]</email>
<name>Adjust Ops</name>
</maintainer>
<upstream>
<remote-id type="github">justwatchcom/sql_exporter</remote-id>
</upstream>
</pkgmetadata>
59 changes: 59 additions & 0 deletions app-metrics/sql_exporter/sql_exporter-0.5.3.ebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2024 @Leo
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit golang-build golang-vcs-snapshot

DESCRIPTION="Prometheus exporter for SQL database metrics."
HOMEPAGE="https://github.com/justwatchcom/sql_exporter"
SRC_URI="https://github.com/justwatchcom/sql_exporter/archive/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="doc"

DEPEND="
dev-lang/go
acct-group/sql_exporter
acct-user/sql_exporter"

RDEPEND="
${DEPEND}"

EGO_PN="github.com/justwatchcom/sql_exporter"

src_prepare() {
default
cd "${S}"
export GO111MODULE=auto
}

src_compile() {
export GOPATH="${S}"
cd "${S}/src/${EGO_PN}"
echo "compiling from $(pwd)"
go build -o "${S}/bin/sql_exporter"
}

src_test() {
cd "${S}/src/${EGO_PN}"
go test -v ./...
}

src_install() {
dobin "${S}/bin/sql_exporter"
newinitd "${FILESDIR}/sql_exporter.init.d" sql_exporter
dosym /etc/init.d/sql_exporter /etc/runlevels/default/sql_exporter

insinto /etc/sql_exporter
doins "${FILESDIR}/sql_exporter.yml"

# Create log directory and file
dodir /var/log/sql_exporter
touch "${ED}"/var/log/sql_exporter/sql_exporter.log

# Change ownership of the log file to sql_exporter
chown sql_exporter:sql_exporter "${ED}"/var/log/sql_exporter/sql_exporter.log
}

0 comments on commit f7ed2a0

Please sign in to comment.