-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Showing
7 changed files
with
119 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './mutex' | ||
export * from './stream' | ||
export * from './async-abort-controller' |
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
2 changes: 1 addition & 1 deletion
2
src/internal/concurrency/stream.ts → src/internal/streams/byte-counter.ts
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,3 @@ | ||
export * from './stream-speed' | ||
export * from './byte-counter' | ||
export * from './monitor' |
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,42 @@ | ||
import { createByteCounterStream } from './byte-counter' | ||
import { monitorStreamSpeed } from './stream-speed' | ||
import { trace } from '@opentelemetry/api' | ||
import { Readable } from 'node:stream' | ||
|
||
/** | ||
* Monitor readable streams by tracking their speed and bytes read | ||
* @param dataStream | ||
*/ | ||
export function monitorStream(dataStream: Readable) { | ||
const speedMonitor = monitorStreamSpeed(dataStream) | ||
const byteCounter = createByteCounterStream() | ||
|
||
let measures: number[] = [] | ||
|
||
// Handle the 'speed' event to collect speed measurements | ||
speedMonitor.on('speed', (bps) => { | ||
measures.push(bps) | ||
const span = trace.getActiveSpan() | ||
span?.setAttributes({ 'stream.speed': measures, bytesRead: byteCounter.bytes }) | ||
}) | ||
|
||
speedMonitor.on('close', () => { | ||
measures = [] | ||
const span = trace.getActiveSpan() | ||
span?.setAttributes({ uploadRead: byteCounter.bytes }) | ||
}) | ||
|
||
// Handle errors by cleaning up and destroying the downstream stream | ||
speedMonitor.on('error', (err) => { | ||
// Destroy the byte counter stream with the error | ||
byteCounter.transformStream.destroy(err) | ||
}) | ||
|
||
// Ensure the byteCounter stream ends when speedMonitor ends | ||
speedMonitor.on('end', () => { | ||
byteCounter.transformStream.end() | ||
}) | ||
|
||
// Return the piped stream | ||
return speedMonitor.pipe(byteCounter.transformStream) | ||
} |
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,51 @@ | ||
import { Readable } from 'stream' | ||
import { PassThrough } from 'node:stream' | ||
|
||
/** | ||
* Keep track of a stream's speed | ||
* @param stream | ||
* @param frequency | ||
*/ | ||
/** | ||
* Keep track of a stream's speed | ||
* @param stream | ||
* @param frequency | ||
*/ | ||
export function monitorStreamSpeed(stream: Readable, frequency = 1000) { | ||
let totalBytes = 0 | ||
const startTime = Date.now() | ||
|
||
const passThrough = new PassThrough() | ||
|
||
const interval = setInterval(() => { | ||
const currentTime = Date.now() | ||
const elapsedTime = (currentTime - startTime) / 1000 | ||
const currentSpeedBytesPerSecond = totalBytes / elapsedTime | ||
|
||
passThrough.emit('speed', currentSpeedBytesPerSecond) | ||
}, frequency) | ||
|
||
passThrough.on('data', (chunk) => { | ||
totalBytes += chunk.length | ||
}) | ||
|
||
const cleanup = () => { | ||
clearInterval(interval) | ||
passThrough.removeAllListeners('speed') | ||
} | ||
|
||
// Handle close event to ensure cleanup | ||
passThrough.on('close', cleanup) | ||
|
||
// Propagate errors from the source stream to the passThrough | ||
stream.on('error', (err) => { | ||
passThrough.destroy(err) | ||
}) | ||
|
||
// Ensure the passThrough ends when the source stream ends | ||
stream.on('end', () => { | ||
passThrough.end() | ||
}) | ||
|
||
return stream.pipe(passThrough) | ||
} |
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