<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using NewRelic.Platform.Sdk;
using NewRelic.Platform.Sdk.Utils;
using Newtonsoft.Json;

namespace S203.NewRelic.NeuronEsb.Plugin
{
    public class EsbAgent : Agent
    {
        private static readonly Logger Logger = Logger.GetLogger("NeuronEsbLogger");
        private readonly Version _version = Assembly.GetExecutingAssembly().GetName().Version;
        private readonly string _name;
        private static string _host;
        private static int _port;
        private static string _instance;

        public EsbAgent(string name, string host, int port, string instance)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name), "Name must be specified for the agent to initialize");
            if (string.IsNullOrEmpty(host))
                throw new ArgumentNullException(nameof(host), "Host must be specified for the agent to initialize");
            if (string.IsNullOrEmpty(instance))
                throw new ArgumentNullException(nameof(instance), "Instance must be specified for the agent to initialize");

            _name = name;
            _host = host;
            _port = port;
            _instance = instance;
        }

        public override string Guid => "E203.NewRelic.NeuronEsb";
        public override string Version => _version.Major + "." + _version.Minor + "." + _version.Build;

        public override string GetAgentName()
        {
            return _name;
        }

        public override void PollCycle()
        {
            // Assemble URIs
            var esbUri = $"http://{_host}:{_port}/neuronesb/api/v1/";
            var endpointHealth = $"endpointhealth/{_instance}";

            var serializer = new JsonSerializer();

            // Get endpoint health from Neuron
            var uri = esbUri + endpointHealth;
            Logger.Debug("Endpoint URI: " + esbUri);
            var client = new WebClient();
            client.Headers.Add("content-Type", "Application/json");

            Logger.Debug("Getting Endpoint Health");
            var result = client.DownloadString(uri);

            Logger.Debug("Deserializing Neuron Endpoint Health");
            var data = JsonConvert.DeserializeObject<List<EndpointHealth>>(result);
            Logger.Debug("Response received:\n" + data);

            Logger.Debug("Sending Summary Metrics to New Relic");
            ReportMetric("Summary/Heartbeat", "count", data.Sum(d => d.Heartbeats));
            ReportMetric("Summary/Error", "count", data.Sum(d => d.Errors));
            ReportMetric("Summary/Warning", "count", data.Sum(d => d.Warnings));
            ReportMetric("Summary/MessageRate", "count", (float)data.Sum(d => d.MessageRate));
            ReportMetric("Summary/MessagesProcessed", "count", data.Sum(d => d.MessagesProcessed));

            Logger.Debug("Sending Individual Metrics to New Relic");
            foreach (var endpoint in data)
            {
                ReportMetric("Heartbeat/" + endpoint.Name, "count", endpoint.Heartbeats);
                ReportMetric("Error/" + endpoint.Name, "count", endpoint.Errors);
                ReportMetric("Warning/" + endpoint.Name, "count", endpoint.Warnings);
                ReportMetric("MessageRate/" + endpoint.Name, "count/second", endpoint.Heartbeats);
                ReportMetric("MessagesProcessed/" + endpoint.Name, "count", endpoint.Heartbeats);
            }
        }
    }
}<commit_msg>Updating UOM for metrics<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using NewRelic.Platform.Sdk;
using NewRelic.Platform.Sdk.Utils;
using Newtonsoft.Json;

namespace S203.NewRelic.NeuronEsb.Plugin
{
    public class EsbAgent : Agent
    {
        private static readonly Logger Logger = Logger.GetLogger("NeuronEsbLogger");
        private readonly Version _version = Assembly.GetExecutingAssembly().GetName().Version;
        private readonly string _name;
        private static string _host;
        private static int _port;
        private static string _instance;

        public EsbAgent(string name, string host, int port, string instance)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name), "Name must be specified for the agent to initialize");
            if (string.IsNullOrEmpty(host))
                throw new ArgumentNullException(nameof(host), "Host must be specified for the agent to initialize");
            if (string.IsNullOrEmpty(instance))
                throw new ArgumentNullException(nameof(instance), "Instance must be specified for the agent to initialize");

            _name = name;
            _host = host;
            _port = port;
            _instance = instance;
        }

        public override string Guid => "E203.NewRelic.NeuronEsb";
        public override string Version => _version.Major + "." + _version.Minor + "." + _version.Build;

        public override string GetAgentName()
        {
            return _name;
        }

        public override void PollCycle()
        {
            // Assemble URIs
            var esbUri = $"http://{_host}:{_port}/neuronesb/api/v1/";
            var endpointHealth = $"endpointhealth/{_instance}";

            var serializer = new JsonSerializer();

            // Get endpoint health from Neuron
            var uri = esbUri + endpointHealth;
            Logger.Debug("Endpoint URI: " + esbUri);
            var client = new WebClient();
            client.Headers.Add("content-Type", "Application/json");

            Logger.Debug("Getting Endpoint Health");
            var result = client.DownloadString(uri);

            Logger.Debug("Deserializing Neuron Endpoint Health");
            var data = JsonConvert.DeserializeObject<List<EndpointHealth>>(result);
            Logger.Debug("Response received:\n" + data);

            Logger.Debug("Sending Summary Metrics to New Relic");
            ReportMetric("Summary/Heartbeat", "checks", data.Sum(d => d.Heartbeats));
            ReportMetric("Summary/Error", "msg", data.Sum(d => d.Errors));
            ReportMetric("Summary/Warning", "msg", data.Sum(d => d.Warnings));
            ReportMetric("Summary/MessageRate", "msg", (float)data.Sum(d => d.MessageRate));
            ReportMetric("Summary/MessagesProcessed", "msg", data.Sum(d => d.MessagesProcessed));

            Logger.Debug("Sending Individual Metrics to New Relic");
            foreach (var endpoint in data)
            {
                ReportMetric("Heartbeat/" + endpoint.Name, "checks", endpoint.Heartbeats);
                ReportMetric("Error/" + endpoint.Name, "msg", endpoint.Errors);
                ReportMetric("Warning/" + endpoint.Name, "msg", endpoint.Warnings);
                ReportMetric("MessageRate/" + endpoint.Name, "msg", endpoint.Heartbeats);
                ReportMetric("MessagesProcessed/" + endpoint.Name, "msg", endpoint.Heartbeats);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11100196463654224, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 2.431391429947039, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7549901', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1794, 'n_words': 469}