Storing application runtime logs
To analyze information about application events for any period, application runtime logs need to be stored securely.
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 functionality and delegate log delivery and storage to special systems.
Using this tutorial, you'll learn how to save application runtime logs to Yandex Object Storage.
To set up application runtime log storage:
- Prepare your cloud.
- Configure the 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.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - 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.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of application log storage support includes:
- Data stream maintenance fees (see Yandex Data Streams pricing).
- Fees for transmitting data between sources and targets (see Yandex Data Transfer pricing).
- Data storage fees (see Yandex Object Storage pricing).
Configure the environment
You'll need the ID and secret key in the next steps.
Create a bucket for storing logs
- In the management console
, select the folder you want to create a bucket in. - In 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 Update after creating it to update 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 created stream, click Actions and select Create data transfer.
- Create a source endpoint:
- In the Direction field, select
Source
. - Enter a name for the endpoint.
- In the Database type list, select
Yandex Data Streams
. - Select a database for the source.
- Enter the name of the previously created stream.
- 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.
- In the Database type list, select
Object Storage
. - Enter the name of the previously created bucket.
- 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 previously created source endpoint.
- Select the previously created target endpoint.
- 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 used for streaming data.
sudo td-agent-gem install fluent-plugin-kinesis
Connect Fluentd to your data stream
- On the page of the created stream, click Connect and go to the Fluentd tab.
- Copy the sample configuration file and paste it into the
/etc/td-agent/td-agent.conf
file. Replace<key_id>
and<secret>
with the previously obtained ID and secret key.
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 the 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 /var/log/td-agent/td-agent.log
operation log will feature messages about receiving the data and writing it to Yandex Data Streams via 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 created bucket will contain a file with the message sent.
How to delete the resources you created
To stop paying for the resources you used: