Question
Scott Roth · Aug 19

SAM - Custom Application Metrics

Now that I have SAM up and running, out of the box I would like to add some Application Metrics. Does anyone have templates, or have suggested classes that someone could use to add custom metrics to SAM? Maybe we should start a repository somewhere? Or would someone be willing to share custom metrics on the Open Exchange? 

What I am looking to do is capture the overall HL7 message and header count per day.

  • SELECT Count(ID) FROM Ens.MessageHeader WHERE TimeCreated > <date> and TimeCreated < <date>
  • SELECT Count(ID) FROM EnsLib.HL7.Message WHERE TimeCreated > <date> and TimeCreated < <date>
  • SELECT Count() FROM EnsLib_HL7.Message HL7 LEFT JOIN Ens.MessageHeader hdr ON HL7.Id=hdr.MessageBodyId  WHERE hdr.MessageBodyId IS NULL AND HL7.OriginalDocId IS NULL

Thanks

Product version: IRIS 2022.1
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2022.1 (Build 209U) Tue May 31 2022 12:13:24 EDT
0
0 90
Discussion (3)1
Log in or sign up to continue

First create a class:

Class MyClass Extends %SYS.Monitor.SAM.Abstract

Add a parameter that will indicate the prefi name for al your used defined metrics.

Parameter PRODUCT = "Prefix";

Create a wrap method GetSensors() for all your user defined sensors (which can be ClassMethods):

Method GetSensors() As %Status
{
Try {
   D ..SetSensor("sensor1", ..Sensor1())
   D ..SetSensor("sensor2", ..Sensor2())
   }
Catch e { ;call your store error function g.e. ##class(anyClass).StoreError($classname(),e.DisplayString()) }
}
ClassMethod Sensor1() As %Integer
{
   ; do any calculation
   Quit Value
}
ClassMethod Sensor1() As %Integer
{
   ; do any calculation
   Quit Value
}
}
 

Now you will get by the API REST call for the /api/mertics your "user defined" sensors at names:
Prefix_sensor1 and Prefix_sensor2

Remarks:
- Make sure that your GetSensors() and all your "used defined" sensors (classmethods) have a proper error handling so they are fail safe (you may use a try/catch or any other error trap like $ZT="something")
- Make sue all your "user defined" sensors are preforming fast. This will enable the SAM metrics API REST call to get the data quickly without delays. In case some calculations are "heavy" it is better to have a separate process (task manager) to do those calculations, and store them in a global for fast data retrieval by the sensor   

What if I only want to run the metric once a day? I don't know how much of a performance hit it will be to keep on having these queries run every 5 min or so.

SAM is executing a REST API call to http://[your-server]/api/monitor/metrics for any server included in your cluster. I'm not sure where the interval is being configured.

If your own metric is needed to be run once a day, you can schedule it with the 'task manager" have the result stored in a global, and let the "user defined" sensor read this global which will not caused any performance issue.

BTW - one thing I forgot to mention in the previous post is:
In order to have SAM run your "user defined" metrics, you need to add it to SAM:

%SYS>Set sc = ##class(SYS.Monitor.SAM.Config).AddApplicationClass("MyClass", "namespace")