Skip to content

Commit d995477

Browse files
committed
refactor: update countEvent to handle optional metric names and normalize event names
1 parent bb72908 commit d995477

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/eventHandlers/countEvent.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
import type { DomainEventHandler } from '#domain/eventPublisher'
12
import { metrics } from '@sentry/browser'
23

3-
export const countEvent = (metricName: string) => () => {
4-
if (__METRICS__) metrics.count(metricName, 1)
5-
}
4+
const normalizeEventName = (eventName: string): string =>
5+
eventName.replace(/:/g, '.')
6+
7+
export const countEvent =
8+
(metricName?: string): DomainEventHandler<IDomainEvent> =>
9+
e => {
10+
if (__METRICS__)
11+
if (metricName !== undefined && metricName !== '')
12+
metrics.count(metricName, 1)
13+
else
14+
metrics.count(normalizeEventName(e.name), 1, {
15+
attributes: { event: { type: 'domain' } },
16+
})
17+
}

src/eventHandlers/countEvnet.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import { DomainEvent } from '#domain/events/base'
2+
import { MockEventPublisher } from '#mocks/eventPublisher'
13
import { countEvent } from './countEvent'
24
import { metrics } from '@sentry/browser'
35

46
describe('countEvent', () => {
7+
class DummyDomainEvent extends DomainEvent {
8+
constructor() {
9+
super('dummy:domain:event' as DomainEvent['name'])
10+
}
11+
}
12+
13+
const eventPublisher = new MockEventPublisher()
14+
515
beforeAll(() => {
616
Object.assign(global, { __METRICS__: true })
717
})
@@ -14,9 +24,21 @@ describe('countEvent', () => {
1424
const mockMetricCount = jest.fn()
1525
jest.spyOn(metrics, 'count').mockImplementationOnce(mockMetricCount)
1626

17-
countEvent('dummy')()
27+
countEvent('dummy')(new DummyDomainEvent(), eventPublisher)
1828

1929
expect(mockMetricCount).toHaveBeenCalledOnce()
2030
expect(mockMetricCount).toHaveBeenCalledWith('dummy', 1)
2131
})
32+
33+
it('can count event with normalized domain event name when no metric name provided', async () => {
34+
const mockMetricCount = jest.fn()
35+
jest.spyOn(metrics, 'count').mockImplementationOnce(mockMetricCount)
36+
37+
countEvent()(new DummyDomainEvent(), eventPublisher)
38+
39+
expect(mockMetricCount).toHaveBeenCalledOnce()
40+
expect(mockMetricCount).toHaveBeenCalledWith('dummy.domain.event', 1, {
41+
attributes: { event: { type: 'domain' } },
42+
})
43+
})
2244
})

src/serviceWorker/initEventPublisher.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,16 @@ const initEventPublisher = (eventPublisher?: DomainEventPublisher) => {
154154
},
155155
})
156156
)
157-
.register('filename:overwritten', countEvent('filename.overwritten'))
157+
.register('filename:overwritten', countEvent())
158158
.register(
159159
'notification:filenameOverwritten:diagnoseButton:clicked',
160-
countEvent('notification.filenameOverwritten.diagnoseButton.clicked')
160+
countEvent()
161161
)
162162
.register(
163163
'notification:filenameOverwritten:ignoreButton:clicked',
164-
countEvent('notification.filenameOverwritten.ignoreButton.clicked')
165-
)
166-
.register(
167-
'notification:filenameOverwritten:self:clicked',
168-
countEvent('notification.filenameOverwritten.self.clicked')
164+
countEvent()
169165
)
166+
.register('notification:filenameOverwritten:self:clicked', countEvent())
170167
}
171168
}
172169

0 commit comments

Comments
 (0)