Skip to content

Commit

Permalink
add capability to set an instance identifier which labels all metrics (
Browse files Browse the repository at this point in the history
…#1198)

* use latest version of observability with otlp updates, reduce export interval

* for cas server, set an instance identifier from the ecs container

* tell the linter that it will be a string, for reals

* the linter wants to be very careful about regexp logic maybe

* fix typos

* do not escape / inside character class

* add test coverage for some lines

* add a test for presence of the instanceIdentifier

* set the instance identifier in the deployment, use in the code

* make tests reflect expected reality
  • Loading branch information
gvelez17 authored Apr 24, 2024
1 parent dc77998 commit fa093e3
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ RUN npm install

WORKDIR /

COPY entrypoint.sh .
RUN chmod +x ./entrypoint.sh

COPY runner.sh .

ENTRYPOINT ["./entrypoint.sh"]
CMD [ "./runner.sh" ]
1 change: 1 addition & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"collectorHost": "",
"exportIntervalMillis": 30000,
"exportTimeoutMillis": 20000,
"instanceIdentifier": "",
"prometheusPort": 0
},
"db": {
Expand Down
1 change: 1 addition & 0 deletions config/env/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"traceRatio": "@@METRICS_TRACE_RATIO",
"exportIntervalMillis": "@@METRICS_EXPORT_INTERVAL_MS",
"exportTimeoutMillis": "@@METRICS_EXPORT_TIMEOUT_MS",
"instanceIdentifier": "@@INSTANCE_IDENTIFIER",
"prometheusPort": "@@METRICS_PORT"
},
"db": {
Expand Down
1 change: 1 addition & 0 deletions config/env/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"traceRatio": "@@METRICS_TRACE_RATIO",
"exportIntervalMillis": "@@METRICS_EXPORT_INTERVAL_MS",
"exportTimeoutMillis": "@@METRICS_EXPORT_TIMEOUT_MS",
"instanceIdentifier": "@@INSTANCE_IDENTIFIER",
"prometheusPort": "@@METRICS_PORT"
},
"db": {
Expand Down
1 change: 1 addition & 0 deletions config/env/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"traceRatio": "@@METRICS_TRACE_RATIO",
"exportIntervalMillis": "@@METRICS_EXPORT_INTERVAL_MS",
"exportTimeoutMillis": "@@METRICS_EXPORT_TIMEOUT_MS",
"instanceIdentifier": "@@INSTANCE_IDENTIFIER",
"prometheusPort": "@@METRICS_PORT"
},
"db": {
Expand Down
14 changes: 14 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

instance_uri=$ECS_CONTAINER_METADATA_URI

# Parse the last portion of the URI
instance_id=${instance_uri##*/}

# Remove everything after the hyphen to get the task ID
instance_id=${instance_id%%-*}

# if present, will be the task ID, otherwise empty string
export INSTANCE_IDENTIFIER=$instance_id

exec "$@"
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@ceramicnetwork/common": "^2.28.0-rc.0",
"@ceramicnetwork/core": "^2.35.0-rc.0",
"@ceramicnetwork/logger": "^2.5.0",
"@ceramicnetwork/observability": "^1.4.6",
"@ceramicnetwork/observability": "^1.5.0",
"@ceramicnetwork/streamid": "^2.15.0-rc.0",
"@ceramicnetwork/wasm-bloom-filter": "^0.1.0",
"@overnightjs/core": "^1.7.6",
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/ceramic_integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,42 @@ describe('CAR file', () => {
await casIPFS.stop()
})
})

describe('Metrics Options', () => {
test('cas starts with a typical instance identifier', async () => {
const ipfsApiPort = await getPort()
const casIPFS = await createIPFS(ipfsApiPort)
const ganacheServer = await makeGanache()
const dbConnection = await createDbConnection()
const casPort = await getPort()
const cas = await makeCAS(createInjector(), dbConnection, {
mode: 'server',
ipfsPort: ipfsApiPort,
ganachePort: ganacheServer.port,
port: casPort,
useSmartContractAnchors: true,
metrics: {
instanceIdentifier: '234fffffffffffffffffffffffffffffffff9726129'
}
})
await cas.start()
// Teardown
await cas.stop()

const cas2 = await makeCAS(createInjector(), dbConnection, {
mode: 'server',
ipfsPort: ipfsApiPort,
ganachePort: ganacheServer.port,
port: casPort,
useSmartContractAnchors: true,
metrics: {
instanceIdentifier: ''
}
})
await cas2.start()
await cas2.stop()

await ganacheServer.close()
await casIPFS.stop()
})
})
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export class CeramicAnchorApp {
)
Metrics.count('HELLO', 1)
logger.imp('Metrics exporter started')
if (this.config.metrics.instanceIdentifier) {
Metrics.setInstanceIdentifier(this.config.metrics.instanceIdentifier)
}
} catch (e: any) {
logger.imp('ERROR: Metrics exporter failed to start. Continuing anyway.')
logger.err(e)
Expand Down

0 comments on commit fa093e3

Please sign in to comment.