Skip to content

Commit

Permalink
feat(canary): publish canary metrics (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
arein authored Feb 8, 2024
1 parent dc8d834 commit d0afcd3
Show file tree
Hide file tree
Showing 4 changed files with 4,821 additions and 3,441 deletions.
1 change: 1 addition & 0 deletions apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"wagmi": "2.5.0"
},
"devDependencies": {
"@aws-sdk/client-cloudwatch": "3.509.0",
"@mailsac/api": "1.0.5",
"@playwright/test": "1.40.1",
"dotenv": "16.3.1",
Expand Down
19 changes: 19 additions & 0 deletions apps/laboratory/tests/canary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { DEFAULT_SESSION_PARAMS } from './shared/constants'
import { testMW } from './shared/fixtures/w3m-wallet-fixture'
import { uploadCanaryResultsToCloudWatch } from './shared/utils/metrics'

const ENV = process.env['ENV'] || 'dev'
const REGION = process.env['REGION'] || 'eu-central-1'

let startTime = 0

testMW.beforeEach(
async ({ modalPage, walletPage, modalValidator, walletValidator, browserName }) => {
startTime = Date.now()
// Canary doesn't need all platforms
if (browserName !== 'chromium' || modalPage.library !== 'ethers') {
return
Expand Down Expand Up @@ -38,5 +45,17 @@ testMW(
await walletValidator.expectReceivedSign({})
await walletPage.handleRequest({ accept: true })
await modalValidator.expectAcceptedSign()

if (ENV !== 'dev') {
const duration: number = Date.now() - startTime
await uploadCanaryResultsToCloudWatch(
ENV,
REGION,
'https://lab.web3modal.com/',
'HappyPath.sign',
true,
duration
)
}
}
)
87 changes: 87 additions & 0 deletions apps/laboratory/tests/shared/utils/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
CloudWatch,
type MetricDatum,
type PutMetricDataCommandInput
} from '@aws-sdk/client-cloudwatch'

// eslint-disable-next-line func-style
export const uploadCanaryResultsToCloudWatch = async (
env: string,
region: string,
target: string,
metricsPrefix: string,
isTestPassed: boolean,
testDurationMs: number
// eslint-disable-next-line max-params
) => {
const cloudwatch = new CloudWatch({ region: 'eu-central-1' })
const ts = new Date()
const metrics: MetricDatum[] = [
{
MetricName: `${metricsPrefix}.success`,
Dimensions: [
{
Name: 'Target',
Value: target
},
{
Name: 'Region',
Value: region
}
],
Unit: 'Count',
Value: isTestPassed ? 1 : 0,
Timestamp: ts
},
{
MetricName: `${metricsPrefix}.failure`,
Dimensions: [
{
Name: 'Target',
Value: target
},
{
Name: 'Region',
Value: region
}
],
Unit: 'Count',
Value: isTestPassed ? 0 : 1,
Timestamp: ts
}
]

if (isTestPassed) {
metrics.push({
MetricName: `${metricsPrefix}.latency`,
Dimensions: [
{
Name: 'Target',
Value: target
},
{
Name: 'Region',
Value: region
}
],
Unit: 'Milliseconds',
Value: testDurationMs,
Timestamp: ts
})
}

const params: PutMetricDataCommandInput = {
MetricData: metrics,
Namespace: `${env}_Canary_Web3Modal`
}

await new Promise<void>(resolve => {
cloudwatch.putMetricData(params, (err: Error) => {
if (err) {
// eslint-disable-next-line no-console
console.error('Failed to upload metrics to CloudWatch', err, err.stack)
}
resolve()
})
})
}
Loading

0 comments on commit d0afcd3

Please sign in to comment.