Skip to content

Commit 178b19f

Browse files
feat: Add support for partialSuccess global configuration (#1359)
* 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>
1 parent eda9661 commit 178b19f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/log.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface LogOptions {
6161
maxEntrySize?: number; // see: https://cloud.google.com/logging/quotas
6262
jsonFieldsToTruncate?: string[];
6363
defaultWriteDeleteCallback?: ApiResponseCallback;
64+
partialSuccess?: boolean;
6465
}
6566

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

126131
constructor(logging: Logging, name: string, options?: LogOptions) {
127132
options = options || {};
@@ -170,6 +175,13 @@ class Log implements LogSeverityFunctions {
170175
* was set by user and only for APIs which does not accept a callback as parameter
171176
*/
172177
this.defaultWriteDeleteCallback = options.defaultWriteDeleteCallback;
178+
179+
/**
180+
Turning partialSuccess by default to be true if not provided in options. This should improve
181+
overall logging reliability since only oversized entries will be dropped
182+
from request. See {@link https://cloud.google.com/logging/quotas#log-limits} for more info
183+
*/
184+
this.partialSuccess = options.partialSuccess ?? true;
173185
}
174186

175187
/**
@@ -967,9 +979,10 @@ class Log implements LogSeverityFunctions {
967979
// Extract & format additional context from individual entries. Make sure to add instrumentation info
968980
const info = populateInstrumentationInfo(entry);
969981
const decoratedEntries = this.decorateEntries(info[0]);
970-
// If instrumentation info was added make sure we set partialSuccess, so entire
971-
// request will make it through and only oversized entries will be dropped if any
972-
if (info[1]) {
982+
// If instrumentation info was added or this.partialSuccess was set, make sure we set
983+
// partialSuccess in outgoing write request, so entire request will make it through and
984+
// only oversized entries will be dropped if any
985+
if (info[1] || (options.partialSuccess ?? this.partialSuccess)) {
973986
options.partialSuccess = true;
974987
}
975988
this.truncateEntries(decoratedEntries);

test/log.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ describe('Log', () => {
379379
{
380380
logName: log.formattedName_,
381381
entries: ENTRIES,
382+
partialSuccess: true,
382383
resource: {
383384
labels: {
384385
project_id: 'fake-project',
@@ -449,6 +450,7 @@ describe('Log', () => {
449450
{
450451
logName: log.formattedName_,
451452
entries: ENTRIES,
453+
partialSuccess: true,
452454
resource: EXPECTED_RESOURCE,
453455
},
454456
undefined,
@@ -464,6 +466,7 @@ describe('Log', () => {
464466
{
465467
logName: log.formattedName_,
466468
entries: ENTRIES,
469+
partialSuccess: true,
467470
resource: FAKE_RESOURCE,
468471
},
469472
undefined,
@@ -480,6 +483,7 @@ describe('Log', () => {
480483
{
481484
logName: log.formattedName_,
482485
entries: ENTRIES,
486+
partialSuccess: true,
483487
resource: FAKE_RESOURCE,
484488
},
485489
undefined,
@@ -534,10 +538,10 @@ describe('Log', () => {
534538
it('should not require options', async () => {
535539
await log.write(ENTRY);
536540
assert(
537-
log.logging.loggingService.writeLogEntries.calledOnceWithExactly(
538-
sinon.match.object,
539-
undefined,
540-
undefined
541+
log.logging.loggingService.writeLogEntries.calledOnceWith(
542+
sinon.match({
543+
partialSuccess: true,
544+
})
541545
)
542546
);
543547
});

0 commit comments

Comments
 (0)