-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Opentelemetry integration (#1078)
* Add opentelemetry tracing * build: rename _toc to toc (#1066) * changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. * fix: rename _toc to toc Source-Author: F. Hinkelmann <franziska.hinkelmann@gmail.com> Source-Date: Tue Jul 21 10:53:20 2020 -0400 Source-Repo: googleapis/synthtool Source-Sha: 99c93fe09f8c1dca09dfc0301c8668e3a70dd796 Source-Link: googleapis/synthtool@99c93fe Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com> * build: move gitattributes files to node templates (#1070) Source-Author: F. Hinkelmann <franziska.hinkelmann@gmail.com> Source-Date: Thu Jul 23 01:45:04 2020 -0400 Source-Repo: googleapis/synthtool Source-Sha: 3a00b7fea8c4c83eaff8eb207f530a2e3e8e1de3 Source-Link: googleapis/synthtool@3a00b7f * Add opentelemetry instrumentation * Add create span test * Refactor tracing * Add publisher key test * Fix linting issues * Add docs * Add example for opentelemetry * Add tracing example * Update headers * Add microsoft api documenter * Fix linting in samples/package.json * Add optional tracing * Fix linting issues * Re-add api-documenter * Update package.json * Update package.json * Update package.json * Fix docs * Add more unit tests * Fix linting * Add disable tracing tests * Update opentelemetryTracing sample Co-authored-by: Yoshi Automation Bot <yoshi-automation@google.com> Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com> Co-authored-by: Benjamin E. Coe <bencoe@google.com> Co-authored-by: Megan Potter <57276408+feywind@users.noreply.github.com>
- Loading branch information
1 parent
80e0ee3
commit 76db007
Showing
9 changed files
with
509 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,108 @@ | ||
/*! | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* This sample demonstrates how to add OpenTelemetry tracing to the | ||
* Google Cloud Pub/Sub API. | ||
* | ||
* For more information, see the README.md under /pubsub and the documentation | ||
* at https://cloud.google.com/pubsub/docs. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// sample-metadata: | ||
// title: OpenTelemetry Tracing | ||
// description: Demonstrates how to enable OpenTelemetry tracing in | ||
// a publisher or subscriber. | ||
// usage: node opentelemetryTracing.js <topic-name> <subscription-name> | ||
|
||
const SUBSCRIBER_TIMEOUT = 10; | ||
|
||
function main( | ||
topicName = 'YOUR_TOPIC_NAME', | ||
subscriptionName = 'YOUR_SUBSCRIPTION_NAME', | ||
data = {foo: 'bar'} | ||
) { | ||
// [START opentelemetry_tracing] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const topicName = 'my-topic'; | ||
// const subscriptionName = 'my-subscription'; | ||
// const data = 'Hello, world!"; | ||
|
||
// Imports the Google Cloud client library | ||
const {PubSub} = require('@google-cloud/pubsub'); | ||
|
||
// Imports the OpenTelemetry API | ||
const {opentelemetry} = require('@opentelemetry/api'); | ||
|
||
// Imports the OpenTelemetry span handlers and exporter | ||
const { | ||
SimpleSpanProcessor, | ||
BasicTracerProvider, | ||
ConsoleSpanExporter, | ||
} = require('@opentelemetry/tracing'); | ||
|
||
// Set up span processing and specify the console as the span exporter | ||
const provider = new BasicTracerProvider(); | ||
const exporter = new ConsoleSpanExporter(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
provider.register(); | ||
opentelemetry.trace.setGlobalTracerProvider(provider); | ||
|
||
// OpenTelemetry tracing is an optional feature and can be enabled by setting | ||
// enableOpenTelemetryTraceing as a publisher or subscriber option | ||
const enableOpenTelemetryTracing = { | ||
enableOpenTelemetryTracing: true, | ||
}; | ||
|
||
// Creates a client; cache this for further use | ||
const pubSubClient = new PubSub(); | ||
|
||
async function publishMessage() { | ||
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject) | ||
const dataBuffer = Buffer.from(data); | ||
|
||
const messageId = await pubSubClient | ||
.topic(topicName, enableOpenTelemetryTracing) | ||
.publish(dataBuffer); | ||
console.log(`Message ${messageId} published.`); | ||
} | ||
|
||
async function subscriptionListen() { | ||
// Message handler for subscriber | ||
const messageHandler = message => { | ||
console.log(`Message ${message.id} received.`); | ||
message.ack(); | ||
}; | ||
|
||
// Listens for new messages from the topic | ||
pubSubClient.subscription(subscriptionName).on('message', messageHandler); | ||
setTimeout(() => { | ||
pubSubClient | ||
.subscription(subscriptionName, enableOpenTelemetryTracing) | ||
.removeAllListeners(); | ||
}, SUBSCRIBER_TIMEOUT * 1000); | ||
} | ||
|
||
publishMessage().then(subscriptionListen()); | ||
// [END opentelemetry_tracing] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains 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
This file contains 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,43 @@ | ||
/*! | ||
* Copyright 2020 Google LLC | ||
* 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 {Attributes, SpanContext, Span, trace} from '@opentelemetry/api'; | ||
import {Tracer} from '@opentelemetry/tracing'; | ||
|
||
/** | ||
* Wrapper for creating OpenTelemetry Spans | ||
* | ||
* @class | ||
*/ | ||
export class OpenTelemetryTracer { | ||
/** | ||
* Creates a new span with the given properties | ||
* | ||
* @param {string} spanName the name for the span | ||
* @param {Attributes?} attributes an object containing the attributes to be set for the span | ||
* @param {SpanContext?} parent the context of the parent span to link to the span | ||
*/ | ||
createSpan( | ||
spanName: string, | ||
attributes?: Attributes, | ||
parent?: SpanContext | ||
): Span { | ||
const tracerProvider: Tracer = trace.getTracer('default') as Tracer; | ||
return tracerProvider.startSpan(spanName, { | ||
parent: parent, | ||
attributes: attributes, | ||
}); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.