Visualizing logs in Grafana using the Cloud Logging plugin
The Yandex Cloud Logging plugin for Grafana
Note
The Cloud Logging plugin works with Grafana versions 11.2.0 or earlier. Menu titles and UI elements may vary depending on the version.
To visualize logs:
- Install the plugin.
- Create a service account.
- Create an authorized key for the service account.
- Create a log group.
- Add records to the log group.
- Connect a data source in Grafana.
- View the logs in Grafana.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. 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 here.
Required paid resources
The cost of resources includes a fee for logging operations and log storage in a log group (see Yandex Cloud Logging pricing).
Install the plugin
-
Download
the archive with the latest plugin version. -
Unpack the archive to the directory with plugins. The path to the directory with plugins is specified in the Grafana configuration
.unzip <path_to_archive> -d <path_to_plugin_directory>Note
In macOS, after you unpack the plugin archive manually run the
/opt/homebrew/var/lib/grafana/plugins/yandexcloud-logging-datasource/yc-logs-plugin_darwin_arm64file and allow launching third-party applications in the system settings. -
Allow loading an unsigned plugin. To do this, specify the plugin name in the
allow_loading_unsigned_pluginsparameter of the Grafana configuration file:allow_loading_unsigned_plugins = yandexcloud-logging-datasourceFor more information about loading unsigned plugins, see the Grafana documentation
. -
Restart the Grafana server:
LinuxWindowsmacOSsudo systemctl restart grafana-server- Click Win+R.
- In the window that opens, enter
services.mscand click OK. - Right-click the line with
Grafanaand select Restart.
brew services restart grafana
Create a service account
- In the management console
, select the folder where you want to create a service account. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account:
grafana-plugin. - Click Add role and select the
logging.readerrole. - Click Create.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
Create a service account named
grafana-plugin:yc iam service-account create --name grafana-pluginResult:
id: nfersamh4s********** folder_id: b1gc1t4cb6********** created_at: "2023-09-26T10:36:29.726397755Z" name: grafana-pluginSave the ID of the
grafana-pluginservice account (id) and the ID of the folder where you created it (folder_id). -
Assign the
logging.readerrole for the folder to the service account:yc resource-manager folder add-access-binding <folder_ID> \ --role logging.reader \ --subject serviceAccount:<service_account_ID>Result:
done (1s)
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
-
In the configuration file, describe the service account parameters:
resource "yandex_iam_service_account" "grafana-plugin" { name = "grafana-plugin" folder_id = "<folder_ID>" } resource "yandex_resourcemanager_folder_iam_member" "reader" { folder_id = "<folder_ID>" role = "logging.reader" member = "serviceAccount:${yandex_iam_service_account.grafana-plugin id}" }Where:
name: Service account name. This is a required setting.folder_id: Folder ID. This is an optional setting. It defaults to the value specified in the provider settings.role: Role being assigned.
For more information about
yandex_iam_service_accountproperties, see this Terraform article. -
Make sure the configuration files are correct.
-
In the command line, navigate to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If you described the configuration correctly, the terminal will display information about the service account. If the configuration contains any errors, Terraform will show them.
-
-
Deploy the cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply -
Confirm creating the service account by typing
yesin the terminal and pressing Enter.This will create the service account. You can check it using the management console
or this CLI command:yc iam service-account list
To create a service account, use the create REST API method for the ServiceAccount resource or the ServiceAccountService/Create gRPC API call.
To assign the logging.reader role for a folder to a service account, use the setAccessBindings method for the ServiceAccount resource or the ServiceAccountService/SetAccessBindings gRPC API call.
Create an authorized key for a service account
- In the management console
, select the folder the service account belongs to. - In the list of services, select Identity and Access Management.
- In the left-hand panel, select
Service accounts. - In the list that opens, select the
grafana-pluginservice account. - Click Create new key in the top panel.
- Select Create authorized key.
- Select the encryption algorithm.
- Enter a description of the key so that you can easily find it in the management console.
- Click Create.
- In the window that opens, click Download file with keys.
- Click Close.
Create authorized keys for the grafana-plugin service account:
yc iam key create --service-account-name grafana-plugin -o authorized_key.json
If successful, a private key (privateKey) and a public key ID (id) will be written to the authorized_key.json file.
Key file example:
{
"id": "lfkoe35hsk**********",
"service_account_id": "ajepg0mjt0**********",
"created_at": "2023-10-10T10:04:56Z",
"key_algorithm": "RSA_2048",
"public_key": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
}
-
In the configuration file, describe the resources you want to create:
service_account_id: Service account ID. This is a required parameter.description: Key description. This is an optional parameter.key_algorithm: Key generation algorithm. This is an optional parameter. The default algorithm isRSA_2048. For more information about the acceptable parameter values, see the API documentation.
Here is an example of the configuration file structure:
resource "yandex_iam_service_account_key" "sa-auth-key" { service_account_id = "<service_account_ID>" description = "<key_description>" key_algorithm = "<key_generation_algorithm>" }For more information about the resources you can create with Terraform, see the relevant provider documentation.
-
Make sure the configuration files are correct.
-
In the command line, navigate to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration description is correct, the terminal will display a list of the resources being created and their settings. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy the cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply -
Confirm creating the resources: type
yesin the terminal and press Enter.This will create all the resources you need in the specified folder. You can check the new resources and their settings using the management console
and this CLI command:yc iam key list --service-account-id <service_account_ID>
-
To create an access key, use the create REST API method for the Key resource or the KeyService/Create gRPC API call.
Example of a request using cURL for the create REST API method:
curl --request POST \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer <IAM_token>" \
--data '{"serviceAccountId": "<service_account_ID>"}' \
https://iam.api.cloud.yandex.net/iam/v1/keys
Where:
<IAM_token>: IAM token of the user with permissions to create keys for the specified service account.<service_account_id>: ID of the service account for which the keys are being created.
If successful, the server response will contain the private key (privateKey) and public key ID (id). Save this data. You will not be able to get the secret key again.
Example of a server response:
{
"key": {
"createdAt": "2023-10-10T10:55:00+00:00",
"description": "",
"id": "lfkoe35hsk**********",
"keyAlgorithm": "RSA_2048",
"publicKey": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n",
"serviceAccountId": "ajepg0mjt0**********"
},
"privateKey": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
}
Create a log group
- In the management console
, go to the folder where you created thegrafana-pluginservice account. - Select Cloud Logging.
- Click Create group.
- Enter
grafana-pluginas the log group name. - Set the log group record retention period.
- Click Create group.
To create a log group, run this command:
yc logging group create \
--name=grafana-plugin \
--retention-period=<retention_period> \
Where:
--name: Log group name.--retention-period: Retention period for log group records.
Result:
done (1s)
id: af3flf29t8**********
folder_id: aoek6qrs8t**********
cloud_id: aoegtvhtp8**********
created_at: "2023-09-26T09:56:38.970Z"
name: grafana-plugin
status: ACTIVE
retention_period: 3600s
-
In the configuration file, describe the log group parameters:
provider "yandex" { token = "<OAuth_token>" cloud_id = "<cloud_ID>" folder_id = "<folder_ID>" zone = "ru-central1-a" } resource "yandex_logging_group" "grafana-plugin" { name = "grafana-plugin" folder_id = "<folder_ID>" retention_period = "1h" }Where:
name: Log group name.folder_id: Folder ID.retention_period: Retention period for log group records.
For more information about
yandex_logging_groupproperties, see this Terraform article. -
Make sure the configuration files are correct.
-
In the command line, navigate to the directory where you created the configuration file.
-
Run a check using this command:
terraform plan
If the configuration description is correct, the terminal will display a list of the resources being created and their settings. If the configuration contains any errors, Terraform will point them out.
-
-
Deploy the cloud resources.
-
If the configuration does not contain any errors, run this command:
terraform apply -
Confirm creating the resources: type
yesin the terminal and press Enter.This will create all the resources you need in the specified folder. You can check the new resources and their settings using the management console
or this CLI command:yc logging group list
To create a log group, use the create REST API method for the LogGroup resource or the LogGroupService/Create gRPC API call.
Add records to the log group
To add records to a log group, run this command:
-
Linux, macOS:
yc logging write \ --group-name=grafana-plugin \ --message="My message" \ --level=INFO -
Windows (cmd):
yc logging write ^ --group-name=grafana-plugin ^ --message="My message" ^ --level=INFO -
Windows (PowerShell):
yc logging write ` --group-name=grafana-plugin ` --message="My message" ` --level=INFOWhere:
--group-name: Name of the log group to add records to.--message: Message.--level: Logging level.
Note
You can skip the --group-name and --message parameters and provide only the values, e.g., grafana-plugin "My message".
To add records to the log group, use the LogIngestionService/Write gRPC API call.
Connect a data source in Grafana
-
In the browser, go to
http://localhost:3000/.Note
By default, Grafana uses port 3000, unless you specified a different one
in the configuration file. -
In the left-hand panel, select Connections → Add new connection.
-
In the list of sources, select Yandex Cloud Logging.
-
Click Add new data source.
-
Under Secret config, in the API Key field, paste the contents of the
authorized_key.jsonfile with the authorized keys. -
Under SDK config, in the Folder ID field, specify the ID of the folder with the
grafana-pluginlog group. -
Click Save & test.
View the logs in Grafana
-
In the Grafana interface, select Explore in the left-hand panel.
-
In the top-left corner, select the Yandex Cloud Logging data source from the drop-down list.
-
In the query editor for the data source:
-
Select the ID of the
grafana-pluginlog group in the Group field. -
Enter your query written in the filter expression language in the Filter query field.
-
In the top-right corner, click Run query.
You will see a histogram with log group records in the Logs volume section.
-
How to delete the resources you created
To stop paying for the resources you created, delete the log group.