NavigationContentFooter
Jump toSuggest an edit

How to send metrics and logs to your Cockpit

Reviewed on 19 June 2024Published on 31 October 2022

This page shows you how to send metrics and logs to your Cockpit. You can push metrics with any Prometheus Remote Write compatible agent such as the Prometheus, Grafana or OpenTelemetry Collector agents. You can push logs with any Loki compatible agent such as the Promtail, Fluentd, Fluent Bit or Logstash agents.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • Configured an agent. Read our dedicated documentation to find out how to configure the Grafana Alloy agent
  • Created an API key and retrieved your API secret key
Important
  • Having the default configuration on your agents might lead to more of your resources’ metrics being sent, a high consumption, and a high bill at the end of the month.
  • Sending metrics and logs for Scaleway resources or personal data using an external path is a billable feature. In addition, any data that you push yourself is billed, even if you send data from Scaleway products. Refer to the product pricing for more information.

Send metrics and logs to your Cockpit

  1. Click Cockpit in the Observability section of the console side menu. The Cockpit overview page displays.

  2. Create a token with the Push permission for metrics and logs.

    Important
    • Make sure that you create your token in the same region as the data sources you want to use it for.
    • Make sure that you copy your token’s secret key and save it before you exit the page.
  3. Configure your applications to send metrics and logs to your Cockpit endpoints, using your token’s secret key for authentication.

    Important

    Retrieve your metrics’ and logs’ push endpoints in the Data sources tab of the Scaleway console.

  4. Log in to your Grafana account.

  5. Create your Grafana dashboard and start monitoring your data.


Sending Python logs to Cockpit

Important

You need external logs in the fr-par region to follow the procedure below. Find out how to create external logs.

  1. Open a terminal and run pip install python-logging-loki or pip3 install python-logging-loki if you are using Python 3, to install the logging_loki package to send your Python logs.
  2. Create a token with the Push permission for logs.
  3. Create a python_logs.py Python file and paste the following code snippet into it. Make sure that you replace $SCW_SECRET_KEY with your API secret key and $COCKPIT_TOKEN_SECRET_KEY with your Cockpit token’s secret key.
import logging
import logging_loki
import time
handler = logging_loki.LokiHandler(
url="https://logs.cockpit.fr-par.scw.cloud/loki/api/v1/push",
tags={"job": "logs_from_python"},
auth=("$SCW_SECRET_KEY", "$COCKPIT_TOKEN_SECRET_KEY"),
version="1",
)
logger = logging.getLogger("my-first-python-logger")
logger.addHandler(handler)
def main():
# Start generating log messages
for i in range(10):
logger.error(f"Error message {i}")
time.sleep(1)
if __name__ == "__main__":
main()

This script sets up a logger that sends error log messages to the Loki endpoint in Cockpit. It then generates 10 error log messages with a one-second delay between each message. These log messages are sent to Cockpit and can be viewed and analyzed via the Grafana dashboard we will create in the next part.

Tip

If you are using the Scaleway Loki API, you can POST through the Authorization: Bearer <token> header, or POST using the following endpoint: https://api_key:YOUR_COCKPIT_TOKEN@logs.cockpit.fr-par.scw.cloud/loki/api/v1/push.

Creating a Grafana dashboard and visualizing your Python logs

  1. Log into Grafana using your Grafana credentials.

  2. Click Toggle menu, then click Dashboards.

  3. Click New in the top right corner of your screen, then Import.

  4. Upload the JSON file of the Grafana Log Viewer dashboard for Loki or enter 13639 in the Import via grafana.com field.

  5. Click Load.

  6. Enter a name for the dashboard or leave the default name.

  7. Select a folder in which to store the dashboard.

  8. Select your external logs data source in the Loki drop-down. Make sure that the data source you select is in the fr-par region.

  9. Click Import.

  10. Open a terminal and enter the following command.

    python3 python_logs.py
  11. Click the Home icon > Dashboards. Your Logs / App dashboard should display.

  12. Click the Logs / App dashboard. Your log lines from the Python snippet should appear.

    If your logs do not display, try changing the time range to match the time at which you ran your Python file.

  13. Click the View in Explore button for more configuration options.

Tip

Read our more in depth-documentation on how to configure the Grafana alloy agent to push your Scaleway metrics, and visualize them in a Grafana dashboard.

See also
How to send metrics to Cockpit using Grafana AlloyHow to delete an external data source
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway