Storing application runtime logs
Analyzing information about application events over any period requires secure storage of application runtime logs.
Applications do not usually send their logs to storage systems directly. Instead, they use aggregators such as fluentd
This approach lets developers focus on application features and delegate log delivery and storage to special systems.
In this tutorial, you will learn how to save application runtime logs to Yandex Object Storage.
To set up application runtime log storage:
- Get your cloud ready.
- Set up your environment.
- Create a bucket for storing logs.
- Create a data stream.
- Create a transfer.
- Install Fluentd.
- Connect Fluentd to your data stream.
- Test sending and receiving data.
If you no longer need to store any logs, delete the resources in use.
Get your cloud ready
Sign up in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of application log storage support includes:
- Fee for data stream maintenance (see Yandex Data Streams pricing).
- Fee for transmitting data between sources and targets (see Yandex Data Transfer pricing).
- Fee for data storage (see Yandex Object Storage pricing).
Set up your environment
You will need the ID and secret key at the next steps.
Create a bucket for storing logs
- In the management console
, select the folder where you want to create a bucket. - From the list of services, select Object Storage.
- Click Create bucket.
- Name the bucket.
- In the Object read access, Object listing access, and Read access to settings fields, select Limited.
- In the Storage class field, select
Cold
. - Click Create bucket.
Create a data stream
- In the management console
, select the folder to create a data stream in. - Select Yandex Data Streams.
- Click Create stream.
- Specify an existing serverless YDB database or create a new one. If you chose to create a new database, click Refresh after creating it to refresh the list of databases.
- Enter a name for the stream.
- Click Create.
Wait for the stream to start. Once the stream is ready for use, its status will change from CREATING
to ACTIVE
.
Create a transfer
- On the page of the stream you created, click Actions and select Create data transfer.
- Create a source endpoint:
- In the Direction field, select
Source
. - Enter a name for the endpoint.
- From the Database type list, select
Yandex Data Streams
. - Select a database for the source.
- Enter the name of the stream you created earlier.
- Select the service account you created earlier.
- Click Create.
- In the Direction field, select
- Create a target endpoint:
- Click Create endpoint.
- In the Direction field, select
Target
. - Enter a name for the endpoint.
- From the Database type list, select
Object Storage
. - Enter the name of the bucket you created earlier.
- Select the service account you created earlier.
- Click Create.
- Create a transfer:
- In the left-hand panel, select
Transfers. - Click Create transfer.
- Name the transfer.
- Select the source endpoint you created earlier.
- Select the target endpoint you created earlier.
- Click Create.
- Click
next to the name of the created transfer and select Activate.
- In the left-hand panel, select
Wait until the transfer is activated. Once the transfer is ready for use, its status will change from Creating to Replicating.
Install Fluentd
- Download and install Fluentd
. - Install the Fluentd plugin to support the AWS Kinesis Data Streams protocol. This protocol will be responsible for streaming data.
sudo td-agent-gem install fluent-plugin-kinesis
Connect Fluentd to your data stream
- On the page of the stream you created, click Connect and navigate to the Fluentd tab.
- Copy the configuration file example and paste it into the
/etc/td-agent/td-agent.conf
file. Replace<key_id>
and<secret>
with the ID and secret key you got earlier.
Sample configuration file
<system>
log_level debug
</system>
<source>
@type http
@id input_http
port 8888
</source>
<match kinesis>
@type copy
<store>
@type stdout
</store>
<store>
@type kinesis_streams
aws_key_id <key_id>
aws_sec_key <secret>
# kinesis stream name
stream_name /ru-central1/b1gia92mbaom********/etnhstu01nin********/my-stream
# region
region ru-central-1
endpoint https://yds.serverless.yandexcloud.net
<buffer>
flush_interval 5s
</buffer>
</store>
</match>
Test sending and receiving data
To send data to your stream using Fluentd, run the command:
curl --request POST --data 'json={"user_id":"user1", "score": 100}' http://localhost:8888/kinesis
If the setup is successful, the Fluentd operation log named /var/log/td-agent/td-agent.log
will feature messages about receiving the data and writing it to Yandex Data Streams over the AWS Kinesis Data Streams protocol:
...
2022-04-20 19:36:37.770311035 +0000 kinesis: {"user_id":"user1","score":100}
2022-04-20 19:36:42 +0000 [debug]: #0 /ru-central1/b1gia92mbaom********/etnhstu01nin********/my-stream: Write chunk 5dd1b1ca1bd788e49185aa681e8132b9 / 1 records / 0 KB
2022-04-20 19:36:42 +0000 [debug]: #0 /ru-central1/b1gia92mbaom********/etnhstu01nin********/my-stream: Finish writing chunk
...
The bucket you created will contain a file with the sent message.
How to delete the resources you created
To stop paying for the resources you used: