Skip to content

Commit

Permalink
chore: merge remote-tracking branch 'remotes/origin/otel-2' into otel-4
Browse files Browse the repository at this point in the history
  • Loading branch information
feywind committed Sep 20, 2023
2 parents 9776fab + 60c70a8 commit d0503d0
Show file tree
Hide file tree
Showing 27 changed files with 1,413 additions and 449 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"@google-cloud/precise-date": "^4.0.0",
"@google-cloud/projectify": "^4.0.0",
"@google-cloud/promisify": "^4.0.0",
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/semantic-conventions": "~1.17.0",
"@opentelemetry/api": "~1.1.0",
"@opentelemetry/semantic-conventions": "~1.3.1",
"@types/duplexify": "^3.6.0",
"@types/long": "^4.0.0",
"arrify": "^2.0.0",
Expand All @@ -66,7 +66,8 @@
},
"devDependencies": {
"@grpc/proto-loader": "^0.7.0",
"@opentelemetry/tracing": "^0.24.0",
"@opentelemetry/core": "~1.3.1",
"@opentelemetry/sdk-trace-base": "~1.3.1",
"@types/execa": "^0.9.0",
"@types/extend": "^3.0.0",
"@types/lodash.snakecase": "^4.1.6",
Expand Down
218 changes: 109 additions & 109 deletions samples/openTelemetryTracing.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/*!
* 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.
*/
// Copyright 2020-2022 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 is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This sample demonstrates how to add OpenTelemetry tracing to the
Expand All @@ -22,8 +24,6 @@
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: OpenTelemetry Tracing
// description: Demonstrates how to enable OpenTelemetry tracing in
Expand All @@ -32,103 +32,103 @@

const SUBSCRIBER_TIMEOUT = 10;

// [START opentelemetry_tracing]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_OR_ID';
// const data = 'Hello, world!";

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Imports the OpenTelemetry API
const otel = require('@opentelemetry/sdk-trace-node');
const {diag, DiagConsoleLogger, DiagLogLevel} = require('@opentelemetry/api');
const {NodeTracerProvider} = otel;
const {
SimpleSpanProcessor,
ConsoleSpanExporter,
} = require('@opentelemetry/sdk-trace-base');

const {Resource} = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
} = require('@opentelemetry/semantic-conventions');

// Enable the diagnostic logger for OpenTelemetry
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const exporter = new ConsoleSpanExporter();

const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'otel example',
}),
});
const processor = new SimpleSpanProcessor(exporter);
provider.addSpanProcessor(processor);
provider.register();

// Creates a client; cache this for further use.
const pubSubClient = new PubSub();

async function publishMessage(topicNameOrId, data) {
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);
const messageId = await pubSubClient
.topic(topicNameOrId)
.publishMessage({data: dataBuffer});
console.log(`Message ${messageId} published.`);
}

async function subscriptionListen(subscriptionNameOrId) {
// Message handler for subscriber
const messageHandler = message => {
console.log(`Message ${message.id} received.`);
message.ack();

// Ensure that all spans got flushed by the exporter
console.log('Cleaning up OpenTelemetry exporter...');
exporter.shutdown().then(() => {
// Cleaned up exporter.
process.exit(0);
});
};

const errorHandler = error => {
console.log('Received error:', error);

console.log('Cleaning up OpenTelemetry exporter...');
exporter.shutdown().then(() => {
// Cleaned up exporter.
process.exit(0);
});
};

// Listens for new messages from the topic
pubSubClient.subscription(subscriptionNameOrId).on('message', messageHandler);
pubSubClient.subscription(subscriptionNameOrId).on('error', errorHandler);

// Wait a bit for the subscription. For the sample only.
setTimeout(() => {
pubSubClient.subscription(subscriptionNameOrId).removeAllListeners();
}, SUBSCRIBER_TIMEOUT * 1000);
}
// [END opentelemetry_tracing]

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
data = 'Hello, world!'
) {
// [START opentelemetry_tracing]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_OR_ID';
// 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));
// Enable the diagnostic logger for Opentelemetry
opentelemetry.diag.setLogger(
new opentelemetry.DiagConsoleLogger(),
opentelemetry.DiagLogLevel.INFO
);

provider.register();

// OpenTelemetry tracing is an optional feature and can be enabled by setting
// enableOpenTelemetryTracing 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(topicNameOrId, 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();

// Ensure that all spans got flushed by the exporter
console.log('Cleaning up Opentelemetry exporter...');
exporter.shutdown().then(() => {
// Cleaned up exporter.
process.exit(0);
});
};

const errorHandler = error => {
console.log('Received error:', error);

console.log('Cleaning up Opentelemetry exporter...');
exporter.shutdown().then(() => {
// Cleaned up exporter.
process.exit(0);
});
};

// Listens for new messages from the topic
pubSubClient
.subscription(subscriptionNameOrId, enableOpenTelemetryTracing)
.on('message', messageHandler);
pubSubClient
.subscription(subscriptionNameOrId, enableOpenTelemetryTracing)
.on('error', errorHandler);

setTimeout(() => {
pubSubClient
.subscription(subscriptionNameOrId, enableOpenTelemetryTracing)
.removeAllListeners();
}, SUBSCRIBER_TIMEOUT * 1000);
}

publishMessage().then(subscriptionListen());
// [END opentelemetry_tracing]
publishMessage(topicNameOrId, data)
.then(() => subscriptionListen(subscriptionNameOrId))
.catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
7 changes: 5 additions & 2 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
},
"dependencies": {
"@google-cloud/pubsub": "^4.0.5",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/tracing": "^0.24.0",
"@opentelemetry/api": "~1.1.0",
"@opentelemetry/resources": "~1.3.1",
"@opentelemetry/sdk-trace-node": "~1.3.1",
"@opentelemetry/sdk-trace-base": "~1.3.1",
"@opentelemetry/semantic-conventions": "~1.3.1",
"avro-js": "^1.10.1",
"p-defer": "^3.0.0",
"protobufjs": "^7.0.0"
Expand Down
Loading

0 comments on commit d0503d0

Please sign in to comment.