Prometheus is a popular open source monitoring and alerting solution, originally created by SoundCloud to provide a monitoring solution for their hundreds of services, and many thousands of running instances. It has since become hugely popular, and was the second project adopted by the Cloud Native Computing Foundation after Kubernetes, which provides infrastructure for deploying and co-ordinating large numbers of scalable applications in Docker containers. Prometheus can be easily used in any environment, but is also optimised to be used in the Cloud, and is particularly well integrated into Kubernetes for monitoring application clusters.

SwiftMetrics now supports exporting data from your Swift Application into Prometheus by providing a metrics endpoint. You can run this along side the SwiftMetrics Dashboard or by itself.

The Prometheus metrics endpoint is made available on your server at /metrics, and the Prometheus server is configured to collect data from all the instances of your application that you may have deployed for reliability or scalability.

To add a Prometheus endpoint to your Kitura application:

  1. Add SwiftMetrics to the dependencies in your Package.swift file
    import PackageDescription
    
    let package = Package(
        name: "MyServer",
        dependencies: [
          .package(url: "https://github.com/IBM-Swift/Kitura.git", .branch("issue.swift4")),
          .package(url: "https://github.com/IBM-Swift/HeliumLogger.git", .upToNextMinor(from: "1.7.1")),
          .package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", .upToNextMinor(from: "4.0.5")),
          .package(url: "https://github.com/IBM-Swift/Configuration.git", .upToNextMinor(from: "1.0.0")),
          .package(url: "https://github.com/RuntimeTools/SwiftMetrics.git", .upToNextMinor(from: "1.2.5")),
          .package(url: "https://github.com/IBM-Swift/Health.git", from: "0.0.0"),
        ],
        targets: [
          .target(name: "MyServer", dependencies: [ .target(name: "Application"), "Kitura" , "HeliumLogger"]),
          .target(name: "Application", dependencies: [ "Kitura", "Configuration", "CloudEnvironment", "Health" , "SwiftMetrics",]),
        ]
    )
    
  2. Add SwiftMetrics into your application. For an application created with “kitura init” this would be in your /Sources/Application/Metrics.swift file:
    import SwiftMetrics
    import SwiftMetricsPrometheus
    
    let sm = try SwiftMetrics()   
    let router: Router
    
    // Activate the /metrics endpoint.
    let _ = try SwiftMetricsPrometheus(swiftMetricsInstance: sm, endpoint: router)
    
    // Add an HTTP server and connect it to the router
    Kitura.addHTTPServer(onPort: 8080, with: router)  
    

You then need to install and configure Prometheus to collect data from your application.

The Getting Started guide on Prometheus.io shows how to download, install and configure the Prometheus server.

In order to configure Prometheus to collect data from your application, you need to update the prometheus.yml file to the following:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'Swift Kitura App'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['localhost:8080']

This adds a “job” (an application to monitor) called “Swift Kitura App” which is running on localhosts:8080.

You can also use more advanced methods of service discovery which is especially useful when deploying an application into a Kubernetes cluster, see: https://prometheus.io/docs/operating/configuration/#<kubernetes_sd_config>

If you create your application using ‘kitura init’ or ‘kitura create’ options of the Kitura CLI, then the configuration for use in Kubernetes is done for you.

Once you start the prometheus server using:

prometheus -config.file=prometheus.yml

and your application is running, you can go to http://localhost:9090 to see the Prometheus dashboard. This allows you to build custom graphs and set alerts. More information on building dashboards is available from the Visualisation section of the Prometheus Docs.

Whilst the SwiftMetrics dashboard is perfect for monitoring and analysis and development and for simple deployments, Prometheus allows you to monitor multiple instances of an application at once and either graph and explore the data within Prometheus and set Alerts, or export it to visualise with an analysis package like Grafana allowing you to build feature rich dashboards:

This provides you with the ability to build real production-grade monitoring for your Swift applications that is customised for the data that you need to see, and to provide you with alerts based on your requirements.






Join the discussion on Slack Learn more at kitura.io Star Kitura on GitHub

Join The Discussion

Your email address will not be published. Required fields are marked *