/** * Copyright 2018, OpenCensus Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { BasePlugin, PluginInternalFiles, SpanContext, SpanKind, TagMap } from '@opencensus/core'; import * as grpcTypes from 'grpc'; /** The metadata key under which span context is stored as a binary value. */ export declare const GRPC_TRACE_KEY = "grpc-trace-bin"; /** The metadata key under which TagMap is stored as a binary value. */ export declare const GRPC_TAGS_KEY = "grpc-tags-bin"; export declare type GrpcModule = typeof grpcTypes; export declare type SendUnaryDataCallback = (error: grpcTypes.ServiceError | null, value?: any, trailer?: grpcTypes.Metadata, flags?: grpcTypes.writeFlags) => void; /** gRPC instrumentation plugin for Opencensus */ export declare class GrpcPlugin extends BasePlugin { /** Span grpc attributes */ static readonly ATTRIBUTE_GRPC_KIND = "grpc.kind"; static readonly ATTRIBUTE_GRPC_METHOD = "grpc.method"; static readonly ATTRIBUTE_GRPC_STATUS_CODE = "grpc.status_code"; static readonly ATTRIBUTE_GRPC_ERROR_NAME = "grpc.error_name"; static readonly ATTRIBUTE_GRPC_ERROR_MESSAGE = "grpc.error_message"; protected readonly internalFileList: PluginInternalFiles; /** Constructs a new GrpcPlugin instance. */ constructor(); /** Patches gRPC incoming and outcoming request functions. */ protected applyPatch(): any; /** Unpatches all gRPC patched function. */ protected applyUnpatch(): void; /** * Returns a function that patches the gRPC server register function in order * to create trace spans for gRPC service methods. * @returns function that returns a patch for server.register function */ private getPatchServer; /** * Handler Unary and Client Stream Calls */ private clientStreamAndUnaryHandler; /** * Handler Server Stream and Bidirectional Stream Calls */ private serverStreamAndBidiHandler; /** * Returns a function that patches the gRPC makeClientConstructor in order * to patch all client methods. * @returns function that returns a patch for makeClientConstructor. */ private getPatchClient; /** * This function starts a span (child or root) immediately before the * client method is invoked, and ends it either in a callback or stream * event handler, depending on the method type. */ private getPatchedClientMethods; /** * This method handels the client remote call */ private makeGrpcClientRemoteCall; /** * Gets a metadata form args, creates a new one if not found. * * Code snippet inspired by: * https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/src/plugins/plugin-grpc.ts#L96) */ private getMetadata; /** * Convert Grpc Status to Span Status * * At this current version of Opencensus Specs they are the same. */ static convertGrpcStatusToSpanStatus(statusCode: grpcTypes.status): number; /** * Returns a span context on a Metadata object if it exists and is * well-formed, or null otherwise. * @param metadata The Metadata object from which span context should be * retrieved. */ static getSpanContext(metadata: grpcTypes.Metadata): SpanContext | null; /** * Sets a span context on a Metadata object if it exists. * @param metadata The Metadata object to which a span context should be * added. * @param spanContext The span context. */ static setSpanContext(metadata: grpcTypes.Metadata, spanContext: SpanContext): void; /** * Returns a TagMap on a Metadata object if it exists and is well-formed, or * null otherwise. * @param metadata The Metadata object from which TagMap should be retrieved. */ static getTagContext(metadata: grpcTypes.Metadata): TagMap | null; /** * Sets a TagMap context on a Metadata object if it exists. * @param metadata The Metadata object to which a TagMap should be added. * @param TagMap The TagMap. */ static setTagContext(metadata: grpcTypes.Metadata, tagMap: TagMap): void; /** Method to record stats for client and server. */ static recordStats(kind: SpanKind, tags: TagMap, argsOrValue: {}, reqOrRes: {}, ms: number): void; } declare const plugin: GrpcPlugin; export { plugin };