Skip to content
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

sql_exporter: adding package plus config, user and group #869

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 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,24 @@
#!/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"

depend() {
need net
}

start() {
ebegin "Starting sql_exporter"
start-stop-daemon --start --chuid sql_exporter --exec /usr/bin/sql_exporter -- \
-config.file /etc/sql_exporter/sql_exporter.yml \
>> /var/log/sql_exporter/sql_exporter.log 2>&1 &
eend $?
}

stop() {
ebegin "Stopping sql_exporter"
start-stop-daemon --stop --quiet --exec /usr/bin/sql_exporter
eend $?
}
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: "example"
# interval defined the pause between the runs of this job
interval: '5m'
# 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://postgres@localhost/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>
61 changes: 61 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,61 @@
# 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="acct-group/sql_exporter
acct-user/sql_exporter"
EGO_PN="github.com/justwatchcom/sql_exporter"

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

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

src_install() {
# Create user and group
enewgroup sql_exporter
enewuser sql_exporter -1 -1 /var/log/sql_exporter sql_exporter

dobin "${S}/bin/sql_exporter" || die
monsieurp marked this conversation as resolved.
Show resolved Hide resolved
newinitd "${FILESDIR}/sql_exporter.init.d" sql_exporter || die
monsieurp marked this conversation as resolved.
Show resolved Hide resolved
exeinto /etc/init.d
newexe "${FILESDIR}/sql_exporter.init.d" sql_exporter || die
monsieurp marked this conversation as resolved.
Show resolved Hide resolved
dosym /etc/init.d/sql_exporter /etc/runlevels/default/sql_exporter || die
monsieurp marked this conversation as resolved.
Show resolved Hide resolved

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

# Create log directory and file
if ! dodir /var/log/sql_exporter; then
die "Failed to create log directory"
fi
newins /dev/null sql_exporter.log || die "Failed to create log file"
monsieurp marked this conversation as resolved.
Show resolved Hide resolved
}

src_test() {
cd "${S}/src/${EGO_PN}" || die
go test -v ./... || die "Tests failed"
}
Loading