Skip to content

Commit

Permalink
Add hostname and service labels (#12)
Browse files Browse the repository at this point in the history
* Add hostname and service labels

* Update prometheus example config

* No logs
  • Loading branch information
HDegroote authored Jul 28, 2024
1 parent 6858f34 commit a0d7e42
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
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

0 comments on commit a0d7e42

Please sign in to comment.