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
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ export class {{ service.name }}Client {
}
{%- endif %}

{%- if api.enableTelemetryTracing %}
opts.internalTelemetryInfo = {
gcpClientService: '{{ api.loggingName }}',
gcpClientVersion: '{{ api.naming.version }}',
gcpRepo: 'googleapis/google-cloud-node',
gcpArtifact: '{{ api.publishName }}',
}
{%- endif %}

// Choose either gRPC or proto-over-HTTP implementation of google-gax.
this._gaxModule = {% if not api.legacyProtoLoad %}opts.fallback ? gaxInstance.fallback : gaxInstance{% else %}gax{% endif %};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ export class {{ service.name }}Client {
gaxInstance = gax as typeof gax;
}
{%- endif %}

{%- if api.enableTelemetryTracing %}
opts.internalTelemetryInfo = {
gcpClientService: '{{ api.loggingName }}',
gcpClientVersion: '{{ api.naming.version }}',
gcpRepo: 'googleapis/google-cloud-node',
gcpArtifact: '{{ api.publishName }}',
}
{%- endif %}

// Choose either gRPC or proto-over-HTTP implementation of google-gax.
this._gaxModule = {% if not api.legacyProtoLoad %}opts.fallback ? gaxInstance.fallback : gaxInstance{% else %}gax{% endif %};
Expand Down
9 changes: 9 additions & 0 deletions core/packages/gax/src/clientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface ClientOptions
// No preference; exception will be thrown if both are set to different values.
universeDomain?: string;
universe_domain?: string;
enableTelemetryTracing?: boolean;
internalTelemetryInfo?: InternalTelemetry;
}

export interface Descriptors {
Expand Down Expand Up @@ -93,3 +95,10 @@ export interface PaginationResponse<
nextPageRequest?: RequestObject;
rawResponse?: ResponseObject;
}

export interface InternalTelemetry {
gcpClientService?: string;
gcpVersion?: string;
gcpRepo?: string;
gcpArtifact?: string;
}
18 changes: 17 additions & 1 deletion core/packages/gax/src/createApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Provides function wrappers that implement page streaming and retrying.
*/

import { ClientOptions } from './clientInterface';
import {createAPICaller} from './apiCaller';
import {
APICallback,
Expand All @@ -27,6 +28,7 @@ import {
RequestType,
SimpleCallbackFunction,
} from './apitypes';
import { ClientOptions } from './clientInterface';
import {Descriptor} from './descriptor';
import {CallOptions, CallSettings, convertRetryOptions} from './gax';
import {retryable} from './normalCalls/retries';
Expand Down Expand Up @@ -59,6 +61,7 @@ export function createApiCall(
descriptor?: Descriptor,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_fallback?: boolean | 'proto' | 'rest', // unused here, used in fallback.ts implementation
clientOptions?: ClientOptions
): GaxCall {
// we want to be able to accept both promise resolving to a function and a
// function. Currently client librares are only calling this method with a
Expand All @@ -67,7 +70,12 @@ export function createApiCall(
// the following apiCaller will be used for all calls of this function...
const apiCaller = createAPICaller(settings, descriptor);

return (
// Check if telemetry tracing is enabled and also check if internal telemetry information has been passed through the templates
const tracingEnabled =
clientOptions?.enableTelemetryTracing &&
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED &&
clientOptions?.internalTelemetryInfo !== undefined;
const invokeCall = (
request: RequestType,
callOptions?: CallOptions,
callback?: APICallback,
Expand Down Expand Up @@ -164,4 +172,12 @@ export function createApiCall(
// or to cancel the ongoing call.
return currentApiCaller.result(ongoingCall);
};

if (tracingEnabled) {
console.log('tracing enabled');
return invokeCall;
} else {
console.log('tracing disabled');
return invokeCall;
}
}
21 changes: 21 additions & 0 deletions core/packages/gax/src/observability/TracerHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { context, trace, Tracer } from `@opentelemetry/api`;
import * as grpc from '@grpc/grpc-js';
Comment on lines +1 to +2

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

Using template literals (backticks) for import module specifiers is a syntax error in TypeScript. Additionally, the @grpc/grpc-js import is unused in this file. Use single quotes for the import and remove the unused dependency.

Suggested change
import { context, trace, Tracer } from `@opentelemetry/api`;
import * as grpc from '@grpc/grpc-js';
import { context, trace, Tracer } from '@opentelemetry/api';


export interface StaticTraceContext {
gcpClientService?: string;
gcpVersion?: string;
gcpRepo?: string;
gcpArtifact?: string;
}

export interface DynamicTraceContext {
clientName: string;
methodName: string;
rpcType: 'grpc' | 'http';
}

export function getGaxTracer(): Tracer {
return trace.getTracer('google-gax');
}

export async function traceAttempt() { }
24 changes: 24 additions & 0 deletions core/packages/gax/test/unit/apiCallable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,30 @@ describe('createApiCall', () => {
);
}
});

describe('in regards to OpenTelemetry Tracing', () => {
let consoleSpy: sinon.SinonSpy;
beforeEach(() => {
consoleSpy = sinon.spy(console, 'log');
});

afterEach(() => {
consoleSpy.restore();
delete process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED;
});

it('logs "tracing enabled" when GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED and ClientOptions field is set', () => {
process.env.GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED = 'true';
const mockClientOptions = { enableTelemetryTracing: true };
createApiCall(() => { }, { clientOptions: mockClientOptions });
assert(consoleSpy.calledWith('tracing enabled'));
});

it('logs "tracing disabled" when GOOGLE_SDK_NODE_EXPERIMENTAL_O11Y_ENABLED is not set', () => {
createApiCall(() => { });
assert(consoleSpy.calledWith('tracing disabled'));
});
});
});

describe('Promise', () => {
Expand Down
4 changes: 4 additions & 0 deletions core/packages/gax/test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {GaxCallPromise} from '../../src/apitypes';
import {createApiCall as realCreateApiCall} from '../../src/createApiCall';
import {ClientOptions} from '../../src/clientInterface';
import * as gax from '../../src/gax';
import {GoogleError} from '../../src/googleError';
import {Descriptor} from '../../src/descriptor';
Expand All @@ -42,6 +43,7 @@ export interface Options {
returnCancelFunc?: boolean;
cancel?: Function;
deadline?: string;
clientOptions?: ClientOptions;
}

export function createApiCall(func: Function, opts?: Options) {
Expand Down Expand Up @@ -78,6 +80,8 @@ export function createApiCall(func: Function, opts?: Options) {
}),
settings,
descriptor,
undefined,
opts?.clientOptions,
) as GaxCallPromise;
}

Expand Down
Loading