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

Add hostname and service labels #12

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class PrometheusDhtBridge extends ReadyResource {
if (this.opened) await this._writeAliases()
}

putAlias (alias, targetPubKey, { write = true } = {}) {
putAlias (alias, targetPubKey, hostname, service, { write = true } = {}) {
if (!this.opened && write) throw new Error('Cannot put aliases before ready')

targetPubKey = idEnc.decode(idEnc.normalize(targetPubKey))
Expand All @@ -111,10 +111,13 @@ class PrometheusDhtBridge extends ReadyResource {

const entry = new AliasesEntry(
new ScraperClient(this.swarm, targetPubKey),
hostname,
service,
Date.now() + this.entryExpiryMs
)

this.aliases.set(alias, entry)
// TODO: just emit entry?
this.emit('set-alias', { alias, publicKey: targetPubKey, scrapeClient: entry.scrapeClient })
const updated = true

Expand Down Expand Up @@ -192,11 +195,12 @@ class PrometheusDhtBridge extends ReadyResource {
async _loadAliases () { // should never throw
try {
const aliases = await readPromTargets(this.promTargetsLoc)
for (const [alias, pubKey] of aliases) {

for (const [alias, { z32PubKey, hostname, service }] of aliases) {
// Write false since we load an existing state
// (otherwise we overwrite them 1 by 1, and can lose
// entries if we restart/crash during setup)
this.putAlias(alias, pubKey, { write: false })
this.putAlias(alias, z32PubKey, hostname, service, { write: false })
}
} catch (e) {
this.emit('load-aliases-error', e)
Expand All @@ -205,10 +209,12 @@ class PrometheusDhtBridge extends ReadyResource {
}

class AliasesEntry extends ReadyResource {
constructor (scrapeClient, expiry) {
constructor (scrapeClient, hostname, service, expiry) {
super()

this.scrapeClient = scrapeClient
this.hostname = hostname
this.service = service
this.expiry = expiry
}

Expand Down
8 changes: 5 additions & 3 deletions lib/prom-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ async function writePromTargets (location, aliases) {
// than to extract the pubkey in the caller
for (const [target, entry] of aliases) {
const pubKey = idEnc.normalize(entry.targetKey)
targets.push(`${target}:${pubKey}`)
const hostname = entry.hostname.replaceAll(':', '-')
const service = entry.service.replaceAll(':', '-')
targets.push(`${target}:${pubKey}:${hostname}:${service}`)
}

const content = [
Expand Down Expand Up @@ -38,8 +40,8 @@ async function readPromTargets (location) {

const aliases = new Map()
for (const target of fullJson[0].targets) {
const [alias, z32PubKey] = target.split(':')
aliases.set(alias, z32PubKey)
const [alias, z32PubKey, hostname, service] = target.split(':')
aliases.set(alias, { z32PubKey, hostname, service })
}

return aliases
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"dependencies": {
"b4a": "^1.6.6",
"debounceify": "^1.1.0",
"dht-prom-alias-rpc": "^0.0.1-alpha.0",
"dht-prom-client": "^0.0.1-alpha.9",
"dht-prom-alias-rpc": "^0.0.1-alpha.1",
"dht-prom-client": "^0.0.1-alpha.10",
"fastify": "^4.28.0",
"graceful-goodbye": "^1.3.0",
"hypercore-id-encoding": "^1.3.0",
Expand Down
14 changes: 11 additions & 3 deletions prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This is an example config.

global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_interval: 1s
evaluation_interval: 1s

# Source for relabeling approach: https://stackoverflow.com/questions/59866342/prometheus-dynamic-metrics-path
scrape_configs:
Expand All @@ -12,9 +12,17 @@ scrape_configs:
- 'targets.json'
relabel_configs:
- source_labels: [__address__]
regex: "(.+):.{52}" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
regex: "(.+):.{52}:.+" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "/scrape/$1/metrics" # Captured part + /metrics appendix
target_label: __metrics_path__ # => instead of default /metrics
- source_labels: [__address__]
replacement: "localhost:30000" # Replace with the port where the dht-prometheus http server runs
target_label: __address__
- source_labels: [__address__]
regex: ".+:.{52}:([^:]+):.+:" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "$1" # Captured part + /metrics appendix
target_label: hostname
- source_labels: [__address__]
regex: ".+:.{52}:[^:]+:(.+):" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "$1" # Captured part + /metrics appendix
target_label: service
17 changes: 13 additions & 4 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ function getClient (t, bootstrap, scraperPubKey, sharedSecret, { name = 'dummy'
idEnc.decode(scraperPubKey),
name,
sharedSecret,
{ bootstrap }
'my-service',
{ bootstrap, hostname: 'my-hostname' }
)

t.teardown(async () => {
Expand All @@ -349,12 +350,20 @@ scrape_configs:
- '${promTargetsLoc}'
relabel_configs:
- source_labels: [__address__]
regex: "(.+):.{52}" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the key
regex: "(.+):.{52}:.+" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "/scrape/$1/metrics" # Captured part + /metrics appendix
target_label: __metrics_path__ # => instead of default /metrics
- source_labels: [__address__]
replacement: "${bridgeHttpAddress}"
target_label: __address__ # => That's the actual address
replacement: "${bridgeHttpAddress}" # Replace with the port where the dht-prometheus http server runs
target_label: __address__
- source_labels: [__address__]
regex: ".+:.{52}:([^:]+):.+:" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "$1" # Captured part + /metrics appendix
target_label: hostname
- source_labels: [__address__]
regex: ".+:.{52}:[^:]+:(.+):" # Targets are structured as <targetname>:<target z32 key>, and at prometheus level we only need the target name
replacement: "$1" # Captured part + /metrics appendix
target_label: service
`

await fs.promises.writeFile(loc, content)
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ async function setup (t, bridgeOpts = {}) {
scraperPubKey,
'dummy',
sharedSecret,
{ bootstrap }
'my-service',
{ bootstrap, hostname: 'my-hostname' }
)

t.teardown(async () => {
Expand Down
Loading