-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6db2c83
commit 026c8fd
Showing
14 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Analytics Plugin <!-- #omit in toc --> | ||
|
||
Send some analytics data to Datadog internally. | ||
|
||
It gives you acces to the `context.sendLog()` function. | ||
|
||
```typescript | ||
// Send a basic log. | ||
context.sendLog('My basic log'); | ||
|
||
// Send some context with the log. | ||
context.sendLog('My contextual log', { some: 'context' }); | ||
``` | ||
|
||
Every log already has some context to it: | ||
|
||
```typescript | ||
{ | ||
ddsource: string; // Name of the bundler plugin (e.g. `@datadog/webpack-plugin`). | ||
env: Env; // Environment (e.g. `production`). | ||
message; // The log message. | ||
service: 'build-plugins'; | ||
bundler: { | ||
name: string; // Name of the bundler (e.g. `webpack`). | ||
version: string; // Version of the bundler. | ||
}; | ||
plugins: PluginName[]; // List of the plugins/features enabled. | ||
version: string; // Version of the plugin. | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@dd/internal-analytics-plugin", | ||
"packageManager": "[email protected]", | ||
"license": "MIT", | ||
"private": true, | ||
"author": "Datadog", | ||
"description": "Send some analytics data to Datadog internally.", | ||
"homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/analytics#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/DataDog/build-plugins", | ||
"directory": "packages/plugins/analytics" | ||
}, | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./*": "./src/*.ts" | ||
}, | ||
"scripts": { | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@dd/core": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import type { PluginName } from '@dd/core/types'; | ||
|
||
export const PLUGIN_NAME: PluginName = 'datadog-analytics-plugin' as const; | ||
export const INTAKE_PATH = 'v1/input/pub44d5f4eb86e1392037b7501f7adc540e'; | ||
export const INTAKE_HOST = 'browser-http-intake.logs.datadoghq.com'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import { doRequest } from '@dd/core/helpers'; | ||
import type { GlobalContext, PluginOptions } from '@dd/core/types'; | ||
|
||
import { INTAKE_HOST, INTAKE_PATH, PLUGIN_NAME } from './constants'; | ||
|
||
export { PLUGIN_NAME } from './constants'; | ||
|
||
export const getAnalyticsPlugins = (context: GlobalContext): PluginOptions[] => { | ||
const log = context.getLogger(PLUGIN_NAME); | ||
|
||
context.sendLog = async (message: string, ctx: any = {}) => { | ||
// Only send logs in production. | ||
if (context.env !== 'production') { | ||
return; | ||
} | ||
|
||
try { | ||
const bundler = { | ||
name: context.bundler.name, | ||
version: context.bundler.version, | ||
}; | ||
|
||
await doRequest({ | ||
// Don't delay the build too much on error. | ||
retries: 2, | ||
minTimeout: 100, | ||
url: `https://${INTAKE_HOST}/${INTAKE_PATH}`, | ||
method: 'POST', | ||
type: 'json', | ||
getData: async () => { | ||
const data = { | ||
ddsource: `@datadog/${bundler.name}-plugin`, | ||
env: context.env, | ||
message, | ||
service: 'build-plugins', | ||
bundler, | ||
plugins: context.pluginNames, | ||
version: context.version, | ||
team: 'yoann', | ||
...ctx, | ||
}; | ||
return { | ||
data: JSON.stringify(data), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
}, | ||
}); | ||
} catch (e: any) { | ||
// We don't want to break anything in case of error. | ||
log.debug(`Could not submit data to Datadog: ${e.message}`); | ||
} | ||
}; | ||
|
||
return [ | ||
{ | ||
name: PLUGIN_NAME, | ||
async buildStart() { | ||
// Send a log. | ||
await context.sendLog('Build started'); | ||
}, | ||
}, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"rootDir": "./", | ||
"outDir": "./dist" | ||
}, | ||
"include": ["**/*"], | ||
"exclude": ["dist", "node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2019-Present Datadog, Inc. | ||
|
||
import { INTAKE_HOST, INTAKE_PATH } from '@dd/internal-analytics-plugin/constants'; | ||
import { BUNDLERS, runBundlers } from '@dd/tests/_jest/helpers/runBundlers'; | ||
import nock from 'nock'; | ||
|
||
describe('Analytics Plugin', () => { | ||
describe('Without network error.', () => { | ||
test('Should submit the data.', async () => { | ||
const replyMock = jest.fn(() => ({})); | ||
|
||
nock(`https://${INTAKE_HOST}`) | ||
// Intercept logs submissions. | ||
.post(`/${INTAKE_PATH}`) | ||
.times(BUNDLERS.length) | ||
.reply(200, replyMock); | ||
|
||
await runBundlers({ | ||
customPlugins: (options, context) => { | ||
// Change the env so we DO send the logs. | ||
context.env = 'production'; | ||
return []; | ||
}, | ||
}); | ||
|
||
expect(replyMock).toHaveBeenCalledTimes(BUNDLERS.length); | ||
|
||
nock.cleanAll(); | ||
}); | ||
}); | ||
|
||
describe('With a network error.', () => { | ||
beforeAll(async () => { | ||
nock(`https://${INTAKE_HOST}`) | ||
// Intercept logs submissions. | ||
.post(`/${INTAKE_PATH}`) | ||
// Reply with an error. | ||
.reply(500, 'Network error.') | ||
.persist(); | ||
}); | ||
|
||
afterAll(async () => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
test('Should not throw.', async () => { | ||
const { errors } = await runBundlers(); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters