Exemplo n.º 1
0
        private static async System.Threading.Tasks.Task TestUdpSendAsync()
        {
            Console.WriteLine("TestUdpSendAsync start:");
            int         port       = 14231;
            IPHostEntry ipHost     = Dns.GetHostEntry("localhost");
            IPAddress   ipAddr     = ipHost.AddressList[1];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);
            string      address    = $"udp://{ipAddr}:{port}";

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                socket.Bind(ipEndPoint);
                byte[]        bytes        = new byte[1024];
                var           bytesRecTask = socket.ReceiveAsync(bytes, SocketFlags.None);
                MetricMessage msg          = new MetricMessage("mymeasure", "ping", 0.03d);
                string        sample       = msg.ToString();
                var           transport    = TgfClientProvider.GetTransport(address);
                TgfClient     client       = new TgfClient(transport);
                client.SendMetric("mymeasure", "ping", 0.03d);
                transport.Disconnect();
                int    bytesRec = await bytesRecTask;
                string msgRcv   = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (!sample.Equals(msgRcv.Trim()))
                {
                    throw new Exception($"TestUdpSendAsync: {msgRcv} not equal {sample}");
                }
            }
            Console.WriteLine("TestUdpSendAsync end");
        }
Exemplo n.º 2
0
        private static async System.Threading.Tasks.Task TestUnixgramSendAsync()
        {
            Console.WriteLine("TestUnixgramSendAsync start:");
            string   filepath   = "/tmp/udp.sock";
            EndPoint ipEndPoint = new UnixDomainSocketEndPoint(filepath);
            string   address    = $"unixgram://{filepath}";

            try { File.Delete(filepath); } catch (Exception) { };
            using (Socket socket = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified))
            {
                socket.Bind(ipEndPoint);
                byte[]        bytes        = new byte[1024];
                var           bytesRecTask = socket.ReceiveAsync(bytes, SocketFlags.None);
                MetricMessage msg          = new MetricMessage("mymeasure", "ping", 0.03d);
                string        sample       = msg.ToString();
                var           transport    = TgfClientProvider.GetTransport(address);
                TgfClient     client       = new TgfClient(transport);
                client.SendMetric("mymeasure", "ping", 0.03d);
                transport.Disconnect();
                int    bytesRec = await bytesRecTask;
                string msgRcv   = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (!sample.Equals(msgRcv.Trim()))
                {
                    throw new Exception($"TestUnixgramSendAsync: {msgRcv} not equal {sample}");
                }
            }
            try { File.Delete(filepath); } catch (Exception) { };
            Console.WriteLine("TestUnixgramSendAsync end");
        }
Exemplo n.º 3
0
        public void ScopeMetric(Action <MetricMessage> metric, IDictionary <string, string> scopeProperties = null, IDictionary <string, string> properties = null)
        {
            telemetryScopedProperties.SetScopeProperties(scopeProperties);

            var saveScoped       = RouteBinding?.Logging?.ScopedLogger?.LogMetric == true;
            var saveScopedStream = RouteBinding?.Logging?.ScopedStreamLoggers?.Where(l => l.LogMetric).Any() == true;

            if (saveScoped || saveScopedStream)
            {
                var messageData = new MetricMessage();
                metric(messageData);

                if (saveScoped)
                {
                    telemetryLogger.Metric(messageData.Message, messageData.Value, properties);
                }

                if (RouteBinding?.Logging?.ScopedStreamLoggers?.Count > 0)
                {
                    foreach (var scopedStreamLogger in RouteBinding.Logging.ScopedStreamLoggers.Where(l => l.LogMetric))
                    {
                        telemetryScopedStreamLogger.Metric(scopedStreamLogger, messageData.Message, messageData.Value, telemetryScopedProperties.Properties.ConcatOnce(properties));
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void HandleGatherMetricsMessage(GatherMetricsMessage message)
        {
            //
            // Gather the next metric value and send the message to the subscribers
            //
            var msg = new MetricMessage(_seriesName, _performanceCounter.NextValue());

            _subscriptions.ForEach(x => x.Tell(msg));
        }
Exemplo n.º 5
0
 private void HandleMetrics(MetricMessage metric)
 {
     if (!string.IsNullOrEmpty(metric.Series) &&
         _seriesIndex.ContainsKey(metric.Series))
     {
         var series = _seriesIndex[metric.Series];
         series.Points.AddXY(xPosCounter++, metric.CounterValue);
         while (series.Points.Count > MaxPoints)
         {
             series.Points.RemoveAt(0);
         }
         SetChartBoundaries();
     }
 }
Exemplo n.º 6
0
        private static async System.Threading.Tasks.Task TestDIAsync()
        {
            Console.WriteLine("TestDIAsync start:");
            int         port       = 14230;
            IPHostEntry ipHost     = Dns.GetHostEntry("localhost");
            IPAddress   ipAddr     = ipHost.AddressList[1];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);
            string      address    = $"tcp://{ipAddr}:{port}";

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(ipEndPoint);
                socket.Listen(1);
                var           acceptTask = socket.AcceptAsync();
                MetricMessage msg        = new MetricMessage("mymeasure", "ping", 0.03d);
                string        sample     = msg.ToString();
                Console.WriteLine("...start host:");
                if (!File.Exists("appsettings.json"))
                {
                    Console.WriteLine("ERR: {0} not found", "appsettings.json");
                }
                var builder = new HostBuilder()
                              .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                })
                              .ConfigureServices((hostContext, services) =>
                {
                    services.AddTgfClient(hostContext.Configuration);
                    services.AddHostedService <TcpSendService>();
                });
                builder.Build().Run();
                // wait ctrl+c
                Console.WriteLine("...host finished:");
                var    handler  = await acceptTask;
                byte[] bytes    = new byte[1024];
                int    bytesRec = handler.Receive(bytes);
                string msgRcv   = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                if (!sample.Equals(msgRcv.Trim()))
                {
                    throw new Exception($"TestDIAsync: {msgRcv} not equal {sample}");
                }
            }
            Console.WriteLine("TestDIAsync end");
        }
Exemplo n.º 7
0
        private static void TestMessagePacking()
        {
            string        sample = "{\"name\":\"mymeasure\",\"ping\":\"0.03\"}";
            MetricMessage msg    = new MetricMessage("mymeasure", "ping", 0.03d);

            if (!sample.Equals(msg.ToString()))
            {
                throw new Exception($"TestMessagePacking: {msg.ToString()} not equal {sample}");
            }
            sample = "{\"name\":\"mymeasure\",\"ping\":\"0.03\",\"mytag\":\"old\"}";
            msg.AddTag("mytag", "old");
            if (!sample.Equals(msg.ToString()))
            {
                throw new Exception($"TestMessagePacking: {msg.ToString()} not equal {sample}");
            }
            JObject o = JObject.Parse(sample);
        }
Exemplo n.º 8
0
        private void HandleMetricsPaused(MetricMessage message)
        {
            if (!string.IsNullOrEmpty(message.Series) && _seriesIndex.ContainsKey(message.Series))
            {
                var series = _seriesIndex[message.Series];
                if (series.Points == null)
                {
                    return;                               // means we're shutting down
                }
                series.Points.AddXY(xPosCounter++, 0.0d); //set the Y value to zero when we're paused

                while (series.Points.Count > MaxPoints)
                {
                    series.Points.RemoveAt(0);
                }

                SetChartBoundaries();
            }
        }
Exemplo n.º 9
0
        public void Send(MetricMessage message, bool flush = true)
        {
            if (transportStream == null)
            {
                throw new IOException("No transport stream exists");
            }

            using (MemoryStream memoryStream = new MemoryStream())
            {
                var datagramBytes = message.ByteSerialize();
                memoryStream.Write(datagramBytes, 0, datagramBytes.Length);
                memoryStream.WriteByte(trailer); // LF
                transportStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
            }

            if (flush && !(transportStream is NetworkStream))
            {
                transportStream.Flush();
            }
        }
Exemplo n.º 10
0
 public void Send(MetricMessage message)
 {
     byte[] datagramBytes = message.ByteSerialize();
     udpClient.Send(datagramBytes, datagramBytes.Length);
 }
Exemplo n.º 11
0
 public void Send(MetricMessage message)
 {
     byte[] datagramBytes = message.ByteSerialize();
     socket.Send(datagramBytes);
 }
Exemplo n.º 12
0
 public void Send(MetricMessage message)
 {
     Send(message, true);
 }