-
Notifications
You must be signed in to change notification settings - Fork 8
[logging] Add meta reporting #134
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, overrides: 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: 'language-foundations', | ||
...overrides, | ||
}; | ||
return { | ||
data: JSON.stringify(data), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
}, | ||
}); | ||
} catch (e: unknown) { | ||
// We don't want to break anything in case of error. | ||
log.debug(`Could not submit data to Datadog: ${e}`); | ||
} | ||
}; | ||
|
||
return [ | ||
{ | ||
name: PLUGIN_NAME, | ||
async buildStart() { | ||
// Send a log. | ||
await context.sendLog('Build started'); | ||
}, | ||
}, | ||
]; | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.