-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Logging API example. #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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; | ||
} | ||
}); | ||
} |
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); | ||
}); | ||
} |
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" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. engines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do I need engines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we at least specify a minimum node version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to provide a minimum version in engines as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's unnecessary, but if you guys want it, what version do you want me to specify? It's being tested on v0.10, v0.12, and v5.x There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol. I can't move on to anything else until I comment on how much I don't want to add this field. My stance is that for samples where we actually invite the user to consume and/or deploy (like when there's an Otherwise, for these samples, it's just bloat, something to be maintained and verified (or not). This sample is not going to be installed by NPM or consumed by a deploy or executed by some platform. There isn't anything that's going to programmatically read that field, only eyeballs will see it. I can set
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would argue that we do want these samples to be consumable. All of the python samples are runnable applications with their own dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure these samples can be run locally and have their own dependencies, but nothing about that cares about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am neutral :) Whatever y'all decide is fine by me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the |
||
} |
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); | ||
}); | ||
}); | ||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, that's what the cloud-node logging docs say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling @stephenplusplus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
googleapis/google-cloud-node#842 (comment)
// @munangst