Skip to content

Commit

Permalink
Add Logging examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Jan 19, 2016
1 parent 2ebd0b4 commit 503fe2f
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 21 deletions.
2 changes: 1 addition & 1 deletion computeengine/sendgrid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"license": "Apache Version 2.0",
"engines": {
"node": "~4.2"
"node": ">=0.10.x"
},
"dependencies": {
"sendgrid": "^2.0.0"
Expand Down
6 changes: 6 additions & 0 deletions datastore/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Datastore Samples

These samples require two environment variables to be set:

- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
download one from your Google project's "permissions" page.
- `TEST_PROJECT_ID` - Id of your Google project.

## Run a sample

Install dependencies:
Expand Down
7 changes: 5 additions & 2 deletions datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"engines": {
"node": ">=0.10.x"
},
"scripts": {
"tasks": "node tasks.js"
},
"dependencies": {
"async": "^1.5.0",
"gcloud": "^0.25.0"
"async": "^1.5.2",
"gcloud": "^0.27.0"
}
}
25 changes: 25 additions & 0 deletions logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Logging Samples

These samples require two environment variables to be set:

- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
download one from your Google project's "permissions" page.
- `TEST_PROJECT_ID` - Id of your Google project.

## Run a sample

Install dependencies:

npm install

To print available commands:

npm run

Execute a sample:

npm run <sample>

Example:

npm run write
92 changes: 92 additions & 0 deletions logging/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 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.

'use strict';

// [START setup]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
keyFilename = keyFilename || '/path/to/keyfile.json';

var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: keyFilename
});

var logging = gcloud.logging();
// [END setup]

// [START listSinks]
function listSinks(callback) {
// list all sinks in the project
logging.getSinks(callback);
}
// [END listSinks]

// [START createSink]
function createSink(callback) {
var gcs = gcloud.storage();

// create a new sink
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
logging.createSink('mySink', {
destination: gcs.bucket('logging-bucket')
}, callback);
}
// [END createSink]

// [START updateSink]
function updateSink(callback) {
var gcs = gcloud.storage();
var sink = logging.sink('mySink');

// update a sink
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
sink.setMetadata({
// change destination to something else
destination: gcs.bucket('other-logging-bucket')
}, callback);
}
// [END updateSink]

// [START deleteSink]
function deleteSink(callback) {
var sink = logging.sink('mySink');

// delete a sink
//
// This method only works if you are authenticated as yourself, e.g. using the
// gcloud SDK.
sink.delete(callback);
}
// [END deleteSink]

exports.listSinks = listSinks;
exports.createSink = createSink;
exports.updateSink = updateSink;
exports.deleteSink = deleteSink;

if (module === require.main) {
listSinks(function (err, sinks, apiResponse) {
console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse);
if (err) {
return;
}
});
}
48 changes: 48 additions & 0 deletions logging/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 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.

'use strict';

// [START list]
// [START auth]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
keyFilename = keyFilename || '/path/to/keyfile.json';

// [START require]
var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: keyFilename
});
// [END require]
// [END auth]

var logging = gcloud.logging();

function list(callback) {
// Retrieve 3 log entries.
logging.getEntries({
pageSize: 3
}, callback);
}
// [END list]

exports.list = list;

if (module === require.main) {
list(function (err, apiResponse) {
console.log(err, 'apiResponse:', apiResponse);
});
}
18 changes: 18 additions & 0 deletions logging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "nodejs-docs-samples-logging",
"description": "Node.js samples for Google Cloud Logging.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"engines": {
"node": ">=0.10.x"
},
"scripts": {
"list": "node list.js",
"write": "node write.js",
"export": "node export.js"
},
"dependencies": {
"gcloud": "^0.27.0"
}
}
86 changes: 86 additions & 0 deletions logging/write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 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.

/* jshint camelcase:false */
'use strict';

// [START write]
// [START setup]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
keyFilename = keyFilename || '/path/to/keyfile.json';

var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: keyFilename
});

var logging = gcloud.logging();
// [END setup]

function write(callback) {
var log = logging.log('myLog');

// Modify this resource type to match a resource in your project
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
// /v2beta1/monitoredResourceDescriptors/list
var resource = {
type: 'gae_app',
labels: {
module_id: 'default',
version_id: 'express'
}
};

var entry = log.entry(resource, {
foo: 'bar'
});

var secondEntry = log.entry(resource, {
beep: 'boop'
});

// You can log multiple entries one at a a time, but it is best to write
// multiple entires together in a batch.
log.write([
entry,
secondEntry
], callback);
}
// [END write]

// [START deleteLog]
function deleteLog(callback) {
var log = logging.log('myLog');

// Delete the logs
log.delete(callback);
}
// [END deleteLog]

exports.write = write;
exports.deleteLog = deleteLog;

if (module === require.main) {
write(function (err, apiResponse) {
console.log(err, 'apiResponse:', apiResponse);
if (err) {
return;
}
deleteLog(function (err, apiResponse) {
console.log(err, 'apiResponse:', apiResponse);
});
});
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@
"deps_datastore": "cd datastore && npm i && cd ../..",
"deps_storage": "cd storage && npm i && cd ../..",
"deps_prediction": "cd prediction && npm i && cd ../..",
"deps_logging": "cd logging && npm i && cd ../..",
"deps_express": "cd appengine/express && npm i && cd ../..",
"deps_sendgrid": "cd appengine/sendgrid && npm i && cd ../.. && cd computeengine/sendgrid && npm i && cd ../..",
"deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..",
"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_datastore && npm run deps_storage && npm run deps_prediction && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_prediction && npm run deps_logging && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
"test": "npm run jshint && npm run cover"
},
"devDependencies": {
"async": "^1.5.0",
"coveralls": "^2.11.4",
"googleapis": "^2.1.6",
"istanbul": "^0.4.0",
"async": "^1.5.2",
"coveralls": "^2.11.6",
"googleapis": "^2.1.7",
"istanbul": "^0.4.2",
"jshint": "~2.8.0",
"mocha": "^2.2.5",
"proxyquire": "^1.7.3",
Expand Down
24 changes: 24 additions & 0 deletions prediction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Prediction API Samples

These samples require an environment variable to be set:

- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
download one from your Google project's "permissions" page.

## Run a sample

Install dependencies:

npm install

To print available commands:

npm run

Execute a sample:

npm run <sample>

Example:

npm run hostedmodels
3 changes: 3 additions & 0 deletions prediction/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"engines": {
"node": ">=0.10.x"
},
"scripts": {
"hostedmodels": "node hostedmodels.js"
},
Expand Down
Loading

0 comments on commit 503fe2f

Please sign in to comment.