コード例 #1
0
        public void Pause()  // deconstructor (really a pauser) of a Monitor class instance
        {
            running = false;
            lock (sessionLock)
            {
                // stops TraceEvent session so events aren't being tracked
                session.Stop();
            }

            // sends an HTTP request with the data stored before pausing monitor
            // creates object that will store all event instances
            Metric_List list = new Metric_List();

            lock (lockObject)
            {
                list.session     = instance;
                list.cpu         = CPUVals;
                list.mem         = MemVals;
                list.exceptions  = ExceptionVals;
                list.requests    = RequestVals;
                list.contentions = ContentionVals;
                list.gc          = GCVals;
                list.jit         = JitVals;

                // creates new pointers for metric value collections
                CPUVals        = new List <CPU_Usage>();
                MemVals        = new List <Mem_Usage>();
                ExceptionVals  = new List <Exceptions>();
                RequestVals    = new List <Http_Request>();
                ContentionVals = new List <Contention>();
                GCVals         = new List <GC>();
                JitVals        = new List <Jit>();
            }

            String output;

            try
            {
                output = JsonConvert.SerializeObject(list);

                // escapes string so that JSON object is interpreted as a single string
                output = JsonConvert.ToString(output);

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/v1/General");
                request.Content = new StringContent(output, System.Text.Encoding.UTF8, "application/json");
                // sends POST request to server, containing JSON representation of events
                try
                {
                    HttpResponseMessage response = client.SendAsync(request).Result;
                }
                catch { }
            }
            catch { }
        }
コード例 #2
0
        /*
         * METHOD DECLARATION BLOCK
         */
        public void Record()  // sets timer that calls Collect every five seconds
        {
            // sets base address for HTTP requests - won't be hard-coded in future
            client.BaseAddress = new Uri("http://10.83.46.226:54022/");

            // assign all properties of the current process to the Session class instance
            instance.process        = (this.process);
            instance.sampleRate     = sampleRate;
            instance.sendRate       = sendRate;
            instance.processorCount = processorTotal;
            instance.os             = myOS;
            instance.application    = app;

            // starts event collection via TraceEvent in separate task if necessary
            if (ContentionEnabled == 1 | ExceptionEnabled == 1 | GCEnabled == 1 | HttpEnabled == 1 | JitEnabled == 1)
            {
                Task.Factory.StartNew(() =>
                {
                    TraceEvents();
                });
            }

            // begins loop that samples CPU/mem and issues HTTP requests in separate task
            Task.Factory.StartNew(async() =>
            {
                timer.Start();
                while (true)
                {
                    if (CPUEnabled == 1)
                    {
                        FetchCPU();
                    }
                    if (MemEnabled == 1)
                    {
                        FetchMem();
                    }

                    // if specified time has passed since HTTP request was made
                    if (timer.ElapsedMilliseconds >= sendRate)
                    {
                        timer.Restart();
                        // creates object that will store all event instances
                        Metric_List list = new Metric_List();

                        lock (lockObject)
                        {
                            list.session     = instance;
                            list.cpu         = CPUVals;
                            list.mem         = MemVals;
                            list.exceptions  = ExceptionVals;
                            list.requests    = RequestVals;
                            list.contentions = ContentionVals;
                            list.gc          = GCVals;
                            list.jit         = JitVals;

                            // creates new pointers for metric value collections
                            CPUVals        = new List <CPU_Usage>();
                            MemVals        = new List <Mem_Usage>();
                            ExceptionVals  = new List <Exceptions>();
                            RequestVals    = new List <Http_Request>();
                            ContentionVals = new List <Contention>();
                            GCVals         = new List <GC>();
                            JitVals        = new List <Jit>();
                        }

                        // starts measurement of HTTP request duration for adjustment to task delay
                        duration.Start();
                        hold = 1;

                        String output;
                        try
                        {
                            output = JsonConvert.SerializeObject(list);

                            // escapes string so that JSON object is interpreted as a single string
                            output = JsonConvert.ToString(output);

                            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/v1/General");
                            request.Content            = new StringContent(output, System.Text.Encoding.UTF8, "application/json");
                            // sends POST request to server, containing JSON representation of events
                            try
                            {
                                HttpResponseMessage response = client.SendAsync(request).Result;
                            }
                            catch { }
                        }
                        catch { }

                        hold = 0;

                        // if HTTP request took longer than sampling rate, skip delay to minizime data loss
                        // else, delay for difference between desired sampling rate and duration of request
                        if (sampleRate < duration.ElapsedMilliseconds)
                        {
                            duration.Reset();
                        }
                        else
                        {
                            await Task.Delay(sampleRate - (int)duration.ElapsedMilliseconds);
                            duration.Reset();
                        }
                    }
                    else             // delay for the amount of time between samples if request wasn't sent
                    {
                        await Task.Delay(sampleRate);
                    }
                }
            });
        }