ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 135 - Professional Cloud DevOps Engineer discussion

Report
Export

Your Cloud Run application writes unstructured logs as text strings to Cloud Logging. You want to convert the unstructured logs to JSON-based structured logs. What should you do?

A.
A Install a Fluent Bit sidecar container, and use a JSON parser.
Answers
A.
A Install a Fluent Bit sidecar container, and use a JSON parser.
B.
Install the log agent in the Cloud Run container image, and use the log agent to forward logs to Cloud Logging.
Answers
B.
Install the log agent in the Cloud Run container image, and use the log agent to forward logs to Cloud Logging.
C.
Configure the log agent to convert log text payload to JSON payload.
Answers
C.
Configure the log agent to convert log text payload to JSON payload.
D.
Modify the application to use Cloud Logging software development kit (SDK), and send log entries with a jsonPay10ad field.
Answers
D.
Modify the application to use Cloud Logging software development kit (SDK), and send log entries with a jsonPay10ad field.
Suggested answer: D

Explanation:

The correct answer is D. Modify the application to use Cloud Logging software development kit (SDK), and send log entries with a jsonPayload field.

Cloud Logging SDKs are libraries that allow you to write structured logs from your Cloud Run application. You can use the SDKs to create log entries with a jsonPayload field, which contains a JSON object with the properties of your log entry. The jsonPayload field allows you to use advanced features of Cloud Logging, such as filtering, querying, and exporting logs based on the properties of your log entry1.

To use Cloud Logging SDKs, you need to install the SDK for your programming language, and then use the SDK methods to create and send log entries to Cloud Logging. For example, if you are using Node.js, you can use the following code to write a structured log entry with a jsonPayload field2:

// Imports the Google Cloud client library

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

// Creates a client

const logging = new Logging();

// Selects the log to write to

const log = logging.log('my-log');

// The data to write to the log

const text = 'Hello, world!';

const metadata = {

// Set the Cloud Run service name and revision as labels

labels: {

service_name: process.env.K_SERVICE || 'unknown',

revision_name: process.env.K_REVISION || 'unknown',

},

// Set the log entry payload type and value

jsonPayload: {

message: text,

timestamp: new Date(),

},

};

// Prepares a log entry

const entry = log.entry(metadata);

// Writes the log entry

await log.write(entry);

console.log(`Logged: ${text}`);

Using Cloud Logging SDKs is the best way to convert unstructured logs to structured logs, as it provides more flexibility and control over the format and content of your log entries.

Using a Fluent Bit sidecar container is not a good option, as it adds complexity and overhead to your Cloud Run application. Fluent Bit is a lightweight log processor and forwarder that can be used to collect and parse logs from various sources and send them to different destinations3. However, Cloud Run does not support sidecar containers, so you would need to run Fluent Bit as part of your main container image. This would require modifying your Dockerfile and configuring Fluent Bit to read logs from supported locations and parse them as JSON. This is more cumbersome and less reliable than using Cloud Logging SDKs.

Using the log agent in the Cloud Run container image is not possible, as the log agent is not supported on Cloud Run. The log agent is a service that runs on Compute Engine or Google Kubernetes Engine instances and collects logs from various applications and system components. However, Cloud Run does not allow you to install or run any agents on its underlying infrastructure, as it is a fully managed service that abstracts away the details of the underlying platform.

Storing the password directly in the code is not a good practice, as it exposes sensitive information and makes it hard to change or rotate the password. It also requires rebuilding and redeploying the application each time the password changes, which adds unnecessary work and downtime.

1: Writing structured logs | Cloud Run Documentation | Google Cloud

2: Write structured logs | Cloud Run Documentation | Google Cloud

3: Fluent Bit - Fast and Lightweight Log Processor & Forwarder

: Logging Best Practices for Serverless Applications - Google Codelabs

: About the logging agent | Cloud Logging Documentation | Google Cloud

: Cloud Run FAQ | Google Cloud

asked 18/09/2024
Martin Schouten
41 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first