Question
· Nov 28, 2023

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 HTTP
	Set 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.URL
	set URL = "http://[Ip]]:[Port]]/api/monitor/metrics"
	$$$LOGINFO("URL: "_URL)

	//Enviamos al sistema externo
	set tSC=httpRequest.Get(URL,0) 
	$$$LOGALERT("tSC: "_$System.Status.GetErrorText(tSC))

	//Lanzamos excepcion si hubo error
	if $$$ISERR(tSC){
			$$$ThrowOnError(tSC)
	}
	
	set tResponse = httpRequest.HttpResponse
	
	set linea = ""
	//Leemos respuesta
	while (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:

 

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-linefeed...

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

 

Thanks for your help

Product version: IRIS 2020.1
Discussion (4)1
Log in or sign up to continue