cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Extension Metric

Gustavo_Godinho
Frequent Guest

Hello everyone,

 

Recently I've been editing an existing script in which send POST method into Dynatrace. As you can see in the code below, we are inserting data from a .csv file. The original code with its metrics already created in Dynatrace works fine. However, when I change the timeseriesId name, I get the following error:

 

"2022-03-04 14:58:12,648 Erro ao incluir o payload: {'error': {'code': 400, 'message': 'Constraints violated.', 'constraintViolations': [{'path': 'series[0].timeseriesId', 'message': 'No configuration found for timeseries id.', 'parameterLocation': 'PAYLOAD_BODY', 'location': None}]}}, Json: {"displayName": "Inclus\u00e3o Proposta", "group": "XXXXXX", "type": "DMPS", "properties": {"u_name_abbreviation": "TZ", "path": "D:\\DynatraceExtension", "exe": "readCSV.py", "ServerExtension": "n2431237"}, "configUrl": "XXXXXX", "tags": ["TZ"], "series": [{"timeseriesId": "custom:motors_dmps_response_time_inc", "dimensions": {}, "dataPoints": "

 

import pandas
import sys
import os
import json
import requests
import logging
from datetime import datetime
from pathlib import Path
import platform
import requests
import numpy as np

HEADER = {'Authorization':'Api-Token XXXXX','Content-Type':'application/json; charset=utf-8'}
logging.basicConfig(filename='readCSV.log', level=logging.INFO, format='%(asctime)s %(message)s')
directory = Path(r'XXXXXXX')
lockfile_history = 'lastline_history.txt'


def eventValidation(payload):
logging.info('Metodo eventValidation')
req = requests.Request('POST', 'https://dynatracewebpre.dcbr01.corp/e/XXXXXX/api/v1/events', headers=HEADER, data=json.dumps(payload))
response = requests.request(req.method, req.url, data=req.data, headers=req.headers, verify=False)
if response.status_code != 200:
logging.error('Erro ao incluir o payload: %s, Json: %s',response.json(),json.dumps(payload))

def sendMetrics(timeseriesId,dimension,datapoints):
logging.info('Metodo sendMetrics')
payload = {
'displayName': 'Inclusão Proposta',
'group': 'XXXXXXX',
'type': 'DMPS',
'properties': {'u_name_abbreviation': 'TZ','path':'D:\dynatrace','exe':'readCSV.py','ServerExtension':platform.uname()[1]},
'configUrl':'XXXXXXX',
'tags': ['TZ'],
'series': [
{
'timeseriesId': 'custom:'+timeseriesId,
'dimensions': dimension,
'dataPoints': datapoints
}]
}
req = requests.Request('POST', 'https://dynatracewebpre.dcbr01.corp/e/XXXXXXXXXXX/api/v1/entity/infrastructure/custom/dmps_motorsInc', headers=HEADER, data=json.dumps(payload))
response = requests.request(req.method, req.url, data=req.data, headers=req.headers, verify=False)
if response.status_code != 200:
logging.error('Erro ao incluir o payload: %s, Json: %s',response.json(),json.dumps(payload))

def cd_situ_prop_flux(group_name,group_filter,group_returncode):
logging.info('Metodo cd_situ_prop_flux')
timeseriesId = 'motors_requests_inc'
dimension = {'CD_ERRO_PROC':str(group_returncode), 'CD_MOTO_PROC':group_name}
data_groups = df[group_filter].groupby(['DH_INCL_MOVI'])
datapoints=[]
for timestamps_group, metrics in data_groups:
timestamp = str(datetime.strptime(timestamps_group,'%Y-%m-%d-%H:%M').timestamp()).replace(".", "")
datapoints.append([int(timestamp[0:10]+'000'),len(metrics)])
sendMetrics(timeseriesId,dimension,datapoints)

def send_responseTime(motor,metric):
dimension = {}
logging.info('Metodo send_responseTime, motor:%s',motor)
if motor == 'ADT':
timeseriesId = 'motors_dmps_response_time_inc'
metricColumn = 'SS_RSPT_DMPS'
if motor == 'RCS':
timeseriesId = 'motors_cntg_response_time_inc'
metricColumn = 'SS_RSPT_CNTG'
datapoints=[]
metricList = metric[['DH_INCL_MOVI',metricColumn]].values.tolist()
for datametrics in metricList:
timestamp = str(datetime.strptime(datametrics[0],'%Y-%m-%d-%H:%M').timestamp()).replace(".", "")
datapoints.append([int(timestamp[0:10]+'000'),datametrics[1]*1000])
sendMetrics(timeseriesId,dimension,datapoints)

def select_lastFiles():
logging.info('Metodo select_lastFiles')
data_criacao = lambda f: f.stat().st_ctime
data_modificacao = lambda f: f.stat().st_mtime
files = directory.glob('*.csv')
return sorted(files, key=data_modificacao, reverse=True)

def delete_oldfiles(last_files):
logging.info('Metodo delete_oldfiles')
if datetime.now().strftime('%Y%m%d') not in last_files:
logging.info('Deletando arquivos no repositorio')
files = os.listdir(directory)
logging.info('Deletando Arquivos do dia anterior...')
for filecsv in files:
csvfile = f'{directory}/{filecsv}'
os.remove(csvfile)


def main():
global df
try:
List_last_files = select_lastFiles() ### Selecionando ultimo Arquivo
logging.info(List_last_files[0])
if len(List_last_files) != 0:
logging.info('Iniciando')
df=pandas.read_csv(List_last_files[0], delimiter=';')
df['DH_INCL_MOVI_1'] = df['DH_INCL_MOVI']
df['DH_INCL_MOVI'] = pandas.to_datetime(df['DH_INCL_MOVI'], format='%Y-%m-%d-%H.%M.%S.%f').dt.strftime('%Y-%m-%d-%H:%M')
df['CD_MOTO_PROC'] = df['CD_MOTO_PROC'].replace(' ', '', regex=True)
df['CD_MOTO_PROC'] = df['CD_MOTO_PROC'].replace('', 'Unprocessed')
motor_list = []
percentMotores = (df['CD_MOTO_PROC'].groupby(df['CD_MOTO_PROC']).count()/df['CD_MOTO_PROC'].groupby(df['CD_MOTO_PROC']).count().sum())*100
if 'RCS' in percentMotores.index:
if percentMotores['RCS'] > 3:
payload = {
"eventType": "ERROR_EVENT",
"attachRules": {
"entityIds": [
"CUSTOM_DEVICE-879884799A9C475A"
]},
"customProperties": {
"Sigla":"TZ",
"Mensagem de Erro": "Execucao no motor de Contingencia Acima de 3% nos ultimos 15min"
},
"source": "PropostaMF",
"description": "Baixo desempenho na execucao do motor do ADT",
"title": "Alto volume no Motor Contingencia"
}
eventValidation(payload)
# dif_incl_envidmps(df)
for motor_name,metricMotors in df.groupby(['CD_MOTO_PROC']):
motor_list.append(motor_name)
if motor_name !='Unprocessed':
send_responseTime(motor_name,metricMotors)

else:
for metricList in metricMotors[['DH_INCL_MOVI','CD_PROP','DS_MESG_ERRO','CD_ERRO_PROC','CD_ERRO_SERV']].values.tolist():
payload = {
"eventType": "CUSTOM_ANNOTATION",
"start": int(str(datetime.strptime(metricList[0],'%Y-%m-%d-%H:%M').timestamp()).replace(".", "")[0:10]+'000'),
"attachRules": {
"entityIds": [
"CUSTOM_DEVICE-879884799A9C475A"
]},
"customProperties": {
"Proposta":str(metricList[1]),
"Mensagem de Erro": metricList[2]
},
"source": "PropostaMF",
"annotationType": str(metricList[3]),
"annotationDescription": metricList[4]
}
eventValidation(payload)
for retcode, datails in df.groupby(['CD_ERRO_PROC']):
for motor in motor_list:
filter_group = (df['CD_MOTO_PROC']==motor) & (df['CD_ERRO_PROC']==retcode)
cd_situ_prop_flux(motor,filter_group,retcode)
else:
logging.info('Nao possui arquivos a serem lidos')
delete_oldfiles(str(List_last_files[0])) ### Deletando arquivos antigos
logging.info('Processo concluido com sucesso!')
except:
logging.exception("Unexpected error: %s", sys.exc_info()[0])

if __name__ == "__main__":
main()

 

I know we must create an extension metric, like: "ext: motors_dmps_response_time_inc". However, I'm not sure how to do that. 

 

So I would like to know how to do this.

3 REPLIES 3

Mike_L
Dynatrace Champion
Dynatrace Champion

Hi, this is the API to create 1.0 metrics in Dynatrace.

https://www.dynatrace.com/support/help/dynatrace-api/environment-api/metric-v1/custom-metrics/put-cu...

I'd recommend you to move over to 2.0 though with the ingest metric API as it doesn't require declaring it first.

https://www.dynatrace.com/support/help/dynatrace-api/environment-api/metric-v2/post-ingest-metrics

It'd require you to use generic entities though instead of custom devices, so it'd be a bigger rewrite.

Mike

Gustavo_Godinho
Frequent Guest

This is exactly what I needed. However, I want to know how I define the dimesion in payload. I tried to fill it out, but I ended up getting the following error:

 

"Erro ao incluir o payload: {'error': {'code': 400, 'message': 'Constraints violated.', 'constraintViolations': [{'path': 'series[0].dimensions', 'message': "Dimensions don't match dimensions specified in configuration. Allowed extra dimensions from the configuration are: Inclusao_Proposta", 'parameterLocation': 'PAYLOAD_BODY', 'location': None}]}}, Json: {"displayName": "Inclus\u00e3o Proposta", "group": "TZ - Administracao de Propostas Santander Financiamentos", "type": "DMPS", "properties": {"u_name_abbreviation": "TZ", "path": "D:\\DynatraceExtension", "exe": "readCSV.py", "ServerExtension": "n2431237"}, "configUrl": "https://confluence.santanderbr.corp/pages/viewpage.action?pageId=409685379", "tags": ["TZ"], "series": [{"timeseriesId":"

 

So, could you tell me what I should fill it out with, based on my code above?

 

Thanks

Hi @Gustavo_Godinho,

 

you need to make sure the types list in the custom metric has also the same type as the custom device you try pushing your metric to. You may need to update the custom metric 

Also in order to push metrics to custom devices you will need to do through with the timeseries field on the custom device, basically refreshing the custom device as well.

 

Hope this helps.

Best,

Mark