Skip to content

How to create a Prometheus endpoint to expose custom metrics

License

Notifications You must be signed in to change notification settings

FactoryTalk-Optix/Optix_Sample_PrometheusMetricsExporter

Repository files navigation

Metric Logic

This project contains a Metric_Logic class that exposes various system and process metrics using Prometheus and OpenTelemetry, the two components can be detached they do not strictly relate each other. The metrics are exposed on http://localhost:1234/metrics.

metrics image

Cloning the repository

There are multiple ways to download this project, here is the recommended one

Clone repository

  1. Click on the green CODE button in the top right corner
  2. Select HTTPS and copy the provided URL
  3. Open FT Optix IDE
  4. Click on Open and select the Remote tab
  5. Paste the URL from step 2
  6. Click Open button in bottom right corner to start cloning process

Prerequisites

  • FactoryTalk Optix 1.5.x or later (.NET8 is required)
  • .NET Core SDK
  • Prometheus

NuGet packages

The following packages have to be installed (if not automatically restored by FactoryTalk Optix)

  • Microsoft.Extensions.ObjectPool (Version 8.0.10)
  • prometheus-net (Version 8.2.1)
  • System.Diagnostics.PerformanceCounter (Version 8.0.1)
  • OpenTelemetry (Version 1.3.0)
  • OpenTelemetry.Exporter.Console (Version 1.3.0)

Configuration

Allow FTOptixRuntime Access to Port 1234

To allow FTOptixRuntime access to port 1234 without administrator privileges, run the following command in the terminal (as Administrator):

netsh http add urlacl url=http://+:1234/ user=Everyone

Prometheus Configuration

Add the following scrape configuration to your Prometheus configuration file (prometheus.yml):

scrape_configs:
  - job_name: 'ftoptix_prometheus'
    static_configs:
    - targets: ['ipaddress:1234']
	
  - job_name: 'FTOptix_OpenTelemetry'
    static_configs:
    - targets: ["otel-collector:8888", "otel-collector:8889"]

Metrics Exposed

The following metrics are exposed:

  • FTOptix_Model_Variable1: Variable1 from Model folder
  • FTOptix_Diagnostics_totalCpuUsagePercent: Total CPU usage percent
  • FTOptix_Diagnostics_totalRamUsageMegaBytes: Total RAM utilization in MB
  • FTOptix_Diagnostics_processCpuUsagePercent: CPU usage percent of the current process
  • FTOptix_Diagnostics_processMemoryUsageMegaBytes: Memory usage of the current process in MB

Code Overview

Metric_Logic Class

  • Start Method: Initializes and starts the metrics server on port 1234, configures OpenTelemetry with a console exporter, and starts a periodic task to refresh the metrics.
  • Stop Method: Disposes of the periodic task and metrics server.
  • UpdateMetricsMethod: Refreshes the memory and CPU metrics and updates the Prometheus gauges.

CpuUsage Class

  • GetCpuUsageForProcess: Retrieves the CPU usage for the current process, with platform-specific implementations for Windows, Linux, and macOS.
  • GetTotalCpuUsage: Retrieves the total CPU usage, with platform-specific implementations for Windows, Linux, and macOS.

MemoryUsage Class

  • GetProcessMemoryUsage: Retrieves the memory usage of the current process.
  • GetTotalMemoryUsage: Retrieves the total memory usage, with platform-specific implementations for Windows, Linux, and macOS.

Running the Code

  1. Build and run the project.
  2. Access the metrics at http://ipaddress:1234/metrics.

Monitoring with dotnet-counters

You can also monitor the metrics using dotnet-counters:

dotnet-counters monitor -n FTOptixRuntime --counters [metric name]

Replace [metric name] with the name of the metric you want to monitor.

Logging

Errors during metric refresh are logged using the Log.Error method, these are shown to the FactoryTalk Optix logs.

Sample Grafana + Prometheus stack

This is just an example of how to run an instance of Grafana + Prometheus + OpenTelemetry to see the exported metrics

  • Grafana: used to create cool dashboards
  • Prometheus: used to collect metrics and pass it to Grafana
  • Ubuntu: optional - only used to edit configs
  • OpenTelemetry Collector: receiving OpenTelemetry data from Optix and passing it to Prometheus using the exporter
services:     
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=Password01
    volumes:
      - grafana:/etc/grafana/provisioning/datasources
      - grafana_db:/var/lib/grafana
      
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    ports:
      - 9090:9090
    restart: unless-stopped
    volumes:
      - prom_etc:/etc/prometheus
      - prom_data:/prometheus

  ubuntu:
    image: ubuntu:latest
    container_name: grafana_ubuntu
    restart: unless-stopped
    stdin_open: true
    tty: true
    volumes:
      - prom_etc:/etc/stack/prometheus_etc
      - prom_data:/etc/stack/prometheus_data
      - grafana:/etc/stack/grafana_sources
      - grafana_db:/etc/stack/grafana_db
      - otel_config:/etc/stack/otel_config

  otel-collector:
    image: otel/opentelemetry-collector-contrib
    container_name: otel-collector
    volumes:
      - otel_config:/etc/otelcol-contrib/
    ports:
      - 1888:1888 # pprof extension
      - 8888:8888 # Prometheus metrics exposed by the Collector
      - 8889:8889 # Prometheus exporter metrics
      - 13133:13133 # health_check extension
      - 4317:4317 # OTLP gRPC receiver
      - 4318:4318 # OTLP http receiver
      - 55679:55679 # zpages extension

volumes:
  grafana:
  grafana_db:
  prom_etc:
  prom_data:
  otel_config:

Disclaimer

Rockwell Automation maintains these repositories as a convenience to you and other users. Although Rockwell Automation reserves the right at any time and for any reason to refuse access to edit or remove content from this Repository, you acknowledge and agree to accept sole responsibility and liability for any Repository content posted, transmitted, downloaded, or used by you. Rockwell Automation has no obligation to monitor or update Repository content

The examples provided are to be used as a reference for building your own application and should not be used in production as-is. It is recommended to adapt the example for the purpose, observing the highest safety standards.

About

How to create a Prometheus endpoint to expose custom metrics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages