GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add support for OTel context propagation and harmonized spans (… · googleapis/google-cloud-node@90be540

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handwritten/pubsub/samples
    • README.md
    • listenWithOpenTelemetryTracing.js
    • openTelemetryTracing.js
    • package.json
    • publishWithOpenTelemetryTracing.js
    • system-test
      • openTelemetryTracing.test.ts
    • typescript
      • listenWithOpenTelemetryTracing.ts
      • publishWithOpenTelemetryTracing.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handwritten/pubsub/samples/README.md‎

Copy file name to clipboardExpand all lines: handwritten/pubsub/samples/README.md
+31-11Lines changed: 31 additions & 11 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ guides.
6060
* [Listen with exactly-once delivery](#listen-with-exactly-once-delivery)
6161
* [Listen For Protobuf Messages](#listen-for-protobuf-messages)
6262
* [Listen For Messages With Custom Attributes](#listen-for-messages-with-custom-attributes)
63+
* [Subscribe with OpenTelemetry Tracing](#subscribe-with-opentelemetry-tracing)
6364
* [Modify Push Configuration](#modify-push-configuration)
64-
* [OpenTelemetry Tracing](#opentelemetry-tracing)
6565
* [Publish Avro Records to a Topic](#publish-avro-records-to-a-topic)
6666
* [Publish Batched Messages](#publish-batched-messages)
6767
* [Publish Message](#publish-message)
6868
* [Publish Message With Custom Attributes](#publish-message-with-custom-attributes)
6969
* [Publish Ordered Message](#publish-ordered-message)
7070
* [Publish Protobuf Messages to a Topic](#publish-protobuf-messages-to-a-topic)
7171
* [Publish with flow control](#publish-with-flow-control)
72+
* [Publish with OpenTelemetry Tracing](#publish-with-opentelemetry-tracing)
7273
* [Publish With Retry Settings](#publish-with-retry-settings)
7374
* [Quickstart](#quickstart)
7475
* [Remove Dead Letter Policy](#remove-dead-letter-policy)
@@ -862,37 +863,37 @@ __Usage:__
862863

863864

864865

865-
### Modify Push Configuration
866+
### Subscribe with OpenTelemetry Tracing
866867

867-
Modifies the configuration of an existing push subscription.
868+
Demonstrates how to enable OpenTelemetry tracing in a subscriber.
868869

869-
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/modifyPushConfig.js).
870+
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/listenWithOpenTelemetryTracing.js).
870871

871-
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/modifyPushConfig.js,samples/README.md)
872+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listenWithOpenTelemetryTracing.js,samples/README.md)
872873

873874
__Usage:__
874875

875876

876-
`node modifyPushConfig.js <topic-name-or-id> <subscription-name-or-id>`
877+
`node listenWithOpenTelemetryTracing.js <subscription-name-or-id>`
877878

878879

879880
-----
880881

881882

882883

883884

884-
### OpenTelemetry Tracing
885+
### Modify Push Configuration
885886

886-
Demonstrates how to enable OpenTelemetry tracing in a publisher or subscriber.
887+
Modifies the configuration of an existing push subscription.
887888

888-
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/openTelemetryTracing.js).
889+
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/modifyPushConfig.js).
889890

890-
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/openTelemetryTracing.js,samples/README.md)
891+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/modifyPushConfig.js,samples/README.md)
891892

892893
__Usage:__
893894

894895

895-
`node openTelemetryTracing.js <topic-name-or-id> <subscription-name-or-id>`
896+
`node modifyPushConfig.js <topic-name-or-id> <subscription-name-or-id>`
896897

897898

898899
-----
@@ -1033,6 +1034,25 @@ __Usage:__
10331034

10341035

10351036

1037+
### Publish with OpenTelemetry Tracing
1038+
1039+
Demonstrates how to enable OpenTelemetry tracing in a publisher.
1040+
1041+
View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/publishWithOpenTelemetryTracing.js).
1042+
1043+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishWithOpenTelemetryTracing.js,samples/README.md)
1044+
1045+
__Usage:__
1046+
1047+
1048+
`node openTelemetryTracing.js <topic-name-or-id>`
1049+
1050+
1051+
-----
1052+
1053+
1054+
1055+
10361056
### Publish With Retry Settings
10371057

10381058
Publishes a message to a topic with retry settings.
Collapse file

‎handwritten/pubsub/samples/listenWithOpenTelemetryTracing.js‎

Copy file name to clipboard
+134Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2020-2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// This is a generated sample, using the typeless sample bot. Please
16+
// look for the source TypeScript sample (.ts) for modifications.
17+
'use strict';
18+
19+
/**
20+
* This sample demonstrates how to add OpenTelemetry tracing to the
21+
* Google Cloud Pub/Sub API.
22+
*
23+
* For more information, see the README.md under /pubsub and the documentation
24+
* at https://cloud.google.com/pubsub/docs.
25+
*/
26+
27+
// sample-metadata:
28+
// title: Subscribe with OpenTelemetry Tracing
29+
// description: Demonstrates how to enable OpenTelemetry tracing in a subscriber.
30+
// usage: node listenWithOpenTelemetryTracing.js <subscription-name-or-id>
31+
32+
const OTEL_TIMEOUT = 2;
33+
const SUBSCRIBER_TIMEOUT = 10;
34+
35+
// [START pubsub_subscribe_otel_tracing]
36+
/**
37+
* TODO(developer): Uncomment these variables before running the sample.
38+
*/
39+
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_OR_ID';
40+
41+
// Imports the Google Cloud client library
42+
const {PubSub} = require('@google-cloud/pubsub');
43+
44+
// Imports the OpenTelemetry API
45+
const {NodeTracerProvider} = require('@opentelemetry/sdk-trace-node');
46+
const {diag, DiagConsoleLogger, DiagLogLevel} = require('@opentelemetry/api');
47+
const {SimpleSpanProcessor} = require('@opentelemetry/sdk-trace-base');
48+
49+
// To output to the console for testing, use the ConsoleSpanExporter.
50+
// import {ConsoleSpanExporter} from '@opentelemetry/sdk-trace-base';
51+
52+
// To output to Cloud Trace, import the OpenTelemetry bridge library.
53+
const {
54+
TraceExporter,
55+
} = require('@google-cloud/opentelemetry-cloud-trace-exporter');
56+
57+
const {Resource} = require('@opentelemetry/resources');
58+
const {
59+
SEMRESATTRS_SERVICE_NAME,
60+
} = require('@opentelemetry/semantic-conventions');
61+
62+
// Enable the diagnostic logger for OpenTelemetry
63+
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
64+
65+
// Log spans out to the console, for testing.
66+
// const exporter = new ConsoleSpanExporter();
67+
68+
// Log spans out to Cloud Trace, for production.
69+
const exporter = new TraceExporter();
70+
71+
// Build a tracer provider and a span processor to do
72+
// something with the spans we're generating.
73+
const provider = new NodeTracerProvider({
74+
resource: new Resource({
75+
[SEMRESATTRS_SERVICE_NAME]: 'otel subscriber example',
76+
}),
77+
});
78+
const processor = new SimpleSpanProcessor(exporter);
79+
provider.addSpanProcessor(processor);
80+
provider.register();
81+
82+
// Creates a client; cache this for further use.
83+
const pubSubClient = new PubSub({enableOpenTelemetryTracing: true});
84+
85+
async function subscriptionListen(subscriptionNameOrId) {
86+
const subscriber = pubSubClient.subscription(subscriptionNameOrId);
87+
88+
// Message handler for subscriber
89+
const messageHandler = async message => {
90+
console.log(`Message ${message.id} received.`);
91+
message.ack();
92+
};
93+
94+
// Error handler for subscriber
95+
const errorHandler = async error => {
96+
console.log('Received error:', error);
97+
};
98+
99+
// Listens for new messages from the topic
100+
subscriber.on('message', messageHandler);
101+
subscriber.on('error', errorHandler);
102+
103+
// Ensures that all spans got flushed by the exporter. This function
104+
// is in service to making sure that any buffered Pub/Sub messages
105+
// and/or OpenTelemetry spans are properly flushed to the server
106+
// side. In normal usage, you'd only need to do something like this
107+
// on process shutdown.
108+
async function shutdown() {
109+
await subscriber.close();
110+
await processor.forceFlush();
111+
await new Promise(r => setTimeout(r, OTEL_TIMEOUT * 1000));
112+
}
113+
114+
// Wait a bit for the subscription to receive messages, then shut down
115+
// gracefully. This is for the sample only; normally you would not need
116+
// this delay.
117+
await new Promise(r =>
118+
setTimeout(async () => {
119+
subscriber.removeAllListeners();
120+
await shutdown();
121+
r();
122+
}, SUBSCRIBER_TIMEOUT * 1000)
123+
);
124+
}
125+
// [END pubsub_subscribe_otel_tracing]
126+
127+
function main(subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID') {
128+
subscriptionListen(subscriptionNameOrId).catch(err => {
129+
console.error(err.message);
130+
process.exitCode = 1;
131+
});
132+
}
133+
134+
main(...process.argv.slice(2));
Collapse file

‎handwritten/pubsub/samples/openTelemetryTracing.js‎

Copy file name to clipboardExpand all lines: handwritten/pubsub/samples/openTelemetryTracing.js
-136Lines changed: 0 additions & 136 deletions
This file was deleted.
Collapse file

‎handwritten/pubsub/samples/package.json‎

Copy file name to clipboardExpand all lines: handwritten/pubsub/samples/package.json
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
"precompile": "npm run clean"
2222
},
2323
"dependencies": {
24+
"@google-cloud/opentelemetry-cloud-trace-exporter": "^2.0.0",
2425
"@google-cloud/pubsub": "^4.6.0",
2526
"@google-cloud/storage": "^7.11.1",
2627
"@opentelemetry/api": "^1.6.0",
27-
"@opentelemetry/tracing": "^0.24.0",
28+
"@opentelemetry/resources": "^1.17.0",
29+
"@opentelemetry/sdk-trace-base": "^1.17.0",
30+
"@opentelemetry/sdk-trace-node": "^1.17.0",
31+
"@opentelemetry/semantic-conventions": "^1.17.0",
2832
"avro-js": "^1.11.3",
2933
"p-defer": "^3.0.0",
3034
"protobufjs": "~7.3.0"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.