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

Remove enabled from telemetry #980

Merged
merged 3 commits into from
Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ironfish/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class IronfishNode {
strategyClass = strategyClass || Strategy
const strategy = new strategyClass(workerPool)

const telemetry = new Telemetry(config, workerPool, logger, [
const telemetry = new Telemetry(workerPool, logger, [
{ name: 'node_id', value: internal.get('telemetryNodeId') },
{ name: 'session_id', value: uuid() },
{ name: 'version', value: pkg.version },
Expand Down
26 changes: 8 additions & 18 deletions ironfish/src/telemetry/telemetry.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { mockLogger, mockWorkerPool } from '../testUtilities/mocks'
import { Metric } from './interfaces/metric'
import { Telemetry } from './telemetry'

Expand All @@ -10,22 +11,6 @@ import { Telemetry } from './telemetry'
describe('Telemetry', () => {
let telemetry: Telemetry

const mockTelemetry = (enabled = true): Telemetry => {
/* eslint-disable @typescript-eslint/no-explicit-any */
const mockConfig: any = {
get: jest.fn().mockResolvedValueOnce(enabled),
}
const mockPool: any = {
submitTelemetry: jest.fn(),
}
const mockLogger: any = {
debug: jest.fn(),
error: jest.fn(),
}
/* eslint-enable @typescript-eslint/no-explicit-any */
return new Telemetry(mockConfig, mockPool, mockLogger, [])
}

const mockMetric: Metric = {
measurement: 'node',
name: 'memory',
Expand All @@ -39,7 +24,12 @@ describe('Telemetry', () => {
}

beforeEach(() => {
telemetry = mockTelemetry()
telemetry = new Telemetry(mockWorkerPool(), mockLogger(), [])
telemetry.start()
})

afterEach(() => {
telemetry?.stop()
})

describe('stop', () => {
Expand All @@ -55,7 +45,7 @@ describe('Telemetry', () => {
describe('submit', () => {
describe('when disabled', () => {
it('does nothing', () => {
const disabledTelemetry = mockTelemetry(false)
const disabledTelemetry = new Telemetry(mockWorkerPool(), mockLogger(), [])
const currentPoints = disabledTelemetry['points']
disabledTelemetry.submit(mockMetric)
expect(disabledTelemetry['points']).toEqual(currentPoints)
Expand Down
26 changes: 16 additions & 10 deletions ironfish/src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { Config } from '../fileStores'
import { Logger } from '../logger'
import { Block } from '../primitives/block'
import { renderError, SetIntervalToken } from '../utils'
Expand All @@ -13,39 +12,46 @@ export class Telemetry {
private readonly FLUSH_INTERVAL = 5000
private readonly MAX_QUEUE_SIZE = 1000

private readonly enabled: boolean
private readonly defaultTags: Tag[]
private readonly logger: Logger
private readonly pool: WorkerPool

private started: boolean
private flushInterval: SetIntervalToken | null
private points: Metric[]

constructor(config: Config, pool: WorkerPool, logger: Logger, defaultTags: Tag[]) {
this.enabled = config.get('enableTelemetry')
constructor(pool: WorkerPool, logger: Logger, defaultTags: Tag[]) {
this.logger = logger
this.pool = pool
this.defaultTags = defaultTags

this.started = false
this.flushInterval = null
this.points = []
}

start(): void {
if (this.enabled) {
void this.flushLoop()
if (this.started) {
return
}

this.started = true
void this.flushLoop()
}

async stop(): Promise<void> {
if (this.enabled) {
this.submitNodeStopped()
await this.flush()
if (!this.started) {
return
}

this.started = false

if (this.flushInterval) {
clearTimeout(this.flushInterval)
}

this.submitNodeStopped()
await this.flush()
}

async flushLoop(): Promise<void> {
Expand All @@ -57,7 +63,7 @@ export class Telemetry {
}

submit(metric: Metric): void {
if (!this.enabled) {
if (!this.started) {
return
}

Expand Down
10 changes: 9 additions & 1 deletion ironfish/src/testUtilities/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ export function mockSyncer(): any {
}
}

function mockWorkerPool(): unknown {
export function mockLogger(): any {
return {
debug: jest.fn(),
error: jest.fn(),
}
}

export function mockWorkerPool(): any {
return {
saturated: jest.fn(),
submitTelemetry: jest.fn(),
}
}