Skip to content

Commit

Permalink
Capture action plugin metrics (#971)
Browse files Browse the repository at this point in the history
Just like how we capture metrics relating to performance of classic integrations, this patch now enables capturing of metrics related to action destinations as well.
  • Loading branch information
zikaari authored Oct 18, 2023
1 parent d8e922d commit 2f1ae75
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-drinks-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Capture action plugin metrics
2 changes: 1 addition & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"size-limit": [
{
"path": "dist/umd/index.js",
"limit": "28.9 KB"
"limit": "29 KB"
}
],
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unfetch from 'unfetch'
import { PluginFactory } from '..'

import { AnalyticsBrowser } from '../../../browser'
import { createSuccess } from '../../../test-helpers/factories'

jest.mock('unfetch')
jest.mocked(unfetch).mockImplementation(() =>
createSuccess({
integrations: {},
remotePlugins: [
{
name: 'testDestination',
libraryName: 'testDestination',
settings: {
subscriptions: [
{
name: 'Track Calls',
enabled: true,
partnerAction: 'trackEvent',
subscribe: 'type = "track"',
},
],
},
},
],
})
)

const testDestination: PluginFactory = () => {
return {
name: 'testDestination',
version: '1.0.0',
type: 'destination',
isLoaded: () => true,
load: () => Promise.resolve(),
track: (ctx) => Promise.resolve(ctx),
}
}

testDestination.pluginName = 'testDestination'

describe('ActionDestination', () => {
it('captures essential metrics when invoking methods on an action plugin', async () => {
const ajs = AnalyticsBrowser.load({
writeKey: 'abc',
plugins: [testDestination],
})

await ajs.ready()

expect(ajs.ctx?.stats.metrics[0]).toMatchObject(
expect.objectContaining({
metric: 'analytics_js.action_plugin.invoke',
tags: ['method:load', 'action_plugin_name:testDestination'],
})
)

const trackCtx = await ajs.track('test')

const actionInvokeMetric = trackCtx.stats.metrics.find(
(m) => m.metric === 'analytics_js.action_plugin.invoke'
)

expect(actionInvokeMetric).toMatchObject(
expect.objectContaining({
metric: 'analytics_js.action_plugin.invoke',
tags: ['method:track', 'action_plugin_name:testDestination'],
})
)
})
})
34 changes: 31 additions & 3 deletions packages/browser/src/plugins/remote-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ export class ActionDestination implements DestinationPlugin {
transformedContext = await this.transform(ctx)
}

await this.action[methodName]!(transformedContext)
try {
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
`method:${methodName}`,
`action_plugin_name:${this.action.name}`,
])

await this.action[methodName]!(transformedContext)
} catch (error) {
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
`method:${methodName}`,
`action_plugin_name:${this.action.name}`,
])

throw error
}

return ctx
}
Expand All @@ -101,8 +115,22 @@ export class ActionDestination implements DestinationPlugin {
return this.action.ready ? this.action.ready() : Promise.resolve()
}

load(ctx: Context, analytics: Analytics): Promise<unknown> {
return this.action.load(ctx, analytics)
async load(ctx: Context, analytics: Analytics): Promise<unknown> {
try {
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
`method:load`,
`action_plugin_name:${this.action.name}`,
])

return await this.action.load(ctx, analytics)
} catch (error) {
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
`method:load`,
`action_plugin_name:${this.action.name}`,
])

throw error
}
}

unload(ctx: Context, analytics: Analytics): Promise<unknown> | unknown {
Expand Down

0 comments on commit 2f1ae75

Please sign in to comment.