Skip to content

Commit

Permalink
add optional log param to GetEntriesRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman committed Mar 28, 2019
1 parent 4d05e81 commit 8d94e03
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ class Logging {
* empty filter matches all log entries.
* @property {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
* @property {string} [log] A name of the log specifying to pnly return
* entries from this log.
* @property {number} [maxApiCalls] Maximum number of API calls to make.
* @property {number} [maxResults] Maximum number of items plus prefixes to
* return.
Expand Down Expand Up @@ -580,10 +582,14 @@ class Logging {
userStream.once('reading', () => {
this.auth.getProjectId().then(projectId => {
this.projectId = projectId;
if (options.logName_) {
options.filter =
`logName="${Log.formatName_(this.projectId, options.logName_)}"`;
delete options.logName_;
if (options.log) {
options = extend(
{
filter:
`logName="${Log.formatName_(this.projectId, options.log)}"`,
},
options);
delete options.log;
}
const reqOpts = extend(
{
Expand Down
4 changes: 2 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export interface GetEntriesRequest {
autoPaginate?: boolean;
filter?: string;
gaxOptions?: CallOptions;
log?: string;
maxApiCalls?: number;
maxResults?: number;
orderBy?: string;
pageSize?: number;
pageToken?: string;
resourceNames?: string[]|string;
logName_?: string;
}

export interface LogOptions {
Expand Down Expand Up @@ -563,7 +563,7 @@ class Log implements LogSeverityFunctions {
getEntriesStream(options: GetEntriesRequest) {
options = extend(
{
logName_: this.name,
log: this.name,
},
options);
return this.logging.getEntriesStream(options);
Expand Down
32 changes: 32 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as assert from 'assert';
import * as extend from 'extend';
import * as proxyquire from 'proxyquire';
import * as through from 'through2';
import {Logging as LOGGING} from '../src/index';

import {GetEntriesCallback} from '../src/index';
import {getOrInjectContext} from '../src/middleware/context';
Expand Down Expand Up @@ -98,6 +99,7 @@ class FakeLog {
constructor() {
this.calledWith_ = arguments;
}
// formatName_ = util.noop
}

class FakeSink {
Expand Down Expand Up @@ -576,6 +578,36 @@ describe('Logging', () => {
stream.emit('reading');
});

it('should set logName filter if has logName flag', done => {
const logName = 'log-name';
logging = new LOGGING({projectId: PROJECT_ID});
logging.loggingService.listLogEntriesStream = (reqOpts, gaxOpts) => {
assert.deepStrictEqual(reqOpts, {
resourceNames: ['projects/' + logging.projectId],
orderBy: 'timestamp desc',
a: 'b',
c: 'd',
filter: `logName="${
['projects', PROJECT_ID, 'logs', encodeURIComponent(logName)]
.join('/')}"`,
});

assert.deepStrictEqual(gaxOpts, {
autoPaginate: undefined,
a: 'b',
c: 'd',
});

setImmediate(done);

return GAX_STREAM;
};

const log = logging.log('log-name');
const stream = log.getEntriesStream(OPTIONS);
stream.emit('reading');
});

it('should destroy request stream if gax fails', done => {
const error = new Error('Error.');
logging.loggingService.listLogEntriesStream = () => {
Expand Down
2 changes: 1 addition & 1 deletion test/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe('Log', () => {
describe('getEntriesStream', () => {
const fakeStream = {};
const EXPECTED_OPTIONS = {
logName_: LOG_NAME_ENCODED,
log: LOG_NAME_ENCODED,
};

it('should call Logging getEntriesStream with defaults', done => {
Expand Down

0 comments on commit 8d94e03

Please sign in to comment.