NavigationContentFooter
Jump toSuggest an edit

How to send metrics and logs to your Cockpit

Reviewed on 05 April 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 agent.
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 newly created token for authentication.

    Important
    • The Prometheus Remote Write endpoint to push your metrics is the following: https://metrics.cockpit.fr-par.scw.cloud/api/v1/push
    • The Remote Write endpoint to push your logs is the following: https://logs.cockpit.fr-par.scw.cloud/loki/api/v1/push
  4. Log in to your Grafana account.

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

    Sending Python logs to Cockpit

    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. Copy and save the following code snippet into a text editor. Make sure that you replace $SCW_SECRET_KEY and $COCKPIT_TOKEN with your values.
    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"),
    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 the Home icon, then click Dashboards.

  3. Click New in the top right corner of your screen and Import to import your Grafana dashboard.

  4. Load the URL of the premade Grafana Log Viewer Dashboard for Loki or enter 13639 in the ID field.

Your log lines from the Python snippet should display in the dashboard.

Tip

To go further, find out how to configure the Grafana agent to push your metrics and logs, and visualize them on your dashboard.

See also
How to create an external data sourceHow to delete an external data source
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway