[MUSIC PLAYING]
JAISEN MATHAL: Hi.
I'm Jaisen, and today I'm going to share
how Google Cloud's serverless products, including Firebase,
Functions, and BigQuery, can help developers build
an automated alerting system without having to stand
up or manage any servers.
Let's get started.
Google Cloud's serverless products
help you focus on what's important to your business.
Every minute not spent configuring and managing
servers can be spent on creating value for your business.
We will be using three of Google Cloud's serverless products,
including Firebase, Functions, and BigQuery.
Firebase lets you quickly build apps
that can scale to meet your customers' needs and usage
patterns.
Functions are the easiest way to get
your code running in the cloud.
Choose from seven popular languages,
upload your function code, and you're ready to go.
BigQuery is a serverless data warehouse
which easily ingests data from many Google Cloud products
and lets you run analysis of that data.
I'm going to walk you through a sample application that's
built on Firebase.
I'll show you how easy it is to get request logs from Firebase
streamed into BigQuery.
Finally, we'll use scheduled queries and remote functions,
a new BigQuery feature, to send alerts when too many errors are
experienced by your users.
Creating an app with Firebase is very simple.
In this session, we'll create a restaurant review web app.
I won't go into every detail in this video,
but you can follow a codelab which has just 15 steps
to build the entire app yourself.
It should take you less than 20 minutes
to have a fully functioning app deployed to Firebase.
A great place to start with Firebase is the CLI.
It's a simple yet powerful tool that
helps you initialize, configure, and deploy your application.
My favorite part is that it's an interactive command line
program.
You don't have to know everything about Firebase
to use it.
It'll ask you questions along the way.
You may be wondering how you can develop, test, and deploy
Firebase apps.
The CLI has you covered.
You can use the CLI to install an emulator that
will start a server locally.
You'll be surprised how quickly you're
seeing your code in action.
Once you're ready to deploy your app to Firebase,
you can run the deploy command.
Easy, huh?
Now that we've deployed our app, we
can use the Firebase console at console.firebase.google.com
to send your Firebase hosting web request
logs to Cloud Logging.
This is as easy as clicking a toggle in the Cloud Logging
integration card.
You can view the requests easily through the Cloud Console
by clicking View in Cloud Logging.
Using the Cloud Console's Log Viewer,
you'll see a lot of useful information,
including if the request was a cache
hit, the latency, caller's IP address, city, and country.
We're going to use the response status code for sending alerts.
Your Firebase app is now complete.
We can move on to the next step.
Let's write a function that will query a BigQuery table
and send email alerts if it sees elevated 400s or 500s.
Cloud Functions is Google Cloud's functions-as-a-service
offering.
It's the easiest way to get your code running in the cloud.
All you need to deploy a function is a function.
No web server, Dockerfile, or container.
Just some code in one of seven languages, including Node.js,
Python, Go, Java, .NET, Ruby, and PHP.
When writing a BigQuery remote function,
you'll take an array of rows as input
and return an array of values as output.
Let's walk through this code.
The function takes a request and response argument.
We will be using the response.
We then set some variables, including a return value.
Each BigQuery row will be a row in response.calls.
We loop over each one and check if the HTTP code
is greater than or equal to 400 and the count
is greater than 50.
If so, we'll send an email and add an email-sent
to the returnValue array.
We'll also call the Send Email function, which alerts us
if there are too many errors.
If not, we simply add no-email to the returnValue array.
Finally, we send the return value back
as the response with a 200 code.
Similar to Firebase, we will test our function locally
before deploying.
We'll use the Functions Framework, which
Google Cloud has open sourced.
Once the Functions Framework is running,
we can make requests on local host port 8080
and make sure the output is what we expect.
After we have our function working correctly,
we can go ahead and deploy it using the gcloud command line
tool.
Here, you see we're specifying the node 12 runtime.
Now that we have completed our Firebase app
and written our Cloud Function, it's
time to tie them together with BigQuery.
We will start by creating a logs routing sync
and choosing BigQuery as the destination.
Now all of our logs will get structured
and flow into BigQuery without any additional setup or jobs
that you have to run.
Next we'll create an external connection.
This lets you access data outside of BigQuery,
but you can use them inside of your BigQuery queries.
Once you've created your external connection,
you can create your BigQuery remote function.
You'll need to include your project, table, and a name
of your BigQuery function.
You'll also need the URL of your Cloud Function
to use as the endpoint.
Your function is ready to be used in a query.
Since we want to receive aggregate counts of error
codes, we can use a GROUP BY clause.
This will call the Cloud Function,
passing in a row for each HTTP code along with a count.
The function conditionally sends an email
if the error rates are too high.
Finally, we will schedule this query
to be executed regularly to automate everything and make
sure we're notified when things go wrong with our website.
As you saw, we built a website in Firebase
that sent request logs to BigQuery via Cloud Logging.
We then created a scheduled query
that called a Cloud Function which sent alerts
based on the data in BigQuery.
And we did all this without having to set up or configure
any servers.
I hope this session has showed you just
how easy Google Cloud's serverless products are
and that you can quickly create complex and scalable apps
in no time.
I look forward to seeing what you build next.
Thanks for watching.
[MUSIC PLAYING]