Elastic Cloud Observability is the premiere tool to provide visibility into your running web apps. Google Cloud Run is the serverless platform of choice to run your web apps that need to scale up massively and scale down to zero. Elastic Observability combined with Google Cloud Run is the perfect solution for developers to deploy web apps that are auto-scaled with fully observable operations, in a way that’s straightforward to implement and manage.
This blog post will show you how to deploy a simple Hello World web app to Cloud Run and then walk you through the steps to instrument the Hello World web app to enable observation of the application’s operations with Elastic Cloud.
Elastic Observability setup
We’ll start with setting up an Elastic Cloud deployment, which is where observability will take place for the web app we’ll be deploying.
From the Elastic Cloud console, select Create deployment.
Enter a deployment name and click Create deployment. It takes a few minutes for your deployment to be created. While waiting, you are prompted to save the admin credentials for your deployment, which provides you with superuser access to your Elastic® deployment. Keep these credentials safe as they are shown only once.
Elastic Observability requires an APM Server URL and an APM Secret token for an app to send observability data to Elastic Cloud. Once the deployment is created, we’ll copy the Elastic Observability server URL and secret token and store them somewhere safely for adding to our web app code in a later step.
To copy the APM Server URL and the APM Secret Token, go to Elastic Cloud. Then go to the Deployments page which lists all of the deployments you have created. Select the deployment you want to use, which will open the deployment details page. In the Kibana row of links, click on Open to open Kibana for your deployment.
Select Integrations from the top-level menu. Then click the APM tile.
On the APM Agents page, copy the secretToken and the serverUrl values and save them for use in a later step.
Now that we’ve completed the Elastic Cloud setup, the next step is to set up our Google Cloud project for deploying apps to Cloud Run.
Google Cloud Run setup
First we’ll need a Google Cloud project, so let’s create one by going to the Google Cloud console and creating a new project. Select the project menu and then click the New Project button.
Once the new project is created, we’ll need to enable the necessary APIs that our Hello World app will be using. This can be done by clicking this enable APIs link, which opens a page in the Google Cloud console that lists the APIs that will be enabled and allows us to confirm their activation.
After we’ve enabled the necessary APIs, we’ll need to set up the required permissions for our Hello World app, which can be done in the IAM section of the Google Cloud Console. Within the IAM section, select the Compute Engine default service account and add the following roles:
- Logs Viewer
- Monitoring Viewer
- Pub/Sub Subscriber
Deploy a Hello World web app to Cloud Run
We’ll perform the process of deploying a Node.js Hello World web app to Cloud Run using the handy Google Cloud tool called Cloud Shell Editor. To deploy the Hello World app, we’ll perform the following five steps:
- In Cloud Shell Editor, in the terminal window that appears at the bottom of the screen, clone a Node.js Hello World sample app repo from GitHub by entering the following command.
git clone https://github.com/elastic/observability-examples
- Change directory to the location of the Hello World web app code.
cd gcp/run/helloworld
- Build the Hello World app image and push the image to Google Container Registry by running the command below in the terminal. Be sure to replace your-project-id in the command below with your actual Google Cloud project ID.
gcloud builds submit --tag gcr.io/your-project-id/elastic-helloworld
- Deploy the Hello World app to Google Cloud Run by running the command below. Be sure to replace your-project-id in the command below with your actual Google Cloud project ID.
gcloud run deploy elastic-helloworld --image gcr.io/your-project-id/elastic-helloworld
- When the deployment process is complete, a Service URL will be displayed within the terminal. Copy and paste the Service URL in a browser to view the Hello World app running in Cloud Run.
Instrument the Hello World web app with Elastic Observability
With a web app successfully running in Cloud Run, we’re now ready to add the minimal code necessary to start monitoring the app. To enable observability for the Hello World app in Elastic Cloud, we’ll perform the following six steps:
- In the Google Cloud Shell Editor, edit the Dockerfile file to add the following Elastic Open Telemetry environment variables along with the commands to install and run the Elastic APM agent. Replace the ELASTIC_APM_SERVER_URL text and the ELASTIC_APM_SECRET_TOKEN text with the APM Server URL and the APM Secret Token values that you copied and saved in an earlier step.
ENV OTEL_EXPORTER_OTLP_ENDPOINT='ELASTIC_APM_SERVER_URL'
ENV OTEL_EXPORTER_OTLP_HEADERS='Authorization=Bearer ELASTIC_APM_SECRET_TOKEN'
ENV OTEL_LOG_LEVEL=info
ENV OTEL_METRICS_EXPORTER=otlp
ENV OTEL_RESOURCE_ATTRIBUTES=service.version=1.0,deployment.environment=production
ENV OTEL_SERVICE_NAME=helloworld
ENV OTEL_TRACES_EXPORTER=otlp
RUN npm install --save @opentelemetry/api
RUN npm install --save @opentelemetry/auto-instrumentations-node
CMD ["node", "--require", "@opentelemetry/auto-instrumentations-node/register", "index.js"]
The updated Dockerfile should look something like this:
FROM node:18-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . ./
OTEL_EXPORTER_OTLP_ENDPOINT='https://******.apm.us-central1.gcp.cloud.es.io:443'
ENV OTEL_EXPORTER_OTLP_HEADERS='Authorization=Bearer ******************'
ENV OTEL_LOG_LEVEL=info
ENV OTEL_METRICS_EXPORTER=otlp
ENV OTEL_RESOURCE_ATTRIBUTES=service.version=1.0,deployment.environment=production
ENV OTEL_SERVICE_NAME=helloworld
ENV OTEL_TRACES_EXPORTER=otlp
RUN npm install --save @opentelemetry/api
RUN npm install --save @opentelemetry/auto-instrumentations-node
CMD ["node", "--require", "@opentelemetry/auto-instrumentations-node/register", "index.js"]
- In the Google Cloud Shell Editor, edit the package.json file to add the Elastic APM dependency. The dependencies section in package.json should look something like this:
"dependencies": {
"express": "^4.18.2",
"elastic-apm-node": "^3.49.1"
},
- In the Google Cloud Shell Editor, edit the index.js file:
- Add the code required to initialize the Elastic Open Telemetry APM agent:
const otel = require("@opentelemetry/api");
const tracer = otel.trace.getTracer("hello-world");
- Replace the “Hello World!” output code . . .
res.send(`<h1>Hello World!</h1>`);
...with the “Hello Elastic Observability” code block.
res.send(
`<div style="text-align: center;">
<h1 style="color: #005A9E; font-family:'Verdana'">
Hello Elastic Observability - Google Cloud Run - Node.js
</h1>
<img src="https://storage.googleapis.com/elastic-helloworld/elastic-logo.png">
</div>`
);
- Add a trace “hi” before the “Hello Elastic Observability” code block and add a trace “bye” after the “Hello Elastic Observability” code block.
tracer.startActiveSpan("hi", (span) => {
console.log("hello");
span.end();
});
res.send(
`<div style="text-align: center;">
<h1 style="color: #005A9E; font-family:'Verdana'">
Hello Elastic Observability - Google Cloud Run - Node.js
</h1>
<img src="https://storage.googleapis.com/elastic-helloworld/elastic-logo.png">
</div>`
);
tracer.startActiveSpan("bye", (span) => {
console.log("goodbye");
span.end();
});
- The completed index.js file should look something like this:
const otel = require("@opentelemetry/api");
const tracer = otel.trace.getTracer("hello-world");
const express = require("express");
const app = express();
app.get("/", (req, res) => {
tracer.startActiveSpan("hi", (span) => {
console.log("hello");
span.end();
});
res.send(
`<div style="text-align: center;">
<h1 style="color: #005A9E; font-family:'Verdana'">
Hello Elastic Observability - Google Cloud Run - Node.js
</h1>
<img src="https://storage.googleapis.com/elastic-helloworld/elastic-logo.png">
</div>`
);
tracer.startActiveSpan("bye", (span) => {
console.log("goodbye");
span.end();
});
});
const port = parseInt(process.env.PORT) || 8080;
app.listen(port, () => {
console.log(`helloworld: listening on port ${port}`);
});
- Rebuild the Hello World app image and push the image to the Google Container Registry by running the command below in the terminal. Be sure to replace your-project-id in the command below with your actual Google Cloud project ID.
gcloud builds submit --tag gcr.io/your-project-id/elastic-helloworld
- Redeploy the Hello World app to Google Cloud Run by running the command below. Be sure to replace your-project-id in the command below with your actual Google Cloud project ID.
gcloud run deploy elastic-helloworld --image gcr.io/your-project-id/elastic-helloworld
- When the deployment process is complete, a Service URL will be displayed within the terminal. Copy and paste the Service URL in a browser to view the updated Hello World app running in Cloud Run.
Observe the Hello World web app
Now that we’ve instrumented the web app to send observability data to Elastic Observability, we can now use Elastic Cloud to monitor the web app’s operations.
-
In Elastic Cloud, select the Observability Services menu item.
-
Click the helloworld service.
-
Click the Transactions tab.
-
Scroll down and click the GET / transaction.
-
Scroll down to the Trace Sample section to see the GET / , hi and bye trace samples.
Observability made to scale
You’ve seen the entire process of deploying a web app to Google Cloud Run that is instrumented with Elastic Observability. The end result is a web app that will scale up and down with demand combined with the observability tools to monitor the web app as it serves a single user or millions of users.
Now that you’ve seen how to deploy a serverless web app instrumented with observability, visit Elastic Observability to learn more about how to implement a complete observability solution for your apps. Or visit Getting started with Elastic on Google Cloud for more examples of how you can drive the data insights you need by combining Google Cloud monitoring and cloud computing services with Elastic’s search-powered platform.
The release and timing of any features or functionality described in this post remain at Elastic's sole discretion. Any features or functionality not currently available may not be delivered on time or at all.