-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Opentelemetry integration #1078
Changes from all commits
af0a95c
73835fb
ae1ae81
f8b6602
c64b1ce
a8ede4b
aec8a5c
dbe8cdf
d8ebcf7
16b9aaa
e93dceb
fe14163
11478a7
3cb49e9
4c4482c
dd42a6a
59b6f62
78ce7f6
99a1fe1
5ccd513
39ec18a
0ad9443
d4996b9
0606f83
c9cd570
c974091
e343c52
00c0980
eccd6eb
92c3a09
f7369e8
c7cc67d
af46e32
5fa6274
ce35b88
d5ac567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
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. This is missing a doc end tag, though it may not matter at this point if nothing in the docsite is expecting it to be here. (We would need to canonicalize the the tag name anyway.) 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. I want to include a link to this example in the blog post. How should we canonicalize the tag name? 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. Let me see if I can find the right person to ask... there were some changes in that area lately. |
||
// [END opentelemetry_tracing] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
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, | ||
}); | ||
} | ||
} |
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.
We've got a change pending actually that will obsolete this, but I will deal with it when that gets merged.
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.
I just added it here to get CI to run the publisher tests too.