Skip to content

Commit

Permalink
fix: Subscribe to node metrics model before publishing to it (#3260)
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody authored Jul 24, 2024
1 parent 536f79c commit 3a3bb99
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"clean": "npx rimraf ./lib"
},
"dependencies": {
"@ceramicnetwork/node-metrics": "^1.0.3",
"@ceramicnetwork/streamid": "^5.6.0",
"@didtools/cacao": "^3.0.0",
"@didtools/key-webauthn": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@ceramicnetwork/indexing": "^5.2.0",
"@ceramicnetwork/ipfs-topology": "^6.2.0",
"@ceramicnetwork/job-queue": "^5.2.0",
"@ceramicnetwork/node-metrics": "^1.0.3",
"@ceramicnetwork/node-metrics": "^1.0.5",
"@ceramicnetwork/observability": "^1.4.4",
"@ceramicnetwork/pinning-aggregation": "^6.2.0",
"@ceramicnetwork/pinning-ipfs-backend": "^6.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/recon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('ReconApi', () => {
await reconApi.registerInterest(MODEL)
expect(mockSendRequest).toHaveBeenCalledWith(
`${RECON_URL}/ceramic/interests/model/${MODEL.toString()}`,
{ method: 'POST' }
{ method: 'POST', body: {}, headers: { 'Content-Type': 'application/json' } }
)
})
})
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
private _signer: CeramicSigner
public readonly dispatcher: Dispatcher
public readonly loggerProvider: LoggerProvider
public readonly recon: IReconApi
public readonly admin: AdminApi
public readonly feed: PublicFeed
readonly repository: Repository
Expand Down Expand Up @@ -329,6 +330,7 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
)
const pinApi = new LocalPinApi(this.repository, this._logger)
this.repository.index.setSyncQueryApi(this.syncApi)
this.recon = modules.reconApi
this.admin = new LocalAdminApi(
this._logger,
localIndex,
Expand Down Expand Up @@ -562,7 +564,6 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
}

await this._startupChecks()

await this._startMetrics()
} catch (err) {
this._logger.err(err)
Expand Down Expand Up @@ -614,6 +615,12 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
// If authenticated into the node, we can start publishing metrics
// publishing metrics is enabled by default, even if no metrics config
if (this._metricsConfig?.metricsPublisherEnabled) {
// First, subscribe the node to the Model used for NodeMetrics
const metricsModel = NodeMetrics.getModel(this._networkOptions.name)
await this.repository.index.indexModels([{ streamID: metricsModel }])
await this.recon.registerInterest(metricsModel, this.did.id)

// Now start the NodeMetrics system.
const ipfsVersion = await this.ipfs.version()
const ipfsId = await this.ipfs.id()

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/recon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface ReconEventFeedResponse {
*/
export interface IReconApi extends Observable<ReconEventFeedResponse> {
init(initialCursor?: string, initialInterests?: Array<StreamID>): Promise<void>
registerInterest(model: StreamID): Promise<void>
registerInterest(model: StreamID, controller?: string): Promise<void>
put(car: CAR, opts?: AbortOptions): Promise<void>
enabled: boolean
stop(): void
Expand Down Expand Up @@ -122,14 +122,19 @@ export class ReconApi extends Observable<ReconEventFeedResponse> implements IRec
/**
* Registers interest in a model
* @param model stream id of the model to register interest in
* @param controller restrict the interest range to just events with this controller
*/
async registerInterest(model: StreamID): Promise<void> {
async registerInterest(model: StreamID, controller?: string): Promise<void> {
if (!this.enabled) {
throw new Error(`Recon: disabled, not registering interest in model ${model.toString()}`)
}
try {
const headers = { 'Content-Type': 'application/json' }
const body = { ...(controller && { controller }) }
await this.#sendRequest(this.#url + `/ceramic/interests/model/${model.toString()}`, {
method: 'POST',
headers,
body,
})
this.#logger.debug(`Recon: added interest for model ${model.toString()}`)
} catch (err) {
Expand Down

0 comments on commit 3a3bb99

Please sign in to comment.