Converting API Monitor Metrics to JSON: Addressing Carriage Return and Line Feed Challenges in InterSystems IRIS Integration
Hello,
First of all thanks for your help, time, and answers.
We would like to know what are we doing wrong and how could we improve it and fix it.
We need to convert the Api Monitor Metrics which are a String with this format:
iris_cache_efficiency 13449.122
iris_cpu_pct{id="CSPDMN"} 0
iris_cpu_pct{id="ECPWorker"} 0
[...]
iris_wdwij_time 11
iris_wd_write_time 8
iris_wij_writes_per_sec 0
To JSON.
We would expect them to look like a normal JSON as follows:
{
"iris_cache_efficiency": "13449.122",
"iris_cpu_pct{id='CSPDMN'}": "0",
[...]
"iris_wij_writes_per_sec": 0
}
We have currently developed a REST Operation which gets them and tries to convert them from the raw string to a JSON format:
Class Operaciones.REST.MetricasApiMonitorv01r00 Extends EnsLib.REST.Operation
{
Parameter INVOCATION = "Queue";
Method obtenerMetricas(pRequest As Mensajes.Request.Metricas.ObtenerRequest, pResponse As Mensajes.Response.Metricas.ObtenerResponse) As%Library.Status
{
//Creamos Request y Response HTTPSet httpRequest=##class(%Net.HttpRequest).%New()
set tResponse = ##class(%Net.HttpResponse).%New()
set pResponse = ##class(Mensajes.Response.Metricas.ObtenerResponse).%New()
// Se obtiene la URL de la configurada en la Producción;set URL = ..Adapter.URLset URL = "http://[Ip]]:[Port]]/api/monitor/metrics"$$$LOGINFO("URL: "_URL)
//Enviamos al sistema externoset tSC=httpRequest.Get(URL,0)
$$$LOGALERT("tSC: "_$System.Status.GetErrorText(tSC))
//Lanzamos excepcion si hubo errorif$$$ISERR(tSC){
$$$ThrowOnError(tSC)
}
set tResponse = httpRequest.HttpResponse
set linea = ""//Leemos respuestawhile (tResponse.Data.AtEnd = 0) {
set linea = linea_tResponse.Data.Read()
}
$$$LOGINFO("linea: "_linea)
set lineaSinComillasDobles = $REPLACE(linea,"""","'")
$$$LOGINFO("lineaSinComillasDobles: "_lineaSinComillasDobles)
set lineaConDobleComillaDosPuntosEnMedio = $REPLACE(lineaSinComillasDobles," ",""": ")
$$$LOGINFO("lineaConDobleComillaDosPuntosEnMedio: "_lineaConDobleComillaDosPuntosEnMedio)
$$$LOGALERT("$FIND(lineaConDobleComillaDosPuntosEnMedio,$CHAR(13,10)): "_$FIND(lineaConDobleComillaDosPuntosEnMedio,$CHAR(13,10)))
set lineaConComasAlFinal = $REPLACE(lineaConDobleComillaDosPuntosEnMedio, $CHAR(13,10),",")
$$$LOGINFO("lineaConComasAlFinal: "_lineaConComasAlFinal)
set pResponse.resultado = "{"_lineaConComasAlFinal_"}"Quit pResponse
}
XData MessageMap
{
<MapItems>
<MapItem MessageType="Mensajes.Request.Metricas.ObtenerRequest">
<Method>obtenerMetricas</Method>
</MapItem>
</MapItems>
}
}
However we do not know hot to replace Carrie Return and Line Feed with a comma and a double quote.
We have tried:
$$$LOGALERT("$FIND(lineaConDobleComillaDosPuntosEnMedio,$CHAR(13,10)): "_$FIND(lineaConDobleComillaDosPuntosEnMedio,$CHAR(13,10)))
set lineaConComasAlFinal = $REPLACE(lineaConDobleComillaDosPuntosEnMedio, $CHAR(13,10),",")
$$$LOGINFO("lineaConComasAlFinal: "_lineaConComasAlFinal)
However the $FIND outputs "0", so we think it means that is does not find a Carrie Return and Line Feed at all.
Even more the $REPLACE outputs no effect.
The response currently shows:
{iris_cache_efficiency": 13492.868
iris_cpu_pct{id='CSPDMN'}": 0
iris_cpu_pct{id='CSPSRV'}": 1
[...]
iris_wdwij_time": 24
iris_wd_write_time": 23
iris_wij_writes_per_sec": 0}
Being outputted at the visual trace:
.png)
How could we convert the API Monitor Metrics to JSON in an effective way?
What have we done wrong?
How could we improve and fix our code to accomplish this need?
In addition we have also read:
https://community.intersystems.com/post/replace-carriage-return-linefee…
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
Thanks for your help
.png)
.png)
.png)
Comments
I have tested the metrics rest/api and seems that the terminator that is being used is $C(10)
set lineaConComasAlFinal = $REPLACE(lineaConDobleComillaDosPuntosEnMedio, $CHAR(10),",")
I think you need more quotes for "proper" JSON
ineaConComasAlFinal: iris_cpu_pct{id='AUXWD'}": 0,iris_cpu_pct{id='CSPSRV'}": 0,
proper JSON is
{
"iris_cpu_pct{id='AUXWD'}": 0,"iris_cpu_pct{id='CSPSRV'}": 0,...
}
Thanks @Yaron Munzand @Oliver Wilms
for your help because as you have stated the terminator that is being used is $CHAR(10), which is the line feed.
We have tested it and it worked correctly.
Again thanks for your help, time and replies they have helped us a lot.