Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/packages/gax/src/clientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export interface ClientOptions
* this option whenever it is set.
*/
enableTelemetryTracing?: boolean;
/**
* Whether to enable telemetry logging for the client.
*
* Logging is only enabled when `GOOGLE_SDK_NODE_EXPERIMENTAL_LOGGING` is enabled
* AND either the `GOOGLE_SDK_NODE_ENABLE_LOGGING` environment variable is enabled
* or a user passes `enableTelemetryLogging: true` in their client options. The
* `GOOGLE_SDK_NODE_ENABLE_LOGGING` environment variable overrides this option
* whenever it is set.
*/
enableTelemetryLogging?: boolean;
}

export interface Descriptors {
Expand Down
4 changes: 2 additions & 2 deletions core/packages/gax/src/createApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
traceAttempt,
ResendRecorder,
} from './observability/TracerHelper';
import {checkTelemetryEnabled} from './observability/util';
import {checkTracingEnabled} from './observability/util';
import {
StaticTraceContext,
DynamicTraceContext,
Expand Down Expand Up @@ -83,7 +83,7 @@ export function createApiCall(
// the following apiCaller will be used for all calls of this function...
const apiCaller = createAPICaller(settings, descriptor);

const tracingEnabled = checkTelemetryEnabled(settings);
const tracingEnabled = checkTracingEnabled(settings);
const staticArgs: StaticTraceContext | undefined = tracingEnabled
? resolveStaticTraceContext(settings)
: undefined;
Expand Down
9 changes: 9 additions & 0 deletions core/packages/gax/src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class GrpcClient {
private _servicePath?: string;
private _port?: number;
private _enableTelemetryTracing?: boolean;
private _enableTelemetryLogging?: boolean;

/**
* In rare cases users might need to deallocate all memory consumed by loaded protos.
Expand Down Expand Up @@ -166,6 +167,9 @@ export class GrpcClient {
this._enableTelemetryTracing = (
options as GrpcClientOptions
).enableTelemetryTracing;
this._enableTelemetryLogging = (
options as GrpcClientOptions
).enableTelemetryLogging;
}

/**
Expand Down Expand Up @@ -221,6 +225,8 @@ export class GrpcClient {
* @param {Object} headers - A dictionary of additional HTTP header name to
* its value.
* @param {boolean} [enableTelemetryTracing] - Flag to enable telemetry tracing.
* @param {unknown} [_legacyTelemetryInfo] - Unused legacy parameter kept for backward compatibility.
* @param {boolean} [enableTelemetryLogging] - Flag to enable telemetry logging.
* @return {Object} A mapping of method names to CallSettings.
*/
constructSettings(
Expand All @@ -231,6 +237,7 @@ export class GrpcClient {
enableTelemetryTracing?: boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_legacyTelemetryInfo?: unknown,
enableTelemetryLogging?: boolean,
) {
function buildMetadata(abTests: {}, moreHeaders: OutgoingHttpHeaders) {
const metadata: OutgoingHttpHeaders = {};
Expand Down Expand Up @@ -305,6 +312,8 @@ export class GrpcClient {
Status,
otherArgs,
enableTelemetryTracing ?? this._enableTelemetryTracing,
_legacyTelemetryInfo,
enableTelemetryLogging ?? this._enableTelemetryLogging,
);
}
Comment on lines 312 to 318

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a parameter mismatch when calling the imported constructSettings function from gax.ts. In gax.ts, the 6th parameter is apiName?: string, and enableTelemetryTracing is the 7th parameter.

By passing enableTelemetryTracing ?? this._enableTelemetryTracing as the 6th argument, it is mapped to apiName in gax.ts. Consequently, enableTelemetryTracing in gax.ts receives _legacyTelemetryInfo (the 7th argument), and enableTelemetryLogging receives undefined because it is not passed as the 9th argument.

You should explicitly pass undefined for the apiName parameter to align the arguments correctly.

    return constructSettings(
      this._servicePath!,
      clientConfig,
      configCommons,
      Status,
      otherArgs,
      undefined,
      enableTelemetryTracing ?? this._enableTelemetryTracing,
      _legacyTelemetryInfo,
      enableTelemetryLogging ?? this._enableTelemetryLogging,
    );


Expand Down
47 changes: 42 additions & 5 deletions core/packages/gax/src/gax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {warn} from './warnings';
import {GoogleError} from './googleError';
import {BundleOptions} from './bundlingCalls/bundleExecutor';
import {toLowerCamelCase} from './util';
import {checkTelemetryEnabled} from './observability/util';
import {checkTracingEnabled, checkLoggingEnabled} from './observability/util';
import {Status} from './status';
import {RequestType} from './apitypes';

Expand Down Expand Up @@ -173,6 +173,7 @@ export interface CallOptions {
apiName?: string;
retryRequestOptions?: RetryRequestOptions;
enableTelemetryTracing?: boolean;
enableTelemetryLogging?: boolean;
/**
* Internal context used by resumable upload methods. Populated by
* GAPIC-generated client libraries; do not set manually.
Expand All @@ -195,6 +196,7 @@ export class CallSettings {
apiName?: string;
retryRequestOptions?: RetryRequestOptions;
enableTelemetryTracing?: boolean;
enableTelemetryLogging?: boolean;
resumableUpload?: ResumableUploadContext;

/**
Expand Down Expand Up @@ -230,6 +232,7 @@ export class CallSettings {
this.apiName = settings.apiName ?? undefined;
this.retryRequestOptions = settings.retryRequestOptions;
this.enableTelemetryTracing = settings.enableTelemetryTracing;
this.enableTelemetryLogging = settings.enableTelemetryLogging;
this.resumableUpload =
'resumableUpload' in settings ? settings.resumableUpload : undefined;
}
Expand All @@ -256,6 +259,7 @@ export class CallSettings {
let apiName = this.apiName;
let retryRequestOptions = this.retryRequestOptions;
let enableTelemetryTracing = this.enableTelemetryTracing;
let enableTelemetryLogging = this.enableTelemetryLogging;
let resumableUpload = this.resumableUpload;

// If the user provides a timeout to the method, that timeout value will be used
Expand Down Expand Up @@ -315,6 +319,9 @@ export class CallSettings {
if ('enableTelemetryTracing' in options) {
enableTelemetryTracing = options.enableTelemetryTracing;
}
if ('enableTelemetryLogging' in options) {
enableTelemetryLogging = options.enableTelemetryLogging;
}
if ('resumableUpload' in options) {
resumableUpload = options.resumableUpload;
}
Expand All @@ -331,6 +338,7 @@ export class CallSettings {
apiName,
retryRequestOptions,
enableTelemetryTracing,
enableTelemetryLogging,
resumableUpload,
});
}
Expand Down Expand Up @@ -807,6 +815,9 @@ export interface ClientConfig {
* calls.
* @param {boolean} [enableTelemetryTracing] - Flag to enable telemetry tracing
* when passed in client options (or overridden by `GOOGLE_SDK_NODE_ENABLE_TRACING`).
* @param {unknown} [_legacyTelemetryInfo] - Unused legacy parameter kept for backward compatibility.
* @param {boolean} [enableTelemetryLogging] - Flag to enable telemetry logging
* when passed in client options (or overridden by `GOOGLE_SDK_NODE_ENABLE_LOGGING`).
* @return {Object} A mapping from method name to CallSettings, or null if the
* service is not found in the config.
*/
Expand All @@ -819,6 +830,7 @@ export function constructSettings(
enableTelemetryTracing?: boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_legacyTelemetryInfo?: unknown,
enableTelemetryLogging?: boolean,
) {
Comment on lines 830 to 834

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The shifting logic inside constructSettings (located in the function body) must be updated to support the new enableTelemetryLogging parameter. Currently, when status is omitted (e.g., in direct calls or unit tests where otherArgs is passed as the 5th argument), the shifting logic only shifts enableTelemetryTracing and otherArgs. It does not shift _legacyTelemetryInfo or enableTelemetryLogging. This causes enableTelemetryLogging to be mapped incorrectly to _legacyTelemetryInfo or lost entirely.

Please update the shifting logic inside the function body to also shift _legacyTelemetryInfo and enableTelemetryLogging:

  if (
    status &&
    !Object.prototype.hasOwnProperty.call(status, 'OK')
  ) {
    enableTelemetryLogging = _legacyTelemetryInfo as boolean;
    _legacyTelemetryInfo = enableTelemetryTracing;
    enableTelemetryTracing = otherArgs as boolean;
    otherArgs = status;
    status = undefined;
  }

const env: Record<string, string | undefined> =
typeof process === 'object' && typeof process.env === 'object'
Expand All @@ -827,7 +839,7 @@ export function constructSettings(
const envTracing = env.GOOGLE_SDK_NODE_ENABLE_TRACING?.trim();
const isEnvSet = envTracing !== undefined && envTracing !== '';

const isEnvEnabled = checkTelemetryEnabled();
const isEnvEnabled = checkTracingEnabled();

// Tracing is only enabled via GOOGLE_SDK_NODE_ENABLE_TRACING or when the user
// passes enableTelemetryTracing: true in their client options. If
Expand All @@ -836,6 +848,18 @@ export function constructSettings(
? isEnvEnabled
: Boolean(enableTelemetryTracing);

const experimentalLogging =
env.GOOGLE_SDK_NODE_EXPERIMENTAL_LOGGING?.trim().toLowerCase();
const isExperimentalLoggingEnabled =
experimentalLogging === 'true' || experimentalLogging === '1';
const envLogging = env.GOOGLE_SDK_NODE_ENABLE_LOGGING?.trim();
const isLoggingEnvSet = envLogging !== undefined && envLogging !== '';
const isLoggingEnvEnabled = checkLoggingEnabled();

const loggingRequested =
isExperimentalLoggingEnabled &&
(isLoggingEnvSet ? isLoggingEnvEnabled : Boolean(enableTelemetryLogging));

otherArgs = otherArgs || {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const defaults: any = {};
Expand Down Expand Up @@ -893,11 +917,24 @@ export function constructSettings(
bundleOptions: bundlingConfig
? createBundleOptions(bundlingConfig)
: null,
otherArgs: tracingRequested
? {...otherArgs, internalMethodName: methodName}
: otherArgs,
otherArgs:
tracingRequested || loggingRequested
? {
...otherArgs,
internalMethodName: methodName,
enableTelemetryTracing: tracingRequested,
enableTelemetryLogging: loggingRequested,
}
: otherArgs,
apiName,
enableTelemetryTracing: isEnvSet ? isEnvEnabled : enableTelemetryTracing,
enableTelemetryLogging: isExperimentalLoggingEnabled
? isLoggingEnvSet
? isLoggingEnvEnabled
: enableTelemetryLogging
: enableTelemetryLogging !== undefined || isLoggingEnvSet
? false
: undefined,
});
}

Expand Down
15 changes: 15 additions & 0 deletions core/packages/gax/src/grpc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/**
* Copyright 2020 Google LLC
*
Expand Down Expand Up @@ -63,6 +63,14 @@
* options.
*/
enableTelemetryTracing?: boolean;
/**
* Whether to enable telemetry logging for the client.
*
* Logging is only enabled when `GOOGLE_SDK_NODE_EXPERIMENTAL_LOGGING` is enabled
* AND either the `GOOGLE_SDK_NODE_ENABLE_LOGGING` environment variable is enabled
* or a user passes `enableTelemetryLogging: true` in their client options.
*/
enableTelemetryLogging?: boolean;
}

export interface MetadataValue {
Expand Down Expand Up @@ -134,6 +142,7 @@
private _servicePath?: string;
private _port?: number;
private _enableTelemetryTracing?: boolean;
private _enableTelemetryLogging?: boolean;
/**
* Base directory for resolving client certificates.
*
Expand Down Expand Up @@ -193,6 +202,7 @@
? parseInt(options.port, 10)
: options.port;
this._enableTelemetryTracing = options.enableTelemetryTracing;
this._enableTelemetryLogging = options.enableTelemetryLogging;

const minimumVersion = 10;
const major = Number(process.version.match(/^v(\d+)/)?.[1]);
Expand Down Expand Up @@ -396,6 +406,8 @@
* @param {Object} headers - A dictionary of additional HTTP header name to
* its value.
* @param {boolean} [enableTelemetryTracing] - Flag to enable telemetry tracing.
* @param {unknown} [_legacyTelemetryInfo] - Unused legacy parameter kept for backward compatibility.
* @param {boolean} [enableTelemetryLogging] - Flag to enable telemetry logging.
* @return {Object} A mapping of method names to CallSettings.
*/
constructSettings(
Expand All @@ -406,6 +418,7 @@
enableTelemetryTracing?: boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_legacyTelemetryInfo?: unknown,
enableTelemetryLogging?: boolean,
) {
const otherArgs: Record<string, unknown> = {
metadataBuilder: this.metadataBuilder(headers),
Expand All @@ -423,6 +436,8 @@
this.grpc.status,
otherArgs,
enableTelemetryTracing ?? this._enableTelemetryTracing,
_legacyTelemetryInfo,
enableTelemetryLogging ?? this._enableTelemetryLogging,
);
}
Comment on lines 436 to 442

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There is a parameter mismatch when calling the imported constructSettings function from gax.ts. In gax.ts, the 6th parameter is apiName?: string, and enableTelemetryTracing is the 7th parameter.

By passing enableTelemetryTracing ?? this._enableTelemetryTracing as the 6th argument, it is mapped to apiName in gax.ts. Consequently, enableTelemetryTracing in gax.ts receives _legacyTelemetryInfo (the 7th argument), and enableTelemetryLogging receives undefined because it is not passed as the 9th argument.

You should explicitly pass undefined for the apiName parameter to align the arguments correctly.

      this.grpc.status,
      otherArgs,
      undefined,
      enableTelemetryTracing ?? this._enableTelemetryTracing,
      _legacyTelemetryInfo,
      enableTelemetryLogging ?? this._enableTelemetryLogging,
    );


Expand Down
2 changes: 1 addition & 1 deletion core/packages/gax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export {

export {makeUUID, decodeAnyProtosInArray, decodeProtobufAny} from './util';

export {checkTelemetryEnabled} from './observability/util';
export {checkTracingEnabled} from './observability/util';
export {
StaticTraceContext,
resolveStaticTraceContext,
Expand Down
38 changes: 37 additions & 1 deletion core/packages/gax/src/observability/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {CallSettings} from '../gax';
* @param settings
* @returns true if telemetry tracing is enabled, false otherwise
*/
export function checkTelemetryEnabled(settings?: CallSettings): boolean {
export function checkTracingEnabled(settings?: CallSettings): boolean {
// `process` is undeclared in browsers and some edge runtimes, where reading
// it would throw a ReferenceError rather than yield undefined, so it is
// reached through a `typeof` guard and stands in as an empty environment.
Expand All @@ -47,3 +47,39 @@ export function checkTelemetryEnabled(settings?: CallSettings): boolean {
settings?.otherArgs?.enableTelemetryTracing,
);
}

/**
* Checks if telemetry logging is enabled.
*
* Logging is only enabled when `GOOGLE_SDK_NODE_EXPERIMENTAL_LOGGING` is enabled
* AND either the `GOOGLE_SDK_NODE_ENABLE_LOGGING` environment variable is enabled
* or a user passes `enableTelemetryLogging: true` in their client options.
* If `GOOGLE_SDK_NODE_ENABLE_LOGGING` is explicitly set, it takes precedence over
* the client option.
*
* @param settings
* @returns true if telemetry logging is enabled, false otherwise
*/
export function checkLoggingEnabled(settings?: CallSettings): boolean {
const env: Record<string, string | undefined> =
typeof process === 'object' && typeof process.env === 'object'
? process.env
: {};
const experimentalOptIn =
env.GOOGLE_SDK_NODE_EXPERIMENTAL_LOGGING?.trim().toLowerCase();
if (experimentalOptIn !== 'true' && experimentalOptIn !== '1') {
return false;
}

const envOptIn = env.GOOGLE_SDK_NODE_ENABLE_LOGGING?.trim();

if (envOptIn !== undefined && envOptIn !== '') {
const lower = envOptIn.toLowerCase();
return lower === 'true' || lower === '1';
}

return Boolean(
settings?.enableTelemetryLogging ||
settings?.otherArgs?.enableTelemetryLogging,
);
}
4 changes: 2 additions & 2 deletions core/packages/gax/test/unit/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ describe('exports', () => {
it('exports protobufMinimal', () => {
assert(typeof index.protobufMinimal === 'object');
});
it('exports checkTelemetryEnabled', () => {
assert(typeof index.checkTelemetryEnabled === 'function');
it('exports checkTracingEnabled', () => {
assert(typeof index.checkTracingEnabled === 'function');
});
it('exports resolveStaticTraceContext', () => {
assert(typeof index.resolveStaticTraceContext === 'function');
Expand Down
Loading
Loading