Skip to content

Commit

Permalink
introduce Logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Dec 17, 2015
1 parent c11b5e8 commit 3c16414
Show file tree
Hide file tree
Showing 19 changed files with 3,873 additions and 12 deletions.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This client supports the following Google Cloud Platform services:
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
* [Google Cloud Storage](#google-cloud-storage)
* [Google Compute Engine](#google-compute-engine)
* [Google Cloud Logging](#google-cloud-logging-beta) (Beta)
* [Google Cloud Resource Manager](#google-cloud-resource-manager-beta) (Beta)
* [Google Cloud Search](#google-cloud-search-alpha) (Alpha)

Expand Down Expand Up @@ -337,9 +338,61 @@ zone.createVM(name, { os: 'ubuntu' }, function(err, vm, operation) {
```


## Google Cloud Logging (Beta)

> **This is a Beta release of Google Cloud Logging.** This API is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
- [API Documentation][gcloud-logging-docs]
- [Official Documentation][cloud-logging-docs]

#### Preview

```js
// Authenticating on a global-basis. You can also authenticate on a per-API-
// basis (see Authentication section above).
var gcloud = require('gcloud')({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});

var logging = gcloud.logging();

// Create a sink using a Bucket as a destination.
var gcs = gcloud.storage();

logging.createSink('my-new-sink', {
destination: gcs.bucket('my-sink')
}, function(err, sink) {});

// Write a critical entry to a log.
var syslog = logging.log('syslog');

var resource = {
type: 'gce_instance',
labels: {
zone: 'global',
instance_id: 3
}
};

var entry = syslog.entry(resource, {
delegate: process.env.user
});

syslog.critical(entry, function(err) {});

// Get all entries in your project.
logging.getEntries(function(err, entries) {
if (!err) {
// `entries` contains all of the entries from the logs in your project.
}
});
```


## Google Cloud Resource Manager (Beta)

> This is a *Beta* release of Google Cloud Resource Manager. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
> **This is a Beta release of Google Cloud Resource Manager.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
- [API Documentation][gcloud-resource-docs]
- [Official Documentation][cloud-resource-docs]
Expand Down Expand Up @@ -375,7 +428,7 @@ project.getMetadata(function(err, metadata) {

## Google Cloud Search (Alpha)

> This is an *Alpha* release of Google Cloud Search. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
> **This is an Alpha release of Google Cloud Search.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
- [API Documentation][gcloud-search-docs]
- [Official Documentation][cloud-search-docs]
Expand Down Expand Up @@ -433,6 +486,7 @@ Apache 2.0 - See [COPYING](COPYING) for more information.
[gcloud-compute-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/compute
[gcloud-datastore-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/datastore
[gcloud-dns-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/dns
[gcloud-logging-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/logging
[gcloud-pubsub-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/pubsub
[gcloud-resource-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/resource
[gcloud-search-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/search
Expand Down Expand Up @@ -460,6 +514,8 @@ Apache 2.0 - See [COPYING](COPYING) for more information.

[cloud-dns-docs]: https://cloud.google.com/dns/docs

[cloud-logging-docs]: https://cloud.google.com/logging/docs

[cloud-pubsub-docs]: https://cloud.google.com/pubsub/docs

[cloud-resource-docs]: https://cloud.google.com/resource-manager
Expand Down
Empty file.
24 changes: 23 additions & 1 deletion docs/site/components/docs/docs-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ angular.module('gcloud.docs')
]
},

logging: {
title: 'Logging',
_url: '{baseUrl}/logging',
pages: [
{
title: 'Entry',
url: '/entry'
},
{
title: 'Log',
url: '/log'
},
{
title: 'Sink',
url: '/sink'
}
]
},

pubsub: {
title: 'PubSub',
_url: '{baseUrl}/pubsub',
Expand Down Expand Up @@ -262,6 +281,9 @@ angular.module('gcloud.docs')
'>=0.22.0': ['resource'],

// introduce Storage#Channel
'>=0.26.0': ['storageWithChannels']
'>=0.26.0': ['storageWithChannels'],

// introduce logging api.
'>=0.27.0': ['logging']
}
});
2 changes: 1 addition & 1 deletion docs/site/components/docs/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h3 class="sub-heading">
</article>
<hr>

<article ng-repeat="service in ['bigquery', 'compute', 'datastore', 'dns', 'pubsub', 'resource', 'search', 'storage']"
<article ng-repeat="service in ['bigquery', 'compute', 'datastore', 'dns', 'pubsub', 'logging', 'resource', 'search', 'storage']"
ng-if="isActiveDoc(service)"
ng-include="'site/components/docs/' + service + '-overview.html'">
</article>
Expand Down
11 changes: 11 additions & 0 deletions docs/site/components/docs/logging-overview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h3>Logging Overview</h3>
<p class="notice">
<strong>This is a Beta release of Google Cloud Logging.</strong> This API is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
</p>
<p>
The <code>gcloud.logging</code> method will return a <code>logging</code> object, allowing you to create sinks, write log entries, and more.
</p>
<p>
To learn more about Logging, see the <a href="https://cloud.google.com/logging/docs">What is Google Cloud Logging?</a>
</p>

2 changes: 1 addition & 1 deletion lib/common/service-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ ServiceObject.prototype.request = function(reqOpts, callback) {

reqOpts.interceptors_ = [].slice.call(this.interceptors);

this.parent.request(reqOpts, callback);
return this.parent.request(reqOpts, callback);
};

module.exports = ServiceObject;
2 changes: 1 addition & 1 deletion lib/common/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Service.prototype.request = function(reqOpts, callback) {

delete reqOpts.interceptors_;

this.makeAuthenticatedRequest(reqOpts, callback);
return this.makeAuthenticatedRequest(reqOpts, callback);
};

module.exports = Service;
40 changes: 34 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,34 @@ var apis = {
*/
dns: require('./dns'),

/**
* [Google Cloud Logging](https://cloud.google.com/logging/docs) collects and
* stores logs from applications and services on the Google Cloud Platform:
*
* - Export your logs to Google Cloud Storage, Google BigQuery, or Google
* Cloud Pub/Sub.
* - Integrate third-party logs from your virtual machine instances by
* installing the logging agent, `google-fluentd`.
*
* <p class="notice">
* **This is a Beta release of Google Cloud Logging.** This API is not
* covered by any SLA or deprecation policy and may be subject to backward-
* incompatible changes.
* </p>
*
* @type {module:logging}
*
* @return {module:logging}
*
* @example
* var gcloud = require('gcloud');
* var logging = gcloud.logging({
* projectId: 'grape-spaceship-123',
* keyFilename: '/path/to/keyfile.json'
* });
*/
logging: require('./logging'),

/**
* [Google Cloud Pub/Sub](https://developers.google.com/pubsub/overview) is a
* reliable, many-to-many, asynchronous messaging service from Google Cloud
Expand Down Expand Up @@ -138,9 +166,9 @@ var apis = {
* - Recover projects.
*
* <p class="notice">
* **This is a *Beta* release of Cloud Resource Manager.** This feature is
* not covered by any SLA or deprecation policy and may be subject to
* backward-incompatible changes.
* **This is a Beta release of Cloud Resource Manager.** This feature is not
* covered by any SLA or deprecation policy and may be subject to backward-
* incompatible changes.
* </p>
*
* @type {module:resource}
Expand All @@ -163,9 +191,9 @@ var apis = {
* maintaining a search service.
*
* <p class="notice">
* **This is an *Alpha* release of Google Cloud Search.** This feature is
* not covered by any SLA or deprecation policy and may be subject to
* backward-incompatible changes.
* **This is an Alpha release of Google Cloud Search.** This feature is not
* covered by any SLA or deprecation policy and may be subject to backward-
* incompatible changes.
* </p>
*
* @type {module:search}
Expand Down
140 changes: 140 additions & 0 deletions lib/logging/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.
*/

/*!
* @module logging/entry
*/

'use strict';

var extend = require('extend');
var is = require('is');

/**
* Create an entry object to define new data to insert into a log.
*
* @resource [LogEntry JSON representation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry}
*
* @param {object=|string=} resource - See a
* [Monitored Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource).
* @param {object|string} data - The data to use as the value for this log
* entry.
* @return {module:logging/entry}
*
* @example
* var gcloud = require('gcloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'grape-spaceship-123'
* });
*
* var logging = gcloud.logging();
*
* var syslog = logging.log('syslog');
*
* var resource = {
* type: 'gce_instance',
* labels: {
* zone: 'global',
* instance_id: 3
* }
* };
*
* var entry = syslog.entry(resource, {
* delegate: process.env.user
* });
*
* syslog.alert(entry, function(err, apiResponse) {
* if (!error) {
* // Log entry inserted successfully.
* }
* });
*
* //-
* // You will also receive `Entry` objects when using
* // {module:logging#getEntries} and {module:logging/log#getEntries}.
* //-
* logging.getEntries(function(err, entries) {
* if (!err) {
* // entries[0].data = The data value from the log entry.
* }
* });
*/
function Entry(resource, data) {
if (!data) {
this.data = resource;
return;
}

this.resource = resource;
this.data = data;
}

/**
* Create an Entry object from an API response, such as `entries:list`.
*
* @private
*
* @param {object} entry - An API representation of an entry. See a
* [LogEntry](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry).
* @return {module:logging/entity}
*/
Entry.fromApiResponse_ = function(entry) {
// Only one of these are populated.
var data = entry.protoPayload || entry.jsonPayload || entry.textPayload;
return extend(new Entry(entry.resource, data), entry);
};

/**
* Serialize an entry to the format the API expects.
*
* @private
*/
Entry.prototype.toJSON = function() {
var entry = extend(true, {}, this);

var whitelist = [
'logName',
'resource',
'timestamp',
'severity',
'insertId',
'httpRequest',
'labels',
'operation'
];

for (var prop in entry) {
if (whitelist.indexOf(prop) === -1) {
delete entry[prop];
}
}

if (is.string(this.resource)) {
entry.resource = {
type: this.resource
};
}

if (is.object(this.data)) {
entry.jsonPayload = this.data;
} else if (is.string(this.data)) {
entry.textPayload = this.data;
}

return entry;
};

module.exports = Entry;
Loading

0 comments on commit 3c16414

Please sign in to comment.