Skip to content
155 changes: 103 additions & 52 deletions core/packages/gax/src/createApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import {
traceAttempt,
ResendRecorder,
} from './observability/TracerHelper';
import {checkTracingEnabled} from './observability/util';
import {logCall, logNetworkAttempt} from './observability/LoggerHelper';
import {checkLoggingEnabled, checkTracingEnabled} from './observability/util';
import {
StaticTraceContext,
DynamicTraceContext,
AttemptTraceContext,
resolveStaticTraceContext,
} from './observability/contextUtil';
Expand Down Expand Up @@ -84,23 +84,19 @@ export function createApiCall(
const apiCaller = createAPICaller(settings, descriptor);

const tracingEnabled = checkTracingEnabled(settings);
const staticArgs: StaticTraceContext | undefined = tracingEnabled
? resolveStaticTraceContext(settings)
: undefined;
const loggingEnabled = checkLoggingEnabled(settings);
const staticArgs: StaticTraceContext | undefined =
tracingEnabled || loggingEnabled
? resolveStaticTraceContext(settings)
: undefined;
const serviceName = settings.apiName?.split('.').pop() ?? '';
const isFallback = Boolean(_fallback);
const dynamicArgs: DynamicTraceContext | undefined = tracingEnabled
? {
clientName: serviceName ? `${serviceName}Client` : '',
methodName: settings.otherArgs?.internalMethodName ?? '',
rpcType: isFallback ? 'http' : 'grpc',
}
: undefined;
// Context for per-attempt low level network CLIENT spans.
const attemptDynamicArgs: AttemptTraceContext | undefined =
tracingEnabled && dynamicArgs
const dynamicArgs: AttemptTraceContext | undefined =
tracingEnabled || loggingEnabled
? {
...dynamicArgs,
clientName: serviceName ? `${serviceName}Client` : '',
methodName: settings.otherArgs?.internalMethodName ?? '',
rpcType: isFallback ? 'http' : 'grpc',
apiName: settings.apiName ?? '',
}
: undefined;
Expand All @@ -110,8 +106,10 @@ export function createApiCall(
callOptions?: CallOptions,
callback?: APICallback,
) => {
// Capture the active client request span context to parent async low level network attempt spans.
const parentContext = tracingEnabled ? context.active() : undefined;
// Capture the active OpenTelemetry context to parent async attempt spans and preserve
// outer span correlation on attempt logs when context does not propagate across async gaps.
const parentContext =
tracingEnabled || loggingEnabled ? context.active() : undefined;
let currentApiCaller = apiCaller;

let thisSettings: CallSettings;
Expand Down Expand Up @@ -181,33 +179,64 @@ export function createApiCall(
?.streaming;

// Wrap the transport call so each attempt (initial send and retries) emits a low level network CLIENT span.
if (tracingEnabled && attemptDynamicArgs && staticArgs) {
if ((tracingEnabled || loggingEnabled) && dynamicArgs && staticArgs) {
const callerWrappedFunc = wrappedFunc;
wrappedFunc = (
argument: {},
metadata: {},
options: {},
attemptCallback: APICallback,
): GRPCCallResult => {
if (!isRetryAttempt) {
attemptResendCount = 0;
let attemptExecution = (cb: APICallback): GRPCCallResult =>
Reflect.apply(callerWrappedFunc, undefined, [
argument,
metadata,
options,
cb,
]);

// If logging is enabled, wrap with L4 attempt error logging.
// When tracing is disabled, restore `parentContext` around the attempt so
// `logNetworkAttempt` preserves the caller's outer OpenTelemetry span even
// without an async ContextManager across `funcPromise.then` or retry timers.
if (loggingEnabled) {
const originalAttemptExecution = attemptExecution;
attemptExecution = (cb: APICallback) => {
const runLoggedAttempt = () =>
logNetworkAttempt(
dynamicArgs,
staticArgs,
loggedCallback =>
originalAttemptExecution(loggedCallback ?? cb),
Boolean(streaming),
cb,
);
return !tracingEnabled && parentContext
? context.with(parentContext, runLoggedAttempt)
: runLoggedAttempt();
};
}
Comment thread
shivanee-p marked this conversation as resolved.
Resolved
isRetryAttempt = false;
const resendCount = attemptResendCount;
return traceAttempt(
{...attemptDynamicArgs, resendCount},
staticArgs,
tracedAttemptCallback =>
Reflect.apply(callerWrappedFunc, undefined, [
argument,
metadata,
options,
tracedAttemptCallback ?? attemptCallback,
]),
Boolean(streaming),
attemptCallback,
parentContext,
);

if (tracingEnabled) {
if (!isRetryAttempt) {
attemptResendCount = 0;
}
isRetryAttempt = false;
const resendCount = attemptResendCount;
const originalAttemptExecution = attemptExecution;
attemptExecution = (cb: APICallback) =>
traceAttempt(
{...dynamicArgs, resendCount},
staticArgs,
tracedAttemptCallback =>
originalAttemptExecution(tracedAttemptCallback ?? cb),
Boolean(streaming),
cb,
parentContext,
);
}

return attemptExecution(attemptCallback);
};
}

Expand Down Expand Up @@ -279,27 +308,49 @@ export function createApiCall(
return currentApiCaller.result(ongoingCall);
};

if (tracingEnabled && dynamicArgs && staticArgs) {
if ((tracingEnabled || loggingEnabled) && dynamicArgs && staticArgs) {
const isStreamingCall = apiCaller instanceof StreamingApiCaller;
return (
request: RequestType,
callOptions?: CallOptions,
callback?: APICallback,
) => {
return traceCall(
dynamicArgs,
staticArgs,
(tracedCallback?: APICallback) => {
// `traceCall` wraps the user's callback whenever one was supplied,
// for stream and non-stream calls alike, and that wrapper is what
// closes the span. It is undefined only when there is no callback to
// wrap, in which case the span is bound to the returned promise or
// stream instead; the fallback keeps this correct either way.
return invokeCall(request, callOptions, tracedCallback ?? callback);
},
isStreamingCall,
callback,
);
let callExecution = (cb?: APICallback) =>
invokeCall(request, callOptions, cb);

// Wrap with L3 terminal logging
if (loggingEnabled) {
const originalCallExecution = callExecution;
callExecution = (cb?: APICallback) =>
logCall(
dynamicArgs,
staticArgs,
loggedCallback => originalCallExecution(loggedCallback ?? cb),
isStreamingCall,
cb,
);
}

if (tracingEnabled) {
const originalCallExecution = callExecution;
callExecution = (cb?: APICallback) =>
traceCall(
dynamicArgs,
staticArgs,
(tracedCallback?: APICallback) => {
// `traceCall` wraps the user's callback whenever one was supplied,
// for stream and non-stream calls alike, and that wrapper is what
// closes the span. It is undefined only when there is no callback to
// wrap, in which case the span is bound to the returned promise or
// stream instead; the fallback keeps this correct either way.
return originalCallExecution(tracedCallback ?? cb);
},
isStreamingCall,
cb,
);
}

return callExecution(callback);
};
} else {
return invokeCall;
Expand Down
22 changes: 22 additions & 0 deletions core/packages/gax/src/iamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class IamClient {
},
options,
) as ClientOptions & ClientStubOptions;
if (!opts.fallback && gaxGrpc.fallback) {
opts.fallback = gaxGrpc.fallback;
}
version = opts.fallback ? fallback.version : version;
opts.scopes = (this.constructor as typeof IamClient).scopes;
// Save options to use in initialize() method.
Expand Down Expand Up @@ -91,7 +94,24 @@ export class IamClient {
gapicConfig as gax.ClientConfig,
opts!.clientConfig || {},
{'x-goog-api-client': clientHeader.join(' ')},
opts.enableTelemetryTracing,
undefined,
opts.enableTelemetryLogging,
);
const servicePath = opts.servicePath || opts.apiEndpoint;
const parsedPort =
typeof opts.port === 'string' ? parseInt(opts.port, 10) : opts.port;
for (const methodName of Object.keys(this._defaults)) {
const otherArgs = this._defaults[methodName].otherArgs;
if (otherArgs) {
if (servicePath) {
otherArgs.servicePath = servicePath;
}
if (parsedPort !== undefined && !Number.isNaN(parsedPort)) {
otherArgs.port = parsedPort;
}
}
}
this.innerApiCalls = {};
}

Expand Down Expand Up @@ -145,6 +165,7 @@ export class IamClient {
innerCallPromise,
this._defaults[methodName],
this.descriptors.page[methodName],
this._opts.fallback || this.gaxGrpc.fallback,
);
}
return this.iamPolicyStub;
Expand Down Expand Up @@ -384,6 +405,7 @@ export class IamClient {
close(): Promise<void> {
this.initialize().catch(console.error);
if (!this._terminated) {
// eslint-disable-next-line promise/always-return
return this.iamPolicyStub!.then(stub => {
this._terminated = true;
stub.close();
Expand Down
38 changes: 33 additions & 5 deletions core/packages/gax/src/locationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,27 @@ export class LocationsClient {
// Ensure that options include all the required fields.
this.gaxGrpc = gaxGrpc;
const staticMembers = this.constructor as typeof LocationsClient;
const gaxGrpcEndpoint = gaxGrpc as unknown as {
_servicePath?: string;
_port?: number;
};
const servicePath =
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
opts?.servicePath ||
opts?.apiEndpoint ||
gaxGrpcEndpoint._servicePath ||
staticMembers.servicePath;
this._providedCustomServicePath = !!(
opts?.servicePath || opts?.apiEndpoint
opts?.servicePath ||
opts?.apiEndpoint ||
gaxGrpcEndpoint._servicePath
);
const port = opts?.port || staticMembers.port;
const port = opts?.port ?? gaxGrpcEndpoint._port ?? staticMembers.port;
const clientConfig = opts?.clientConfig ?? {};
const fallback =
opts?.fallback ??
(typeof window !== 'undefined' && typeof window?.fetch === 'function');
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);
(gaxGrpc.fallback ||
(typeof window !== 'undefined' && typeof window?.fetch === 'function'));
opts = Object.assign({clientConfig}, opts, {servicePath, port, fallback});

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
Expand Down Expand Up @@ -168,7 +178,23 @@ export class LocationsClient {
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
{'x-goog-api-client': clientHeader.join(' ')},
opts.enableTelemetryTracing,
undefined,
opts.enableTelemetryLogging,
);
const parsedPort =
typeof opts.port === 'string' ? parseInt(opts.port, 10) : opts.port;
for (const methodName of Object.keys(this._defaults)) {
const otherArgs = this._defaults[methodName].otherArgs;
if (otherArgs) {
if (opts.servicePath) {
otherArgs.servicePath = opts.servicePath;
}
if (parsedPort !== undefined && !Number.isNaN(parsedPort)) {
otherArgs.port = parsedPort;
}
}
}

// Set up a dictionary of "inner API calls"; the core implementation
// of calling the API is handled in `google-gax`, with this code
Expand Down Expand Up @@ -232,6 +258,7 @@ export class LocationsClient {
callPromise,
this._defaults[methodName],
descriptor,
this._opts.fallback || this.gaxGrpc.fallback,
);

this.innerApiCalls[methodName] = apiCall;
Expand Down Expand Up @@ -521,6 +548,7 @@ export class LocationsClient {
close(): Promise<void> {
this.initialize().catch(console.error);
if (!this._terminated) {
// eslint-disable-next-line promise/always-return
return this.locationsStub!.then(stub => {
this._terminated = true;
stub.close();
Expand Down
1 change: 0 additions & 1 deletion core/packages/gax/src/observability/LoggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ export function buildLogAttributes(
(attributes as Record<string, unknown>)[key] = value;
}
}
attributes['gcp.client.language'] = 'node';
if (rawAddress !== undefined && !isPreConnectionFailure(err)) {
attributes['server.address'] = rawAddress;
}
Expand Down
Loading
Loading