-
Notifications
You must be signed in to change notification settings - Fork 11
feat(logs): add logs fetching and streaming #23
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ef49fb3
feat(logs): new function to only load one page of logs
philnash 515cd41
feat(logs): adds a streaming class for logs that polls for now
philnash aa4da1b
feat(logs): adds convenience func, page size and function filter
philnash d3eb255
chore: updates package.json
philnash bc62e97
refactor(./src/streams/logs.ts): logsStream refactor
philnash 466b8d8
feat(./src/api/logs.ts): adds filters to listOnePageLogResources
philnash 2832315
feat(client): adds getLogs function to client
philnash 020c8c6
improvement(logs): turn filters for logs into an object argument
philnash 2c25c75
improvement(logs): make polling frequency settable for log streams
philnash 7208356
improvement(logs): in objectmode readable streams can push objects
philnash f8d35be
improvement(logs): removed unused _buffer variable
philnash 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Readable } from 'stream'; | ||
import { listOnePageLogResources } from '../api/logs'; | ||
import { LogApiResource, Sid, GotClient } from '../types'; | ||
import { LogsConfig } from '../types/logs'; | ||
|
||
export class LogsStream extends Readable { | ||
private _pollingFrequency: number; | ||
private _interval: NodeJS.Timeout | undefined; | ||
private _viewedSids: Set<Sid>; | ||
|
||
constructor( | ||
private environmentSid: Sid, | ||
private serviceSid: Sid, | ||
private client: GotClient, | ||
private config: LogsConfig | ||
) { | ||
super({ objectMode: true }); | ||
this._interval = undefined; | ||
this._viewedSids = new Set(); | ||
this._pollingFrequency = config.pollingFrequency || 1000; | ||
} | ||
|
||
set pollingFrequency(frequency: number) { | ||
this._pollingFrequency = frequency; | ||
if (this.config.tail && this._interval) { | ||
clearInterval(this._interval); | ||
this._interval = setInterval(() => { | ||
this._poll(); | ||
}, this._pollingFrequency); | ||
} | ||
} | ||
|
||
async _poll() { | ||
try { | ||
const logs = await listOnePageLogResources( | ||
this.environmentSid, | ||
this.serviceSid, | ||
this.client, | ||
{ | ||
functionSid: this.config.filterByFunction, | ||
pageSize: this.config.limit, | ||
} | ||
); | ||
logs | ||
.filter(log => !this._viewedSids.has(log.sid)) | ||
.reverse() | ||
.forEach(log => { | ||
this.push(log); | ||
}); | ||
// Replace the set each time rather than adding to the set. | ||
philnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This way the set is always the size of a page of logs and the next page | ||
// will either overlap or not. This is instead of keeping an ever growing | ||
// set of viewSids which would cause memory issues for long running log | ||
// tails. | ||
this._viewedSids = new Set(logs.map(log => log.sid)); | ||
if (!this.config.tail) { | ||
this.push(null); | ||
} | ||
} catch (err) { | ||
this.destroy(err); | ||
} | ||
} | ||
|
||
_read() { | ||
if (this.config.tail && !this._interval) { | ||
this._interval = setInterval(() => { | ||
this._poll(); | ||
}, this._pollingFrequency); | ||
} else { | ||
this._poll(); | ||
} | ||
} | ||
|
||
_destroy() { | ||
if (this._interval) { | ||
clearInterval(this._interval); | ||
this._interval = undefined; | ||
} | ||
} | ||
} |
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,20 @@ | ||
/** @module @twilio-labs/serverless-api */ | ||
|
||
import { Sid } from './serverless-api'; | ||
|
||
export type LogsConfig = { | ||
serviceSid: Sid; | ||
environment: string | Sid; | ||
tail: boolean; | ||
limit?: number; | ||
filterByFunction?: string | Sid; | ||
pollingFrequency?: number; | ||
}; | ||
|
||
export type LogFilters = { | ||
pageSize?: number; | ||
functionSid?: Sid; | ||
startDate?: string | Date; | ||
endDate?: string | Date; | ||
pageToken?: string; | ||
}; |
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.