Skip to content

Commit

Permalink
feat: Add support for partialSuccess global configuration (#1359)
Browse files Browse the repository at this point in the history
* feat: Add support for partialSuccess global configuration

* 馃 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
losalex and gcf-owl-bot[bot] committed Oct 27, 2022
1 parent eda9661 commit 178b19f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface LogOptions {
maxEntrySize?: number; // see: https://cloud.google.com/logging/quotas
jsonFieldsToTruncate?: string[];
defaultWriteDeleteCallback?: ApiResponseCallback;
partialSuccess?: boolean;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -95,6 +96,9 @@ export type DeleteCallback = ApiResponseCallback;
* Note that {@link LogOptions#defaultWriteDeleteCallback} is useful when {@link Log#write} and {@link Log#delete} APIs are called
* without `await` and without callback added explicitly to every call - this way {@link LogOptions#defaultWriteDeleteCallback}
* can serve as global callback handler, which for example could be used to catch all errors and eliminate crashes.
* @param {boolean} [options.partialSuccess] Global flag indicating Whether a batch's valid entries should be written even if
* some other entry failed due to errors. Default is true.
* See {@link https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write#body.request_body.FIELDS.partial_success|partialSuccess} for more info.
* @example
* ```
* import {Logging} from '@google-cloud/logging';
Expand Down Expand Up @@ -122,6 +126,7 @@ class Log implements LogSeverityFunctions {
name: string;
jsonFieldsToTruncate: string[];
defaultWriteDeleteCallback?: ApiResponseCallback;
partialSuccess: boolean;

constructor(logging: Logging, name: string, options?: LogOptions) {
options = options || {};
Expand Down Expand Up @@ -170,6 +175,13 @@ class Log implements LogSeverityFunctions {
* was set by user and only for APIs which does not accept a callback as parameter
*/
this.defaultWriteDeleteCallback = options.defaultWriteDeleteCallback;

/**
Turning partialSuccess by default to be true if not provided in options. This should improve
overall logging reliability since only oversized entries will be dropped
from request. See {@link https://cloud.google.com/logging/quotas#log-limits} for more info
*/
this.partialSuccess = options.partialSuccess ?? true;
}

/**
Expand Down Expand Up @@ -967,9 +979,10 @@ class Log implements LogSeverityFunctions {
// Extract & format additional context from individual entries. Make sure to add instrumentation info
const info = populateInstrumentationInfo(entry);
const decoratedEntries = this.decorateEntries(info[0]);
// If instrumentation info was added make sure we set partialSuccess, so entire
// request will make it through and only oversized entries will be dropped if any
if (info[1]) {
// If instrumentation info was added or this.partialSuccess was set, make sure we set
// partialSuccess in outgoing write request, so entire request will make it through and
// only oversized entries will be dropped if any
if (info[1] || (options.partialSuccess ?? this.partialSuccess)) {
options.partialSuccess = true;
}
this.truncateEntries(decoratedEntries);
Expand Down
12 changes: 8 additions & 4 deletions test/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ describe('Log', () => {
{
logName: log.formattedName_,
entries: ENTRIES,
partialSuccess: true,
resource: {
labels: {
project_id: 'fake-project',
Expand Down Expand Up @@ -449,6 +450,7 @@ describe('Log', () => {
{
logName: log.formattedName_,
entries: ENTRIES,
partialSuccess: true,
resource: EXPECTED_RESOURCE,
},
undefined,
Expand All @@ -464,6 +466,7 @@ describe('Log', () => {
{
logName: log.formattedName_,
entries: ENTRIES,
partialSuccess: true,
resource: FAKE_RESOURCE,
},
undefined,
Expand All @@ -480,6 +483,7 @@ describe('Log', () => {
{
logName: log.formattedName_,
entries: ENTRIES,
partialSuccess: true,
resource: FAKE_RESOURCE,
},
undefined,
Expand Down Expand Up @@ -534,10 +538,10 @@ describe('Log', () => {
it('should not require options', async () => {
await log.write(ENTRY);
assert(
log.logging.loggingService.writeLogEntries.calledOnceWithExactly(
sinon.match.object,
undefined,
undefined
log.logging.loggingService.writeLogEntries.calledOnceWith(
sinon.match({
partialSuccess: true,
})
)
);
});
Expand Down

0 comments on commit 178b19f

Please sign in to comment.