Skip to content

Commit 93381d4

Browse files
authored
Add small StackDriver Trace and Debugger samples. (GoogleCloudPlatform#141)
1 parent 8152610 commit 93381d4

File tree

11 files changed

+241
-1
lines changed

11 files changed

+241
-1
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ on Google Cloud Platform.
3131
* [Google Container Engine](#google-container-engine)
3232
* [Google BigQuery](#google-bigquery)
3333
* [Google Cloud Datastore (Beta)](#google-cloud-datastore-beta)
34+
* [Stackdriver Debugger (Beta)](#stackdriver-debugger-beta)
3435
* [Google Cloud Functions (Alpha)](#google-cloud-functions-alpha)
3536
* [Stackdriver Logging (Beta)](#stackdriver-logging-beta)
3637
* [Stackdriver Monitoring (Beta)](#stackdriver-monitoring-beta)
3738
* [Google Cloud Prediction API](#google-cloud-prediction-api)
3839
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
3940
* [Google Cloud Speech API (Alpha)](#google-cloud-speech-api-alpha)
4041
* [Google Cloud Storage](#google-cloud-storage)
42+
* [Stackdriver Trace (Beta)](#stackdriver-trace-beta)
4143
* [Google Cloud Vision API](#google-cloud-vision-api)
4244
* [Other sample applications](#other-sample-applications)
4345
* [Bookshelf tutorial app](#bookshelf-tutorial-app)
@@ -235,6 +237,16 @@ View the [Cloud Datastore Node.js samples][datastore_samples].
235237
[datastore_docs]: https://cloud.google.com/datastore/docs/
236238
[datastore_samples]: datastore
237239

240+
### Stackdriver Debugger (Beta)
241+
242+
[Stackdriver Debugger][debugger_docs] makes it easier to view the application
243+
state without adding logging statements.
244+
245+
View the [Stackdriver Debugger Node.js sample][debugger_sample].
246+
247+
[debugger_docs]: https://cloud.google.com/debugger/docs/
248+
[debugger_sample]: debugger
249+
238250
### Google Cloud Functions (Alpha)
239251

240252
[Sign up for the Alpha][functions_signup].
@@ -316,6 +328,17 @@ View the [Cloud Storage Node.js samples][storage_samples].
316328
[storage_docs]: https://cloud.google.com/storage/docs/
317329
[storage_samples]: storage
318330

331+
### Stackdriver Trace (Beta)
332+
333+
[Stackdriver Trace][trace_docs] is a distributed tracing system for Google Cloud
334+
Platform that collects latency data from App Engine applications and displays it
335+
in near real time in the Google Cloud Platform Console.
336+
337+
View the [Stackdriver Trace Node.js sample][trace_sample].
338+
339+
[trace_docs]: https://cloud.google.com/trace/docs/
340+
[trace_sample]: trace
341+
319342
### Google Cloud Vision API
320343

321344
The [Cloud Vision API][vision_docs] allows developers to easily integrate vision

debugger/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# StackDriver Debugger sample for Node.js
2+
3+
This sample demonstrates [StackDriver Debugger](https://cloud.google.com/debugger/) with Node.js.
4+
5+
## Deploy and test
6+
7+
Where appropriate, replace `[YOUR_PROJECT_ID]` with the ID of your Cloud project.
8+
9+
1. `git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git`
10+
1. `cd nodejs-docs-samples/debugger`
11+
1. Initialize a new git repository:
12+
13+
git init
14+
15+
1. Configure git to use gcloud SDK:
16+
17+
git config credential.helper gcloud.sh
18+
19+
1. Add your Cloud Source Repository as a git remote:
20+
21+
git remote add google https://source.developers.google.com/p/[YOUR_PROJECT_ID]/r/default
22+
23+
1. Commit and push the code into the Cloud Source Repository:
24+
25+
git add -A && git commit -m "Initial commit" && git push --all google
26+
27+
1. Deploy the app:
28+
29+
gcloud preview app deploy
30+
31+
1. View the deployed app at https://[YOUR_PROJECT_ID].appspot.com/.
32+
1. Use the [StackDriver Debugger dashboard](https://console.cloud.google.com/debug) to inspect runtime data of the app.

debugger/app.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015-2016, Google, Inc.
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+
// [START app]
16+
'use strict';
17+
18+
if (process.env.GCLOUD_PROJECT) {
19+
require('@google/cloud-debug');
20+
}
21+
22+
var express = require('express');
23+
var app = express();
24+
25+
app.get('/', function (req, res) {
26+
// Try using the StackDriver Debugger dashboard to inspect the "req" object
27+
res.status(200).send('Hello, world!');
28+
});
29+
30+
// Start the server
31+
var server = app.listen(process.env.PORT || '8080', function () {
32+
console.log('App listening on port %s', server.address().port);
33+
console.log('Press Ctrl+C to quit.');
34+
});
35+
// [END app]

debugger/app.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2015-2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
# [END app_yaml]

debugger/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "stackdriver-debugger",
3+
"description": "StackDriver Debugger sample for Node.js",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": "~4.2"
10+
},
11+
"scripts": {
12+
"start": "node app.js"
13+
},
14+
"dependencies": {
15+
"@google/cloud-debug": "^0.8.3",
16+
"express": "^4.13.4"
17+
}
18+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@
5252
"deps_bigquery": "cd bigquery; npm i; cd ../",
5353
"deps_computeengine": "cd computeengine; npm i; cd ../",
5454
"deps_datastore": "cd datastore; npm i; cd ../",
55+
"deps_debugger": "cd debugger; npm i; cd ../",
5556
"deps_functions": "cd functions/uuid; npm i; cd ../..",
5657
"deps_logging": "cd logging; npm i; cd ../",
5758
"deps_monitoring": "cd monitoring; npm i; cd ../",
5859
"deps_prediction": "cd prediction; npm i; cd ../",
5960
"deps_pubsub": "cd pubsub; npm i; cd ../",
6061
"deps_speech": "cd speech; npm i; cd ../",
6162
"deps_storage": "cd storage; npm i; cd ../",
63+
"deps_trace": "cd trace; npm i; cd ../",
6264
"deps_vision": "cd vision; npm i; cd ../",
6365
"lint": "semistandard \"**/*.js\"",
6466
"pretest_geddy": "cd appengine/geddy; npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;",
65-
"pretest": "npm run deps_vision; npm run deps_computeengine; npm run deps_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_speech; npm run pretest_geddy;",
67+
"pretest": "npm run deps_trace; npm run deps_debugger; npm run deps_vision; npm run deps_computeengine; npm run deps_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_speech; npm run pretest_geddy;",
6668
"report": "nyc report --reporter=lcov | codecov",
6769
"report-html": "nyc report --reporter=html",
6870
"test": "npm run lint && npm run cover"

test/appengine/all.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ var sampleTests = [
222222
cmd: 'node',
223223
args: ['server.js'],
224224
msg: 'Hello Kubernetes!'
225+
},
226+
{
227+
dir: 'debugger',
228+
cmd: 'node',
229+
args: ['app.js'],
230+
msg: 'Hello, world!'
231+
},
232+
{
233+
dir: 'trace',
234+
cmd: 'node',
235+
args: ['app.js'],
236+
msg: 'acceleratedmobilepageurl'
225237
}
226238
];
227239

trace/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# StackDriver Trace sample for Node.js
2+
3+
This sample demonstrates [StackDriver Trace](https://cloud.google.com/trace/) with Node.js.
4+
5+
## Deploy and test
6+
7+
Where appropriate, replace `[YOUR_PROJECT_ID]` with the ID of your Cloud project.
8+
9+
1. `git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git`
10+
1. `cd nodejs-docs-samples/trace`
11+
1. Deploy the app:
12+
13+
gcloud preview app deploy
14+
15+
1. Visit the deployed app at https://[YOUR_PROJECT_ID].appspot.com/.
16+
1. Use the [StackDriver Trace dashboard](https://console.cloud.google.com/traces/traces) to inspect recorded traces.

trace/app.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2015-2016, Google, Inc.
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+
// [START app]
16+
'use strict';
17+
18+
if (process.env.NODE_ENV === 'production') {
19+
require('@google/cloud-trace').start();
20+
}
21+
22+
var request = require('request');
23+
var express = require('express');
24+
var app = express();
25+
var DISCOVERY_URL = 'https://www.googleapis.com/discovery/v1/apis';
26+
27+
// This incoming HTTP request should be captured by Trace
28+
app.get('/', function (req, res) {
29+
// This outgoing HTTP request should be captured by Trace
30+
request({
31+
url: DISCOVERY_URL,
32+
json: true
33+
}, function (err, response, body) {
34+
if (err) {
35+
return res.status(500).end();
36+
}
37+
var names = body.items.map(function (item) {
38+
return item.name;
39+
});
40+
return res.status(200).send(names.join('\n'));
41+
});
42+
});
43+
44+
// Start the server
45+
var server = app.listen(process.env.PORT || '8080', function () {
46+
console.log('App listening on port %s', server.address().port);
47+
console.log('Press Ctrl+C to quit.');
48+
});
49+
// [END app]

trace/app.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2015-2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
# [END app_yaml]

0 commit comments

Comments
 (0)