Exemplo n.º 1
0
        public void InstrumentationSamplingRateLimitsForSync()
        {
            int numberOfTimesInstrumented = 0;
            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Counters           = CounterTypes.StandardCounters,
                Description        = "test",
                InstanceName       = "Test instance",
                CategoryName       = "DOESNOTEXISTDONTLOOKFORIT",
                PublishCounters    = true,
                PublishEvent       = true,
                RaisePublishErrors = false
            })
            {
                PublishInstrumentationCallback = (a, b, c, d, e) => numberOfTimesInstrumented++
            };

            double samplingRate = 0.01;

            Enumerable.Range(0, 1000).ToList().ForEach(x =>
            {
                Correlation.SetId(Guid.NewGuid().ToString());
                ins.Instrument(() => { }, samplingRate: samplingRate);
            });

            Assert.InRange(numberOfTimesInstrumented, 1, 100);
        }
Exemplo n.º 2
0
        public void Intercept(IInvocation invocation)
        {
            if (!PublishCounters && !PublishEvent)
            {
                invocation.Proceed();
            }
            else
            {
                try
                {
                    string instrumentationContext = "";

                    if (_instrumentationContextProvider != null)
                    {
                        instrumentationContext = _instrumentationContextProvider.GetContext(invocation.MethodInvocationTarget);
                    }

                    if (!_inited)
                    {
                        lock (_lock)
                        {
                            if (!_inited)
                            {
                                Init();
                            }
                        }
                    }

                    SimpleInstrumentor instrumentor = null;

                    if (_instrumentors == null || !_instrumentors.TryGetValue(instrumentationContext, out instrumentor))
                    {
                        instrumentor = (SimpleInstrumentor)InitInstrumentor(invocation.MethodInvocationTarget);
                    }

                    var returnType = invocation.Method.ReturnType;
                    if (returnType != typeof(void) &&
                        ((returnType == typeof(Task) ||
                          (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>)))))
                    {
                        instrumentor.InstrumentAsync(async() => invocation.Proceed(), instrumentationContext: instrumentationContext, samplingRate: SamplingRate);
                    }
                    else
                    {
                        instrumentor.Instrument(invocation.Proceed, instrumentationContext: instrumentationContext, samplingRate: SamplingRate);
                    }
                }
                catch (Exception exception)
                {
                    Trace.TraceError(exception.ToString());
                    if (RaisePublishErrors)
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void CanPublishAspect()
        {
            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Description  = "test",
                InstanceName = "Test instance",
                CategoryName = TestCategory
            });

            ins.Instrument(() => Thread.Sleep(100));
        }
Exemplo n.º 4
0
        public void Writes()
        {
            string fileName = "shibi";

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Description  = "test",
                InstanceName = "Test instance",
                CategoryName = "test"
            });

            ins.Tracers.Add("File", new SeparatedFileTracer(fileName));
            var ctx = new InstrumentationContext()
            {
                Text1   = "Text1",
                Text2   = "Text2",
                Numeric = 424242,
                Decimal = 0.420420420
            };

            ins.Instrument(() => Thread.Sleep(100), extraContext: ctx);


            Thread.Sleep(1000);
            ins.Dispose();
            var lines = File.ReadAllLines(fileName);

            Assert.Equal(1, lines.Length);

            var segments = lines[0].Split('\t');

            Assert.Equal(8, segments.Length);

            Assert.Equal("Test instance", segments[1]);
            Assert.Equal("test", segments[0]);
            Assert.Equal("Text1", segments[4]);
            Assert.Equal("Text2", segments[5]);
            Assert.Equal("424242", segments[6]);
            // INTENTIONAL // Assert.Equal("0.420420420", segments[7]);  !!floating point numbers :/

            //     1- Category
            //     2- Instance
            //     3- CorrelationId
            //     4- TimeTakenMilli
            //     5- Text1
            //     6- Text2
            //     7- Numberic
            //     8- Decimal
        }
Exemplo n.º 5
0
        public void CanPublishAspect()
        {
            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Counters = CounterTypes.StandardCounters,
                Description = "test",
                InstanceName = "Test instance",
                CategoryName = TestCategory
            });

            var listener = ConsoleLog.CreateListener();
            listener.EnableEvents(InstrumentationEventSource.Instance, EventLevel.LogAlways,
                Keywords.All);

            ins.Instrument(() => Thread.Sleep(100), "test...");

            listener.DisableEvents(InstrumentationEventSource.Instance);
            listener.Dispose();
        }
Exemplo n.º 6
0
        public void CanPublishAspect()
        {
            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Counters     = CounterTypes.StandardCounters,
                Description  = "test",
                InstanceName = "Test instance",
                CategoryName = TestCategory
            });

            var listener = ConsoleLog.CreateListener();

            listener.EnableEvents(InstrumentationEventSource.Instance, EventLevel.LogAlways,
                                  Keywords.All);

            ins.Instrument(() => Thread.Sleep(100), "test...");

            listener.DisableEvents(InstrumentationEventSource.Instance);
            listener.Dispose();
        }
Exemplo n.º 7
0
        public void InstrumentationShouldCallIncludedCounters()
        {
            if (!PerfItRuntime.HandlerFactories.ContainsKey("CustomCounterStub"))
            {
                PerfItRuntime.HandlerFactories.Add("CustomCounterStub", (s, s1) => new CustomCounterStub(s, s1));
            }

            CustomCounterStub.ClearCounters();

            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Counters        = CounterTypes.StandardCounters.Union(new[] { "CustomCounterStub" }).ToArray(),
                Description     = "test",
                InstanceName    = "Test instance",
                CategoryName    = TestCategory,
                PublishCounters = true,
            });

            ins.Instrument(() => { }, "test...");

            Assert.Equal(1, CustomCounterStub.RequestStartCount);
            Assert.Equal(1, CustomCounterStub.RequestEndCount);
        }
Exemplo n.º 8
0
        public void InstrumentationSamplingRateLimitsForSync()
        {
            int numberOfTimesInstrumented = 0;
            var ins = new SimpleInstrumentor(new InstrumentationInfo()
            {
                Description        = "test",
                InstanceName       = "Test instance",
                CategoryName       = "DOESNOTEXISTDONTLOOKFORIT",
                RaisePublishErrors = false
            });

            ins.Tracers.Add("a", new ActionTracer(() => numberOfTimesInstrumented++));

            double samplingRate = 0.01;

            Enumerable.Range(0, 1000).ToList().ForEach(x =>
            {
                Correlation.SetId(Guid.NewGuid().ToString());
                ins.Instrument(() => { }, samplingRate: samplingRate);
            });

            Assert.InRange(numberOfTimesInstrumented, 1, 100);
        }
Exemplo n.º 9
0
        public void InstrumentationSamplingRateLimitsForSync()
        {
            var numberOfTimesInstrumented = 0;

            var ins = new SimpleInstrumentor(new InstrumentationInfo
            {
                Counters           = CounterTypes.StandardCounters.ToArray(),
                Description        = "test",
                InstanceName       = "Test instance",
                CategoryName       = "DOESNOTEXISTDONTLOOKFORIT",
                PublishCounters    = true,
                PublishEvent       = true,
                RaisePublishErrors = false
            })
            {
                PublishInstrumentationCallback = (a, b, c, d) => numberOfTimesInstrumented++
            };

            const double samplingRate = 0.01d;

            Enumerable.Range(0, 1000).ToList().ForEach(x => ins.Instrument(() => { }, samplingRate: samplingRate));

            Assert.InRange(numberOfTimesInstrumented, 1, 100);
        }