templep / devops

0 stars 9 forks source link

Logging, tracing, monitoring and observability - IOT #21

Closed Joekingu closed 1 month ago

Joekingu commented 5 months ago

Logging, tracing, monitoring and observability

Prometheus - Monitoring system & time series database:

Description:

Prometheus is a tool that is used to monitor many different metrics: HTTP metrics, system metrics, application metrics, database metrics, middleware metrics, container metrics or even custom metrics.

For more information you can visit the official documentation here: Official Prometheus Documentation

Our Implementation:

We decided to go for a basic implementation by monitoring all the default metrics collected by Prometheus. We have a basic NodeJS application, and we simply followed the getting-started guide for Docker.

The first step was to expose an endpoint in our application for Prometheus to collect metrics. As we are using NodeJS, we had to install the Prometheus module: npm install prom-client.

Then, in our index.js, we exposed our metrics endpoint:

// Expose Prometheus metrics endpoint
app.get('/metrics', async (req, res) => {
  res.set('Content-Type', register.contentType);
  res.end(await register.metrics());
});

Finally, we had to configure our Docker Compose to add Prometheus in a container in Docker. Initially, we added a file named prometheus.yml that contained the configuration for Prometheus. In the following configuration, we can see that Prometheus is scraping data on the localhost port 3000 at the metrics endpoint:

global:
  scrape_interval: 15s
  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'your-app'
    scrape_interval: 5s
    static_configs:
      - targets: ['app:3000']

After that, we added the Prometheus container in the compose.yaml. We put the Prometheus dashboard on port 9090 of the localhost. After launching Prometheus, it retrieves the configuration we defined before.

 prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"  #Retrieve the configuration from the prometheus.yml
    networks:
      - prom-net

Caution:

After encountering many difficulties, we discovered that both Prometheus and the application have to be on the same network. Moreover, it is important to use the correct address and not simply 'localhost', otherwise, it won't work as intended.

PS:

If you want to have a basic example of prometheus implementation, one can be found in the Monitoring folder.

Datadog:

Description :

DataDog est une plateforme complète de surveillance et d'analyse conçue pour suivre une large gamme de métriques à travers différents systèmes et applications. Elle offre une surveillance de l'infrastructure, des applications, des journaux, et bien plus encore, fournissant des insights sur les performances et la santé de l'ensemble de votre stack. Pour plus d'informations, vous pouvez consulter la documentation officielle ici : Documentation officielle de DataDog.

Notre Implémentation :

Dans notre implémentation, nous avons opté pour une configuration simple afin de surveiller les métriques par défaut collectées par DataDog. Nous disposons d'une application Node.js de base, et nous avons suivi le guide de démarrage pour Docker. La première étape a été d'intégrer l'agent DataDog dans notre conteneur Docker. Nous avons inclus l'installation de l'agent DataDog dans notre Dockerfile :

RUN DD_API_KEY=<VOTRE_CLE_API> bash -c "$(curl -L https://raw.githubusercontent.com/DataDog/datadog-agent/master/cmd/agent/install_script.sh)"

L'étape suivante a été d'exposer un point de terminaison dans notre application pour que DataDog puisse collecter des métriques. Comme nous utilisons Node.js, nous avons dû installer le module DataDog :

npm install datadog-metrics

Ensuite, dans notre fichier index.js, nous avons exposé notre point de terminaison pour les métriques :

const express = require('express');
const app = express();
const dogstatsd = require('datadog-metrics');

const apiKey = process.env.DATADOG_API_KEY;
dogstatsd.init({ apiKey: apiKey });

// Exposer le point de terminaison des métriques pour DataDog
app.get('/metrics', (req, res) => {
  dogstatsd.gauge('nodejs.page.views', 1);
  res.send('Metrics endpoint');
});

const port = 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Enfin, nous avons configuré notre Docker Compose pour ajouter DataDog dans un conteneur Docker. Nous avons ajouté un fichier nommé datadog.yaml qui contenait la configuration pour DataDog. Dans la configuration suivante, DataDog collecte des données sur le port localhost 3000 au point de terminaison des métriques :

api_key: "<VOTRE_CLE_API>"

Ensuite, nous avons ajouté le conteneur DataDog dans le fichier docker-compose.yml. Nous avons placé le tableau de bord DataDog sur le port 8125 du localhost. Après le lancement de DataDog, il récupère la configuration que nous avons définie précédemment.

datadog:
    image: datadog/agent:latest
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /proc/:/host/proc/:ro
      - /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
    environment:
      - DD_API_KEY=<VOTRE_CLE_API>
      - DD_LOGS_ENABLED=true
      - DD_LOGS_CONFIG_SERVICE=<NOM_DE_VOTRE_SERVICE>
      - DD_LOGS_CONFIG_SOURCE=<NOM_DE_VOTRE_SOURCE>
    network_mode: host

Attention :

Pendant notre mise en œuvre, nous avons rencontré des difficultés liées à la configuration réseau. Il est important de s'assurer que DataDog et l'application sont sur le même réseau. De plus, il est essentiel d'utiliser la bonne adresse et non simplement 'localhost', sinon cela ne fonctionnera pas comme prévu.