-
Notifications
You must be signed in to change notification settings - Fork 36
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
chore: lazy load internal request module #519
Merged
duncanista
merged 18 commits into
main
from
jordan.gonzalez/lazy-load-fallback-processor
Mar 20, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2616948
remove `post` and `get` exports from `metrics/index.ts`
duncanista 0fe7ab0
switch `post` import from `utils/request`
duncanista b16ef2f
remove local flush from extension code
duncanista 2a43b0b
lazy load `request` to remove 1 MB dep
duncanista 90f652e
update import from `extension`
duncanista 2d668b2
rename `agent` to `extension`
duncanista 1903994
update imports and move method to private function
duncanista 64d7a3b
update imports and add await
duncanista c9eed0e
fix tests
duncanista 2794f19
move tests from extension to listener test flush
duncanista 8585f47
lint and format
duncanista ddb577a
Merge branch 'main' into jordan.gonzalez/lazy-load-fallback-processor
duncanista 47efaa6
update snapshots?
duncanista 99154ff
Merge branch 'jordan.gonzalez/lazy-load-fallback-processor' of ssh://…
duncanista 6cc0816
solve merge conflicts
duncanista 84e0751
bring back local flush to `extension.ts`
duncanista fab865b
update tests again
duncanista e46ee85
fix error which patched twice the http/https modules
duncanista 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 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
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { StatsD } from "hot-shots"; | ||
import { promisify } from "util"; | ||
import { logDebug, logError } from "../utils"; | ||
import { APIClient } from "./api"; | ||
import { flushExtension, isExtensionRunning } from "./extension"; | ||
import { KMSService } from "./kms-service"; | ||
import { writeMetricToStdout } from "./metric-log"; | ||
import { Distribution } from "./model"; | ||
import { Processor } from "./processor"; | ||
|
||
const metricsBatchSendIntervalMS = 10000; // 10 seconds | ||
const METRICS_BATCH_SEND_INTERVAL = 10000; // 10 seconds | ||
|
||
export interface MetricsConfig { | ||
/** | ||
|
@@ -56,7 +54,7 @@ export interface MetricsConfig { | |
} | ||
|
||
export class MetricsListener { | ||
private currentProcessor?: Promise<Processor>; | ||
private currentProcessor?: Promise<any>; | ||
private apiKey: Promise<string>; | ||
private statsDClient?: StatsD; | ||
private isExtensionRunning?: boolean = undefined; | ||
|
@@ -83,6 +81,7 @@ export class MetricsListener { | |
|
||
return; | ||
} | ||
|
||
this.currentProcessor = this.createProcessor(this.config, this.apiKey); | ||
} | ||
|
||
|
@@ -120,15 +119,11 @@ export class MetricsListener { | |
logError("failed to flush metrics", error as Error); | ||
} | ||
} | ||
try { | ||
if (this.isExtensionRunning && this.config.localTesting) { | ||
logDebug(`Flushing Extension for local test`); | ||
await flushExtension(); | ||
} | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
logError("failed to flush extension", error as Error); | ||
} | ||
|
||
// Flush only when testing extension locally. | ||
// Passing config flag so we can lazy load the request module. | ||
if (this.isExtensionRunning) { | ||
await flushExtension(this.config.localTesting); | ||
} | ||
this.currentProcessor = undefined; | ||
} | ||
|
@@ -171,12 +166,17 @@ export class MetricsListener { | |
} | ||
|
||
private async createProcessor(config: MetricsConfig, apiKey: Promise<string>) { | ||
const key = await apiKey; | ||
const url = `https://api.${config.siteURL}`; | ||
const apiClient = new APIClient(key, url); | ||
const processor = new Processor(apiClient, metricsBatchSendIntervalMS, config.shouldRetryMetrics); | ||
processor.startProcessing(); | ||
return processor; | ||
if (!this.isExtensionRunning && !this.config.logForwarding) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
const { APIClient } = require("./api"); | ||
const { Processor } = require("./processor"); | ||
|
||
const key = await apiKey; | ||
const url = `https://api.${config.siteURL}`; | ||
const apiClient = new APIClient(key, url); | ||
const processor = new Processor(apiClient, METRICS_BATCH_SEND_INTERVAL, config.shouldRetryMetrics); | ||
processor.startProcessing(); | ||
return processor; | ||
} | ||
} | ||
|
||
private async getAPIKey(config: MetricsConfig) { | ||
|
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 |
---|---|---|
|
@@ -33,6 +33,8 @@ export function unpatchHttp() { | |
} | ||
|
||
function patchMethod(mod: typeof http | typeof https, method: "get" | "request", contextService: TraceContextService) { | ||
if (mod[method].__wrapped !== undefined) return; // Only patch once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was missed, required so no repeated logs appear. |
||
|
||
shimmer.wrap(mod, method, (original) => { | ||
const fn = (arg1: any, arg2: any, arg3: any) => { | ||
[arg1, arg2, arg3] = addTraceContextToArgs(contextService, arg1, arg2, arg3); | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, this has to stay as it, so we don't import and load the modules that add 1MB of memory allocation.