Skip to content

Commit

Permalink
Transformando em metrica de prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascimento01 committed Apr 26, 2024
1 parent 8cfa678 commit 1c4870c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
newrelic-tools
56 changes: 39 additions & 17 deletions query_nr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import datetime
import json
import os
import time

import requests

from prometheus_client import Gauge, start_http_server


g = Gauge('Cost_of_APP', 'Cost of using new relic per application', labelnames=['appName'])

def run_nrquery(select_statement):
""""
Run the query against the New Relic GraphQL API and return the response as a string.
Expand Down Expand Up @@ -37,16 +43,16 @@ def extract_results(json_content):
output = json.loads(json_content)['data']['actor']['account']['nrql']['results']
return output

def generate_db_insert(list_of_dicts, current_time):
""""
Generate a list of SQL INSERT statements from the list of dictionaries.
"""
query_list = []
for item in list_of_dicts:
insert_query=f"INSERT INTO app_usage (date, appName, cost) VALUES ('{current_time}', '{item['appName']}', {item['Cost']});"
query_list.append(insert_query)

return query_list
#def generate_db_insert(list_of_dicts, current_time):
# """"
# Generate a list of SQL INSERT statements from the list of dictionaries.
# """
# query_list = []
# for item in list_of_dicts:
# insert_query=f"INSERT INTO app_usage (date, appName, cost) VALUES ('{current_time}', '{item['appName']}', {item['Cost']});"
# query_list.append(insert_query)
#
# return query_list


select_statements=[
Expand All @@ -67,12 +73,28 @@ def generate_db_insert(list_of_dicts, current_time):

current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

for select_statement in select_statements:
#Run the query and extract the results
list_of_results=extract_results(run_nrquery(select_statement))
def main():
for select_statement in select_statements:
#Run the query and extract the results
list_of_results=extract_results(run_nrquery(select_statement))


for item in list_of_results:
#import ipdb
#ipdb.set_trace()
g.labels(appName=item['appName']).set(item['Cost'])


#Generate the list of SQL INSERT statements
# list_of_inserts=generate_db_insert(list_of_results, current_time)

# for insert in list_of_inserts:
# print (insert)

#Generate the list of SQL INSERT statements
list_of_inserts=generate_db_insert(list_of_results, current_time)
start_http_server(8000)

for insert in list_of_inserts:
print (insert)
while True:
main()
# import ipdb
# ipdb.set_trace()
time.sleep(100)
23 changes: 23 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
asttokens==2.4.1
certifi==2024.2.2
charset-normalizer==3.3.2
decorator==5.1.1
executing==2.0.1
idna==3.7
ipdb==0.13.13
ipython==8.24.0
jedi==0.19.1
matplotlib-inline==0.1.7
parso==0.8.4
pexpect==4.9.0
prometheus_client==0.20.0
prompt-toolkit==3.0.43
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.17.2
requests==2.31.0
six==1.16.0
stack-data==0.6.3
traitlets==5.14.3
urllib3==2.2.1
wcwidth==0.2.13

0 comments on commit 1c4870c

Please sign in to comment.