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

How to create custom metrics in AppDynamics by C# program

Ronny.Ellingsen
Wanderer

Hi

 

I have installed the AppDynamics Extension Service for DotNet Agent which have the possibility to kick off custom scripts through its WIndowsScriptMonitor.

 

I was just wondering if it is possible to write the scripts in C#?

 

I tried yesterday, and it seemed to work, but trying again today, nothing works.

I am making trying something like this:

 

// This variant with hardcoded values seemed to work yesterday (but not today)

Console.WriteLine("metricA | metricReporter , value = 12345");

 

// Other approaches with different types of Integers, Float, Double hardcoded or through variables wont work
Console.WriteLine("metricB | metricReporter , value = " + 2);

Console.WriteLine("metricB | metricReporter , value = " + intVariable);

 

Controller doesnt seem to report anything other than zero values.

Anyone tried something similar who are able to help out?

 

3 REPLIES 3

Anurag.Bajpai
AppDynamics Team

Hi Ronny, 

Thanks for trying .NET extension service. 

I am assuming you have created a standalone application in C# and calling that standalone appliication using cmd file. Basically you will still call scripit.cmd file, and in .cmd file you can call your exe. 

Let me know if this is not correct. 

 

This approach should work normally. You should be able to look into extension log file for any warning like, incorrect format received and it will try to print the line from cmd output. 

 

There could be couple of issues- 

- AppDynamics.Agent.Coordinator is not restarted after rerstarting AppDynamics.Agent.Extension service. 

- Format of line outtput is not same as expected. It is space sensitive (but case insensitive)

 

Apart from that, have you looked into using DLL based custom extension. You can create a class library project, using nuget package "AppDynamics.Agent.ExtensionSdk". Let me know if you need more details about this. 

 

Anurag 

Hi Anurag

 

Thanks for the reply :)

 

Yes, i made a standalone application in C#.

First i made it just like the demo my-script.cmd which came with the download.

I just copypasted the string into my C# program changing to other numbers, and then writing it to stdout with Console.WriteLine, like this:

 

 

Console.WriteLine("metric1 | instance1, value = 123456789");
Console.WriteLine("metric2 | instance1, value = 54321");

 

 

I did not run my C# application through a cmd script, but i did receive those new numbers i changed to into my application in the controller (so it seemed to work).

 

The next day i tried again doing it with various primitives(numbers), both from variables and hardcoded, concatenated into the string, like this:

 

 

string datoStreng = datetime.ToString();

string[] splittedFullDate = datoStreng.Split(' ');
string dato = splittedFullDate[0];
string tid = splittedFullDate[1];

string[] strippedTime = tid.Split(':');

string klokka = String.Join("", strippedTime);
int tidToInt32 = System.Convert.ToInt32(klokka);

string test = $"int32 | ronnyscript , value = {tidToInt32}";
Console.WriteLine("int16 | ronnyscript, value = " + "123456789");

Console.WriteLine($"stringvariabel | ronnyscript , value = {klokka}");
Console.WriteLine(test);

Console.WriteLine("hardkodetint | ronnyscript , value = " + 1337);
Console.WriteLine("hardkodetlong | ronnyscript , value = " + 777L);
Console.WriteLine("hardkodetdouble | ronnyscript , value = " + 3.14);
Console.WriteLine("metricx | ronnyscript , value = " + 3.14);
Console.WriteLine("metricy | ronnyscript , value = 78");

 

None of this worked, even though the last line of code was just like one of those which worked the day before.

 

Checking the logs a little bit later i see an error message stating i have reached some sort of metric limit of a maximum of 200 metrics.

 

Do you think maybe i have reached some sort of limit, or do you think all of this is because i didnt run my C# application from a cmd script?

 

Also i would like to get as much information about this as you can offer me.

I am also interested in DLL based custom extension.

 

And thank you again.

 

Kind Regards

Ronny Ellingsen

Hi Ronny, 

 

Your setup looks good, I believe metric limit was causing problem here. By default, the .net machine agent registers a maximum of 200 metrics. See Metrics Limits for more details. We can increase this by adding <metrics> element under machine agent, 

<machine-agent>
<metrics max-metrics="300"/>
</machine-agent>

 

Regarding DLL based extension, here are quick steps- 

- Create a new class lib project (c#) 

- install nuget package for the application- 

   Install-Package AppDynamics.Agent.ExtensionSdk -Version 1.0.4

- This will add two class files in the project. For custom metric, you can write your code in SampleMetricExtension.CS file. 

- You can add more references as needed to write your operation.

- Under <ExtensionManager>/extensions folder, create a new folder with name "CustomCodeExtension" (or anything)

- Create extension.xml file with following content- 

 

<?xml version="1.0" encoding="utf-8"?>
<extension type="Metric" name="CustomCodeExtension" enabled="true">
  <execution mode="Periodic" type="DLL" path="SampleMetricExtension" />
  <description>Optional description for the custom extension.</description>
  <metrics>
  	<metric name="metric1" />
  </metrics>
  <instances>
    <instance name="instance1" />
  </instances>  
  <parameters>
    <add key="key1" value="value1" />
  </parameters>
</extension>

In above file- 

 

 

Spoiler

- name="CustomMetricExtension" >> is the name of extension, it can be same as folder name

- path="SampleMetricExtension" >> is fully classified class name of extension class, basically ayy class extending AExtensionBase in custom extension project. 

- You can set any number of metrics or instances and fill their value in execute method of SampleMetricExtension class. 

 

- Compile the project and copy output assembly to the extension folder <ExtensionManager>/extensions/CustomCodeExtension 

- Now restart extension service followed by restart of coordinator service

 

Let me know if you face any issues while creating this extension or have more questions. 

 

Thanks,

Anurag