diff --git a/README.md b/README.md index 2d5f27249e..93a8078a88 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ on Google Cloud Platform. * [Google Container Engine](#google-container-engine) * [Google BigQuery](#google-bigquery) * [Google Cloud Datastore (Beta)](#google-cloud-datastore-beta) + * [Stackdriver Debugger (Beta)](#stackdriver-debugger-beta) * [Google Cloud Functions (Alpha)](#google-cloud-functions-alpha) * [Stackdriver Logging (Beta)](#stackdriver-logging-beta) * [Stackdriver Monitoring (Beta)](#stackdriver-monitoring-beta) @@ -38,6 +39,7 @@ on Google Cloud Platform. * [Google Cloud Pub/Sub](#google-cloud-pubsub) * [Google Cloud Speech API (Alpha)](#google-cloud-speech-api-alpha) * [Google Cloud Storage](#google-cloud-storage) + * [Stackdriver Trace (Beta)](#stackdriver-trace-beta) * [Google Cloud Vision API](#google-cloud-vision-api) * [Other sample applications](#other-sample-applications) * [Bookshelf tutorial app](#bookshelf-tutorial-app) @@ -235,6 +237,16 @@ View the [Cloud Datastore Node.js samples][datastore_samples]. [datastore_docs]: https://cloud.google.com/datastore/docs/ [datastore_samples]: datastore +### Stackdriver Debugger (Beta) + +[Stackdriver Debugger][debugger_docs] makes it easier to view the application +state without adding logging statements. + +View the [Stackdriver Debugger Node.js sample][debugger_sample]. + +[debugger_docs]: https://cloud.google.com/debugger/docs/ +[debugger_sample]: debugger + ### Google Cloud Functions (Alpha) [Sign up for the Alpha][functions_signup]. @@ -316,6 +328,17 @@ View the [Cloud Storage Node.js samples][storage_samples]. [storage_docs]: https://cloud.google.com/storage/docs/ [storage_samples]: storage +### Stackdriver Trace (Beta) + +[Stackdriver Trace][trace_docs] is a distributed tracing system for Google Cloud +Platform that collects latency data from App Engine applications and displays it +in near real time in the Google Cloud Platform Console. + +View the [Stackdriver Trace Node.js sample][trace_sample]. + +[trace_docs]: https://cloud.google.com/trace/docs/ +[trace_sample]: trace + ### Google Cloud Vision API The [Cloud Vision API][vision_docs] allows developers to easily integrate vision diff --git a/debugger/README.md b/debugger/README.md new file mode 100644 index 0000000000..6adfb55bab --- /dev/null +++ b/debugger/README.md @@ -0,0 +1,32 @@ +# StackDriver Debugger sample for Node.js + +This sample demonstrates [StackDriver Debugger](https://cloud.google.com/debugger/) with Node.js. + +## Deploy and test + +Where appropriate, replace `[YOUR_PROJECT_ID]` with the ID of your Cloud project. + +1. `git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git` +1. `cd nodejs-docs-samples/debugger` +1. Initialize a new git repository: + + git init + +1. Configure git to use gcloud SDK: + + git config credential.helper gcloud.sh + +1. Add your Cloud Source Repository as a git remote: + + git remote add google https://source.developers.google.com/p/[YOUR_PROJECT_ID]/r/default + +1. Commit and push the code into the Cloud Source Repository: + + git add -A && git commit -m "Initial commit" && git push --all google + +1. Deploy the app: + + gcloud preview app deploy + +1. View the deployed app at https://[YOUR_PROJECT_ID].appspot.com/. +1. Use the [StackDriver Debugger dashboard](https://console.cloud.google.com/debug) to inspect runtime data of the app. diff --git a/debugger/app.js b/debugger/app.js new file mode 100644 index 0000000000..67d023f8ed --- /dev/null +++ b/debugger/app.js @@ -0,0 +1,35 @@ +// Copyright 2015-2016, Google, Inc. +// +// 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. + +// [START app] +'use strict'; + +if (process.env.GCLOUD_PROJECT) { + require('@google/cloud-debug'); +} + +var express = require('express'); +var app = express(); + +app.get('/', function (req, res) { + // Try using the StackDriver Debugger dashboard to inspect the "req" object + res.status(200).send('Hello, world!'); +}); + +// Start the server +var server = app.listen(process.env.PORT || '8080', function () { + console.log('App listening on port %s', server.address().port); + console.log('Press Ctrl+C to quit.'); +}); +// [END app] diff --git a/debugger/app.yaml b/debugger/app.yaml new file mode 100644 index 0000000000..f6ffeb3d5d --- /dev/null +++ b/debugger/app.yaml @@ -0,0 +1,17 @@ +# Copyright 2015-2016, Google, Inc. +# 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. + +# [START app_yaml] +runtime: nodejs +vm: true +# [END app_yaml] diff --git a/debugger/package.json b/debugger/package.json new file mode 100644 index 0000000000..2bdf2ac4f3 --- /dev/null +++ b/debugger/package.json @@ -0,0 +1,18 @@ +{ + "name": "stackdriver-debugger", + "description": "StackDriver Debugger sample for Node.js", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": "~4.2" + }, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "@google/cloud-debug": "^0.8.3", + "express": "^4.13.4" + } +} diff --git a/package.json b/package.json index c023f15f5a..f281d721ee 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "deps_bigquery": "cd bigquery; npm i; cd ../", "deps_computeengine": "cd computeengine; npm i; cd ../", "deps_datastore": "cd datastore; npm i; cd ../", + "deps_debugger": "cd debugger; npm i; cd ../", "deps_functions": "cd functions/uuid; npm i; cd ../..", "deps_logging": "cd logging; npm i; cd ../", "deps_monitoring": "cd monitoring; npm i; cd ../", @@ -59,10 +60,11 @@ "deps_pubsub": "cd pubsub; npm i; cd ../", "deps_speech": "cd speech; npm i; cd ../", "deps_storage": "cd storage; npm i; cd ../", + "deps_trace": "cd trace; npm i; cd ../", "deps_vision": "cd vision; npm i; cd ../", "lint": "semistandard \"**/*.js\"", "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 ../..;", - "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;", + "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;", "report": "nyc report --reporter=lcov | codecov", "report-html": "nyc report --reporter=html", "test": "npm run lint && npm run cover" diff --git a/test/appengine/all.test.js b/test/appengine/all.test.js index 41d23127ad..8d3532aaa3 100644 --- a/test/appengine/all.test.js +++ b/test/appengine/all.test.js @@ -222,6 +222,18 @@ var sampleTests = [ cmd: 'node', args: ['server.js'], msg: 'Hello Kubernetes!' + }, + { + dir: 'debugger', + cmd: 'node', + args: ['app.js'], + msg: 'Hello, world!' + }, + { + dir: 'trace', + cmd: 'node', + args: ['app.js'], + msg: 'acceleratedmobilepageurl' } ]; diff --git a/trace/README.md b/trace/README.md new file mode 100644 index 0000000000..d515c58c96 --- /dev/null +++ b/trace/README.md @@ -0,0 +1,16 @@ +# StackDriver Trace sample for Node.js + +This sample demonstrates [StackDriver Trace](https://cloud.google.com/trace/) with Node.js. + +## Deploy and test + +Where appropriate, replace `[YOUR_PROJECT_ID]` with the ID of your Cloud project. + +1. `git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git` +1. `cd nodejs-docs-samples/trace` +1. Deploy the app: + + gcloud preview app deploy + +1. Visit the deployed app at https://[YOUR_PROJECT_ID].appspot.com/. +1. Use the [StackDriver Trace dashboard](https://console.cloud.google.com/traces/traces) to inspect recorded traces. diff --git a/trace/app.js b/trace/app.js new file mode 100644 index 0000000000..5ecec03609 --- /dev/null +++ b/trace/app.js @@ -0,0 +1,49 @@ +// Copyright 2015-2016, Google, Inc. +// +// 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. + +// [START app] +'use strict'; + +if (process.env.NODE_ENV === 'production') { + require('@google/cloud-trace').start(); +} + +var request = require('request'); +var express = require('express'); +var app = express(); +var DISCOVERY_URL = 'https://www.googleapis.com/discovery/v1/apis'; + +// This incoming HTTP request should be captured by Trace +app.get('/', function (req, res) { + // This outgoing HTTP request should be captured by Trace + request({ + url: DISCOVERY_URL, + json: true + }, function (err, response, body) { + if (err) { + return res.status(500).end(); + } + var names = body.items.map(function (item) { + return item.name; + }); + return res.status(200).send(names.join('\n')); + }); +}); + +// Start the server +var server = app.listen(process.env.PORT || '8080', function () { + console.log('App listening on port %s', server.address().port); + console.log('Press Ctrl+C to quit.'); +}); +// [END app] diff --git a/trace/app.yaml b/trace/app.yaml new file mode 100644 index 0000000000..f6ffeb3d5d --- /dev/null +++ b/trace/app.yaml @@ -0,0 +1,17 @@ +# Copyright 2015-2016, Google, Inc. +# 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. + +# [START app_yaml] +runtime: nodejs +vm: true +# [END app_yaml] diff --git a/trace/package.json b/trace/package.json new file mode 100644 index 0000000000..f9502f98f4 --- /dev/null +++ b/trace/package.json @@ -0,0 +1,19 @@ +{ + "name": "stackdriver-trace", + "description": "StackDriver Trace sample for Node.js", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": "~6" + }, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "@google/cloud-trace": "^0.5.3", + "express": "^4.13.4", + "request": "^2.72.0" + } +}