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
.
There are multiple ways to download this project, here is the recommended one
- Click on the green
CODE
button in the top right corner - Select
HTTPS
and copy the provided URL - Open FT Optix IDE
- Click on
Open
and select theRemote
tab - Paste the URL from step 2
- Click
Open
button in bottom right corner to start cloning process
- FactoryTalk Optix 1.5.x or later (
.NET8
is required) - .NET Core SDK
- Prometheus
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)
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
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"]
The following metrics are exposed:
FTOptix_Model_Variable1
: Variable1 from Model folderFTOptix_Diagnostics_totalCpuUsagePercent
: Total CPU usage percentFTOptix_Diagnostics_totalRamUsageMegaBytes
: Total RAM utilization in MBFTOptix_Diagnostics_processCpuUsagePercent
: CPU usage percent of the current processFTOptix_Diagnostics_processMemoryUsageMegaBytes
: Memory usage of the current process in MB
- 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.
- 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.
- GetProcessMemoryUsage: Retrieves the memory usage of the current process.
- GetTotalMemoryUsage: Retrieves the total memory usage, with platform-specific implementations for Windows, Linux, and macOS.
- Build and run the project.
- Access the metrics at
http://ipaddress:1234/metrics
.
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.
Errors during metric refresh are logged using the Log.Error
method, these are shown to the FactoryTalk Optix logs.
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:
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.