Prometheus Metrics

46 views
Skip to first unread message

Neha Dalal

unread,
Jan 21, 2021, 5:50:06 AM1/21/21
to Prometheus Users
I'm new bee and started writing exporter that will use jenkins to pull out build executed timestamp and put it on grafana.
I'm able to write script as below:

#!/usr/bin/python3

import json
import time
from urllib.request import urlopen
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY
#import requests
#from datetime import datetime
import datetime

class JenkinsCollector(object):
  def collect(self):
    metric = GaugeMetricFamily(
        'jenkins_job_deployment_frequency',
        'Jenkins job deployment frequency',
        labels=["jobname"])

   # jenkins_url = "http://localhost:8083"
   # username = "admin"
   # password = "admin"
   # job_name = "test"
   # request_url = "{0:s}/job/{1:s}/api/json{2:s}".format(
   # jenkins_url,
   # job_name,
   # "?tree=builds[fullDisplayName,id,number,timestamp,url]")

   # response = requests.get(request_url, auth=(username, password)).json()
   # builds = []



    for build in result['builds']:
       #build_date = datetime.utcfromtimestamp(build['timestamp']/1000).strftime('%Y-%m-%d %H:%M:%S')
       #build_date  = int(float(datetime.datetime.build['timestamp']) * 1000)
       #build_date = build['timestamp']/1000
       #date_time_obj = datetime.strptime(build_date, '%Y-%m-%d %H:%M:%S')
       #date_time_obj = datetime.fromisoformat(build_date)
       #print build_date
        fullDisplayName = build["fullDisplayName"]
        number = build["number"]
       #build_url = format(build["url"])
        build_date = format(build["timestamp"])
       #build_date = format(build_date)
       #metric.add_metric([fullDisplayName], date_time_obj)
       #build_date = covert_date_to_epoch(build['timestamp'])
       #build_date = int(time.mktime(time.strptime(build_date, '%Y-%m-%d %H:%M:%S')))
        metric.add_metric([fullDisplayName],build_date,value=number)
       #timestamp = int(float(datetime.datetime.now().timestamp()) * 1000)
    yield metric

if __name__ == "__main__":
  REGISTRY.register(JenkinsCollector())
  start_http_server(8089)
  while True: time.sleep(1)

I get following output,
jenkins_job_deployment_frequency{jobname="test2"}1610623189647  9.0 jenkins_job_deployment_frequency{timestamp="test2"}1610555634393 8.0 jenkins_job_deployment_frequency{timestamp="test2"}1610555615120 7.0 jenkins_job_deployment_frequency{timestamp="test2"}1610014768328 6.0 jenkins_job_deployment_frequency{timestamp="test2"}1609824305557 5.0 jenkins_job_deployment_frequency{timestamp="test2"}1609823502624 4.0 jenkins_job_deployment_frequency{timestamp="test2"}1609741464774 3.0 jenkins_job_deployment_frequency{timestamp="test2"} 1609741438261 2.0 jenkins_job_deployment_frequency{timestamp="test2"} 1607961178782 1.0  

I'm just not sure how to format it to make stat report on grafana that maps with timestamp i received from jenkins in prometheus.  My final goal is to get result as job name, timestamp and build number where timestamp from jenkins will map to time range field on grafana and with selection i will get to see deployment frequency, like how many builds executed in that duration. I tried to add [$_interval] but i do not get to see the count result i am expecting. Can someone help me here please?
Reply all
Reply to author
Forward
0 new messages