Skip to content

Commit

Permalink
feat: Publish js-ceramic and C1 versions as a metric once per hour (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody authored Jun 21, 2024
1 parent e7feb63 commit 6124192
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/cli/src/ceramic-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {
DEFAULT_TRACE_SAMPLE_RATIO,
ServiceMetrics as Metrics,
} from '@ceramicnetwork/observability'
import {
DEFAULT_PUBLISH_INTERVAL_MS,
NodeMetrics,
Observable,
} from '@ceramicnetwork/node-metrics'
import { DEFAULT_PUBLISH_INTERVAL_MS, NodeMetrics, Observable } from '@ceramicnetwork/node-metrics'
import { IpfsConnectionFactory } from './ipfs-connection-factory.js'
import {
DiagnosticsLogger,
Expand Down Expand Up @@ -288,6 +284,7 @@ export class CeramicDaemon {
)

const [modules, params] = Ceramic._processConfig(ipfs, ceramicConfig)
params.versionInfo = { cliPackageVersion: version, gitHash: commitHash }
const diagnosticsLogger = modules.loggerProvider.getDiagnosticsLogger()
diagnosticsLogger.imp(
`Starting Ceramic Daemon with @ceramicnetwork/cli package version ${version}, with js-ceramic repo git hash ${commitHash}, and with config: \n${JSON.stringify(
Expand All @@ -298,7 +295,9 @@ export class CeramicDaemon {
)
const ipfsId = await ipfs.id()
diagnosticsLogger.imp(
`Connecting to IPFS node available as ${ipfsId.addresses.map(String).join(', ')}`
`Connecting to IPFS node with version "${ipfsId.agentVersion}" available as ${ipfsId.addresses
.map(String)
.join(', ')}`
)

const ceramic = new Ceramic(modules, params)
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ const DEFAULT_LOAD_OPTS = { sync: SyncOptions.PREFER_CACHE }
export const DEFAULT_STATE_STORE_DIRECTORY = path.join(os.homedir(), '.ceramic', 'statestore')
const ERROR_LOADING_STREAM = 'error_loading_stream'
const ERROR_APPLYING_COMMIT = 'error_applying_commit'
const VERSION_INFO = 'version_info'

const PUBLISH_VERSION_INTERVAL_MS = 1000 * 60 * 60 // once per hour

/**
* Ceramic configuration
Expand Down Expand Up @@ -148,6 +151,11 @@ export interface CeramicModules {
reconApi: IReconApi
}

interface VersionInfo {
cliPackageVersion: string
gitHash: string
}

/**
* Parameters that control internal Ceramic behavior.
* Most users will not provide this directly but will let it be derived automatically from the
Expand All @@ -161,6 +169,7 @@ export interface CeramicParameters {
networkOptions: CeramicNetworkOptions
loadOptsOverride: LoadOpts
anchorLoopMinDurationMs?: number
versionInfo?: VersionInfo
}

const normalizeStreamID = (streamId: StreamID | string): StreamID => {
Expand Down Expand Up @@ -220,6 +229,8 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
private readonly _kvFactory: IKVFactory
private readonly _runId: string
private readonly _startTime: Date
private readonly _versionInfo: VersionInfo
private _versionMetricInterval: NodeJS.Timer | undefined = undefined

constructor(modules: CeramicModules, params: CeramicParameters) {
this._signer = modules.signer
Expand All @@ -236,6 +247,7 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
this._readOnly = params.readOnly
this._networkOptions = params.networkOptions
this._loadOptsOverride = params.loadOptsOverride
this._versionInfo = params.versionInfo
this._runId = crypto.randomUUID()
this._startTime = new Date()

Expand Down Expand Up @@ -510,13 +522,27 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
}

await this._startupChecks()

this._versionMetricInterval = setInterval(
this._publishVersionMetrics.bind(this),
PUBLISH_VERSION_INTERVAL_MS
)
} catch (err) {
this._logger.err(err)
await this.close()
throw err
}
}

async _publishVersionMetrics() {
const ipfsVersion = (await this.ipfs.id()).agentVersion
Metrics.observe(VERSION_INFO, 1, {
jsCeramicVersion: this._versionInfo.cliPackageVersion,
jsCeramicGitHash: this._versionInfo.gitHash,
ceramicOneVersion: ipfsVersion,
})
}

/**
* Runs some checks at node startup to ensure that the node is healthy and properly configured.
* Throws an Error if any issues are detected
Expand Down Expand Up @@ -903,6 +929,7 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
*/
async close(): Promise<void> {
this._logger.imp('Closing Ceramic instance')
clearInterval(this._versionMetricInterval)
await this.anchorService.close()
this._shutdownSignal.abort()
await this.syncApi.shutdown()
Expand Down

0 comments on commit 6124192

Please sign in to comment.