From ebc79ed588dd00e9dfb13c94f7da105dbf9f8d24 Mon Sep 17 00:00:00 2001 From: jos- Date: Wed, 3 Feb 2021 20:53:16 +0100 Subject: [PATCH] Updated cloud-iot-logging tutorial issues (#1567) * 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 --- .../cloud-iot-logging/functions/package.json | 4 ++-- .../cloud-iot-logging/functions/src/index.ts | 19 ++++++++----------- tutorials/cloud-iot-logging/index.md | 15 ++++++--------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/tutorials/cloud-iot-logging/functions/package.json b/tutorials/cloud-iot-logging/functions/package.json index f7032d6cbc..723fa53479 100644 --- a/tutorials/cloud-iot-logging/functions/package.json +++ b/tutorials/cloud-iot-logging/functions/package.json @@ -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": { diff --git a/tutorials/cloud-iot-logging/functions/src/index.ts b/tutorials/cloud-iot-logging/functions/src/index.ts index 506ac2cea9..37456cb6f7 100644 --- a/tutorials/cloud-iot-logging/functions/src/index.ts +++ b/tutorials/cloud-iot-logging/functions/src/index.ts @@ -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'); @@ -42,13 +39,13 @@ 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; @@ -56,7 +53,7 @@ exports.deviceLog = // is present const validSeverity = [ 'DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'ALERT', 'CRITICAL', - 'EMERGENCY' + 'EMERGENCY', ]; if (logData.severity && validSeverity.indexOf(logData.severity.toUpperCase()) > -1) { @@ -64,8 +61,8 @@ exports.deviceLog = 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); } - }); \ No newline at end of file + }); diff --git a/tutorials/cloud-iot-logging/index.md b/tutorials/cloud-iot-logging/index.md index d33d5f66c7..e088b00998 100644 --- a/tutorials/cloud-iot-logging/index.md +++ b/tutorials/cloud-iot-logging/index.md @@ -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'); @@ -102,13 +99,13 @@ 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; @@ -116,7 +113,7 @@ exports.deviceLog = // is present const validSeverity = [ 'DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'ALERT', 'CRITICAL', - 'EMERGENCY' + 'EMERGENCY', ]; if (logData.severity && validSeverity.indexOf(logData.severity.toUpperCase()) > -1) { @@ -124,7 +121,7 @@ exports.deviceLog = 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); }