This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Exporter/Stackdriver: Use Trace SDKs (v2 API) #338
Merged
mayurkale22
merged 2 commits into
census-instrumentation:master
from
mayurkale22:stackdriver_v2
Feb 12, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /** | ||
| * Copyright 2019, 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 | ||
| * | ||
| * http://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 * as coreTypes from '@opencensus/core'; | ||
| import * as types from './types'; | ||
|
|
||
| const AGENT_LABEL_KEY = 'g.co/agent'; | ||
| const AGENT_LABEL_VALUE_STRING = `opencensus-node [${coreTypes.version}]`; | ||
| const AGENT_LABEL_VALUE = createAttributeValue(AGENT_LABEL_VALUE_STRING); | ||
|
|
||
| /** | ||
| * Creates StackDriver Links from OpenCensus Link. | ||
| * @param links coreTypes.Link[] | ||
| * @param droppedLinksCount number | ||
| * @returns types.Links | ||
| */ | ||
| export function createLinks( | ||
| links: coreTypes.Link[], droppedLinksCount: number): types.Links { | ||
| return {link: links.map((link) => createLink(link)), droppedLinksCount}; | ||
| } | ||
|
|
||
| /** | ||
| * Creates StackDriver Attributes from OpenCensus Attributes. | ||
| * @param attributes coreTypes.Attributes | ||
| * @param resourceLabels Record<string, types.AttributeValue> | ||
| * @param droppedAttributesCount number | ||
| * @returns types.Attributes | ||
| */ | ||
| export function createAttributes( | ||
| attributes: coreTypes.Attributes, | ||
| resourceLabels: Record<string, types.AttributeValue>, | ||
| droppedAttributesCount: number): types.Attributes { | ||
| const attributesBuilder = | ||
| createAttributesBuilder(attributes, droppedAttributesCount); | ||
| attributesBuilder.attributeMap[AGENT_LABEL_KEY] = AGENT_LABEL_VALUE; | ||
| attributesBuilder.attributeMap = | ||
| Object.assign({}, attributesBuilder.attributeMap, resourceLabels); | ||
| return attributesBuilder; | ||
| } | ||
|
|
||
| /** | ||
| * Creates StackDriver TimeEvents from OpenCensus Annotation and MessageEvent. | ||
| * @param annotationTimedEvents coreTypes.Annotation[] | ||
| * @param messageEventTimedEvents coreTypes.MessageEvent[] | ||
| * @param droppedAnnotationsCount number | ||
| * @param droppedMessageEventsCount number | ||
| * @returns types.TimeEvents | ||
| */ | ||
| export function createTimeEvents( | ||
| annotationTimedEvents: coreTypes.Annotation[], | ||
| messageEventTimedEvents: coreTypes.MessageEvent[], | ||
| droppedAnnotationsCount: number, | ||
| droppedMessageEventsCount: number): types.TimeEvents { | ||
| let timeEvents: types.TimeEvent[] = []; | ||
| if (annotationTimedEvents) { | ||
| timeEvents = annotationTimedEvents.map( | ||
| (annotation) => ({ | ||
| time: new Date(annotation.timestamp).toISOString(), | ||
| annotation: { | ||
| description: stringToTruncatableString(annotation.description), | ||
| attributes: createAttributesBuilder(annotation.attributes, 0) | ||
| } | ||
| })); | ||
| } | ||
| if (messageEventTimedEvents) { | ||
| timeEvents.push(...messageEventTimedEvents.map( | ||
| (messageEvent) => ({ | ||
| time: new Date(messageEvent.timestamp).toISOString(), | ||
| messageEvent: { | ||
| id: messageEvent.id, | ||
| type: createMessageEventType(messageEvent.type) | ||
| } | ||
| }))); | ||
| } | ||
| return { | ||
| timeEvent: timeEvents, | ||
| droppedAnnotationsCount, | ||
| droppedMessageEventsCount | ||
| }; | ||
| } | ||
|
|
||
| export function stringToTruncatableString(value: string): | ||
| types.TruncatableString { | ||
| return {value}; | ||
| } | ||
|
|
||
| export async function getResourceLabels( | ||
| monitoredResource: Promise<types.MonitoredResource>) { | ||
| const resource = await monitoredResource; | ||
| const resourceLabels: Record<string, types.AttributeValue> = {}; | ||
| if (resource.type === 'global') { | ||
| return resourceLabels; | ||
| } | ||
| for (const key of Object.keys(resource.labels)) { | ||
| const resourceLabel = `g.co/r/${resource.type}/${key}`; | ||
| resourceLabels[resourceLabel] = createAttributeValue(resource.labels[key]); | ||
| } | ||
| return resourceLabels; | ||
| } | ||
|
|
||
| function createAttributesBuilder( | ||
| attributes: coreTypes.Attributes, | ||
| droppedAttributesCount: number): types.Attributes { | ||
| const attributeMap: Record<string, types.AttributeValue> = {}; | ||
| for (const key of Object.keys(attributes)) { | ||
| attributeMap[key] = createAttributeValue(attributes[key]); | ||
| } | ||
| return {attributeMap, droppedAttributesCount}; | ||
| } | ||
|
|
||
| function createLink(link: coreTypes.Link): types.Link { | ||
| const traceId = link.traceId; | ||
| const spanId = link.spanId; | ||
| const type = createLinkType(link.type); | ||
| const attributes = createAttributesBuilder(link.attributes, 0); | ||
| return {traceId, spanId, type, attributes}; | ||
| } | ||
|
|
||
| function createAttributeValue(value: string|number| | ||
| boolean): types.AttributeValue { | ||
| switch (typeof value) { | ||
| case 'number': | ||
| // TODO: Consider to change to doubleValue when available in V2 API. | ||
| return {intValue: String(value)}; | ||
| case 'boolean': | ||
| return {boolValue: value as boolean}; | ||
| case 'string': | ||
| return {stringValue: stringToTruncatableString(value)}; | ||
| default: | ||
| throw new Error(`Unsupported type : ${typeof value}`); | ||
| } | ||
| } | ||
|
|
||
| function createMessageEventType(type: coreTypes.MessageEventType) { | ||
| switch (type) { | ||
| case coreTypes.MessageEventType.SENT: { | ||
| return types.Type.SENT; | ||
| } | ||
| case coreTypes.MessageEventType.RECEIVED: { | ||
| return types.Type.RECEIVED; | ||
| } | ||
| default: { return types.Type.TYPE_UNSPECIFIED; } | ||
| } | ||
| } | ||
|
|
||
| function createLinkType(type: coreTypes.LinkType) { | ||
| switch (type) { | ||
| case coreTypes.LinkType.CHILD_LINKED_SPAN: { | ||
| return types.LinkType.CHILD_LINKED_SPAN; | ||
| } | ||
| case coreTypes.LinkType.PARENT_LINKED_SPAN: { | ||
| return types.LinkType.PARENT_LINKED_SPAN; | ||
| } | ||
| default: { return types.LinkType.UNSPECIFIED; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,29 +14,32 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {Exporter, ExporterBuffer, RootSpan, Span, SpanContext} from '@opencensus/core'; | ||
| import {Exporter, ExporterBuffer, RootSpan, Span as OCSpan, SpanContext} from '@opencensus/core'; | ||
| import {logger, Logger} from '@opencensus/core'; | ||
| import {auth, JWT} from 'google-auth-library'; | ||
| import {google} from 'googleapis'; | ||
| // TODO change to use import when types for hex2dec will be available | ||
| const {hexToDec}: {[key: string]: (input: string) => string} = | ||
| require('hex2dec'); | ||
| import {StackdriverExporterOptions, TracesWithCredentials, TranslatedSpan, TranslatedTrace} from './types'; | ||
|
|
||
| import {getDefaultResource} from './common-utils'; | ||
| import {createAttributes, createLinks, createTimeEvents, getResourceLabels, stringToTruncatableString} from './stackdriver-cloudtrace-utils'; | ||
| import {AttributeValue, Span, SpansWithCredentials, StackdriverExporterOptions} from './types'; | ||
|
|
||
| google.options({headers: {'x-opencensus-outgoing-request': 0x1}}); | ||
| const cloudTrace = google.cloudtrace('v1'); | ||
| const cloudTrace = google.cloudtrace('v2'); | ||
|
|
||
| /** Format and sends span information to Stackdriver */ | ||
| export class StackdriverTraceExporter implements Exporter { | ||
| projectId: string; | ||
| exporterBuffer: ExporterBuffer; | ||
| logger: Logger; | ||
| failBuffer: SpanContext[] = []; | ||
| private RESOURCE_LABELS: Promise<Record<string, AttributeValue>>; | ||
|
|
||
| constructor(options: StackdriverExporterOptions) { | ||
| this.projectId = options.projectId; | ||
| this.logger = options.logger || logger.logger(); | ||
| this.exporterBuffer = new ExporterBuffer(this, options); | ||
| this.RESOURCE_LABELS = | ||
| getResourceLabels(getDefaultResource(this.projectId)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -54,13 +57,12 @@ export class StackdriverTraceExporter implements Exporter { | |
| * Publishes a list of root spans to Stackdriver. | ||
| * @param rootSpans | ||
| */ | ||
| publish(rootSpans: RootSpan[]) { | ||
| const stackdriverTraces = | ||
| rootSpans.map(trace => this.translateTrace(trace)); | ||
| async publish(rootSpans: RootSpan[]) { | ||
| const spanList = await this.translateSpan(rootSpans); | ||
|
|
||
| return this.authorize(stackdriverTraces) | ||
| .then((traces: TracesWithCredentials) => { | ||
| return this.sendTrace(traces); | ||
| return this.authorize(spanList) | ||
| .then((spans: SpansWithCredentials) => { | ||
| return this.batchWriteSpans(spans); | ||
| }) | ||
| .catch(err => { | ||
| for (const root of rootSpans) { | ||
|
|
@@ -70,51 +72,70 @@ export class StackdriverTraceExporter implements Exporter { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Translates root span data to Stackdriver's trace format. | ||
| * @param root | ||
| */ | ||
| private translateTrace(root: RootSpan): TranslatedTrace { | ||
| const spanList = root.spans.map((span: Span) => this.translateSpan(span)); | ||
| spanList.push(this.translateSpan(root)); | ||
|
|
||
| return {projectId: this.projectId, traceId: root.traceId, spans: spanList}; | ||
| async translateSpan(rootSpans: RootSpan[]) { | ||
| const resourceLabel = await this.RESOURCE_LABELS; | ||
| const spanList: Span[] = []; | ||
| rootSpans.forEach(rootSpan => { | ||
| // RootSpan data | ||
| spanList.push(this.createSpan(rootSpan, resourceLabel)); | ||
| rootSpan.spans.forEach(span => { | ||
| // Builds spans data | ||
| spanList.push(this.createSpan(span, resourceLabel)); | ||
| }); | ||
| }); | ||
| return spanList; | ||
| } | ||
|
|
||
| /** | ||
| * Translates span data to Stackdriver's span format. | ||
| * @param span | ||
| */ | ||
| private translateSpan(span: Span): TranslatedSpan { | ||
| return { | ||
| name: span.name, | ||
| kind: 'SPAN_KIND_UNSPECIFIED', | ||
| spanId: hexToDec(span.id), | ||
| startTime: span.startTime, | ||
| endTime: span.endTime, | ||
| labels: Object.keys(span.attributes) | ||
| .reduce( | ||
| (acc, k) => { | ||
| acc[k] = String(span.attributes[k]); | ||
| return acc; | ||
| }, | ||
| {} as Record<string, string>) | ||
| private createSpan( | ||
| span: OCSpan, resourceLabels: Record<string, AttributeValue>): Span { | ||
| const spanName = | ||
| `projects/${this.projectId}/traces/${span.traceId}/spans/${span.id}`; | ||
|
|
||
| const spanBuilder: Span = { | ||
| name: spanName, | ||
| spanId: span.id, | ||
| displayName: stringToTruncatableString(span.name), | ||
| startTime: span.startTime.toISOString(), | ||
| endTime: span.endTime.toISOString(), | ||
| attributes: createAttributes( | ||
| span.attributes, resourceLabels, span.droppedAttributesCount), | ||
| timeEvents: createTimeEvents( | ||
| span.annotations, span.messageEvents, span.droppedAnnotationsCount, | ||
| span.droppedMessageEventsCount), | ||
| links: createLinks(span.links, span.droppedLinksCount), | ||
| status: {code: span.status.code}, | ||
| sameProcessAsParentSpan: !span.remoteParent, | ||
| childSpanCount: null, // TODO: Consider to add count after pull/332 | ||
| stackTrace: null, // Unsupported by nodejs | ||
| }; | ||
| if (span.parentSpanId) { | ||
| spanBuilder.parentSpanId = span.parentSpanId; | ||
| } | ||
| if (span.status.message) { | ||
| spanBuilder.status.message = span.status.message; | ||
| } | ||
|
|
||
| return spanBuilder; | ||
| } | ||
|
|
||
| /** | ||
| * Sends traces in the Stackdriver format to the service. | ||
| * @param traces | ||
| * Sends new spans to new or existing traces in the Stackdriver format to the | ||
| * service. | ||
| * @param spans | ||
| */ | ||
| private sendTrace(traces: TracesWithCredentials) { | ||
| private batchWriteSpans(spans: SpansWithCredentials) { | ||
| return new Promise((resolve, reject) => { | ||
| cloudTrace.projects.patchTraces(traces, (err: Error) => { | ||
| // TODO: Consider to use gRPC call (BatchWriteSpansRequest) for sending | ||
| // data to backend : | ||
| // https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools. | ||
| // cloudtrace.v2#google.devtools.cloudtrace.v2.TraceService | ||
| cloudTrace.projects.traces.batchWrite(spans, (err: Error) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we are using HTTP and not gRPC? (If the answer is just "that's what we did before", could you create an issue to track moving to gRPC for this?)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, added TODO here. |
||
| if (err) { | ||
| err.message = `sendTrace error: ${err.message}`; | ||
| err.message = `batchWriteSpans error: ${err.message}`; | ||
| this.logger.error(err.message); | ||
| reject(err); | ||
| } else { | ||
| const successMsg = 'sendTrace sucessfully'; | ||
| const successMsg = 'batchWriteSpans sucessfully'; | ||
| this.logger.debug(successMsg); | ||
| resolve(successMsg); | ||
| } | ||
|
|
@@ -124,10 +145,10 @@ export class StackdriverTraceExporter implements Exporter { | |
|
|
||
| /** | ||
| * Gets the Google Application Credentials from the environment variables, | ||
| * authenticates the client and calls a method to send the traces data. | ||
| * authenticates the client and calls a method to send the spans data. | ||
| * @param stackdriverTraces | ||
| */ | ||
| private authorize(stackdriverTraces: TranslatedTrace[]) { | ||
| private authorize(stackdriverSpans: Span[]) { | ||
| return auth.getApplicationDefault() | ||
| .then((client) => { | ||
| let authClient = client.credential as JWT; | ||
|
|
@@ -138,12 +159,12 @@ export class StackdriverTraceExporter implements Exporter { | |
| authClient = authClient.createScoped(scopes); | ||
| } | ||
|
|
||
| const traces: TracesWithCredentials = { | ||
| projectId: client.projectId, | ||
| resource: {traces: stackdriverTraces}, | ||
| const spans: SpansWithCredentials = { | ||
| name: `projects/${this.projectId}`, | ||
| resource: {spans: stackdriverSpans}, | ||
| auth: authClient | ||
| }; | ||
| return traces; | ||
| return spans; | ||
| }) | ||
| .catch((err) => { | ||
| err.message = `authorize error: ${err.message}`; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make this be
doubleValue? Thenumbertype in JS is a double so we would be losing accuracy for floating point values.I would think if something is a BigInt and fits in 64 bits it should be made to be an
intValue, which it looks like Node 10 supportsUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v2 API doesn't support
doubleValuetype forAttributeValue-> https://github.com/googleapis/google-api-nodejs-client/blob/master/src/apis/cloudtrace/v2.ts#L153-L166. AlsointValueis considered asStringtype.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, OK. Then here are a couple options:
numberto its string representation. This is probably my suggestion. Maybe add a TODO to change this once the API supports it. The OpenCensus trace proto hasdoubleValue.intValueand otherwise useString.I think
intValueusesStringtype becausenumbercan't represent full accuracy 64-bit ints.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added TODO here to consider
doubleValuetype once available with cloudTrace V2 API. As per our offline discussion (+ your suggestion), will always translatenumberto its string representation forAttributeValuetype.