Skip to content

Commit

Permalink
Updated cloud-iot-logging tutorial issues (GoogleCloudPlatform#1567)
Browse files Browse the repository at this point in the history
* Update index.md

Fixed bugs and typo in index.md

* Update index.ts

Fixed bugs and typo in index.ts

* Upgraded dependencies

Co-authored-by: Todd Kopriva <43478937+ToddKopriva@users.noreply.github.com>
Co-authored-by: Jason Dobry <jmdobry@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 3, 2021
1 parent 3e48b02 commit ebc79ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tutorials/cloud-iot-logging/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"main": "lib/index.js",
"dependencies": {
"@google-cloud/logging": "^1.2.0",
"firebase-admin": "^5.11.0",
"firebase-functions": "^1.0.2",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.11.0",
"googleapis": "^39.1.0"
},
"devDependencies": {
Expand Down
19 changes: 8 additions & 11 deletions tutorials/cloud-iot-logging/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
'use strict';

import * as functions from 'firebase-functions';
const loggingClient = require('@google-cloud/logging');
const { Logging } = require('@google-cloud/logging');

import { runInDebugContext } from 'vm';

// create the Stackdriver Logging client
const logging = new loggingClient({
// create the Cloud Logging client
const logging = new Logging({
projectId: process.env.GCLOUD_PROJECT,
});

// start cloud function

exports.deviceLog =
functions.pubsub.topic('device-logs').onPublish((message) => {
const log = logging.log('device-logs');
Expand All @@ -42,30 +39,30 @@ exports.deviceLog =
device_num_id: message.attributes.deviceNumId,
device_registry_id: message.attributes.deviceRegistryId,
location: message.attributes.location,
}
},
},
labels: {
// note device_id is not part of the monitored resource, but you can
// include it as another log label
device_id: message.attributes.deviceId,
}
},
};
const logData = message.json;

// Here you optionally extract a severity value from the log payload if it
// is present
const validSeverity = [
'DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'ALERT', 'CRITICAL',
'EMERGENCY'
'EMERGENCY',
];
if (logData.severity &&
validSeverity.indexOf(logData.severity.toUpperCase()) > -1) {
(metadata as any)['severity'] = logData.severity.toUpperCase();
delete (logData.severity);


// write the log entryto Stackdriver Logging
// write the log entry to Cloud Logging
const entry = log.entry(metadata, logData);
return log.write(entry);
}
});
});
15 changes: 6 additions & 9 deletions tutorials/cloud-iot-logging/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,14 @@ The main part of the function handles a Pub/Sub message from IoT Core, extracts
[embedmd]:# (functions/src/index.ts /import/ $)
```ts
import * as functions from 'firebase-functions';
const loggingClient = require('@google-cloud/logging');

import { runInDebugContext } from 'vm';
const { Logging } = require('@google-cloud/logging');

// create the Cloud Logging client
const logging = new loggingClient({
const logging = new Logging({
projectId: process.env.GCLOUD_PROJECT,
});

// start cloud function

exports.deviceLog =
functions.pubsub.topic('device-logs').onPublish((message) => {
const log = logging.log('device-logs');
Expand All @@ -102,29 +99,29 @@ exports.deviceLog =
device_num_id: message.attributes.deviceNumId,
device_registry_id: message.attributes.deviceRegistryId,
location: message.attributes.location,
}
},
},
labels: {
// note device_id is not part of the monitored resource, but you can
// include it as another log label
device_id: message.attributes.deviceId,
}
},
};
const logData = message.json;

// Here you optionally extract a severity value from the log payload if it
// is present
const validSeverity = [
'DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'ALERT', 'CRITICAL',
'EMERGENCY'
'EMERGENCY',
];
if (logData.severity &&
validSeverity.indexOf(logData.severity.toUpperCase()) > -1) {
(metadata as any)['severity'] = logData.severity.toUpperCase();
delete (logData.severity);


// write the log entryto Cloud Logging
// write the log entry to Cloud Logging
const entry = log.entry(metadata, logData);
return log.write(entry);
}
Expand Down

0 comments on commit ebc79ed

Please sign in to comment.