protected GetTracerParameters DefaultGetTracerImplementation() { var getTracerParameters = new GetTracerParameters(); // setup the code to execute when NewRelic.Agent.Core.GetTracer is called SetGetTracerDelegate((String tracerFactoryName, UInt32 tracerArguments, String metricName, String assemblyName, Type type, String typeName, String methodName, String argumentSignature, Object invocationTarget, Object[] args, UInt64 functionId) => { // we can't assert in here so save off the variables and assert later getTracerParameters.tracerFactoryName = tracerFactoryName; getTracerParameters.tracerArguments = tracerArguments; getTracerParameters.metricName = metricName; getTracerParameters.assemblyName = assemblyName; getTracerParameters.type = type; getTracerParameters.typeName = typeName; getTracerParameters.methodName = methodName; getTracerParameters.argumentSignature = argumentSignature; getTracerParameters.invocationTarget = invocationTarget; getTracerParameters.args = args; // set the flag indicating the tracer was called, we'll be asserting on that later getTracerParameters.getTracerCalled = true; // create a new GUID for this tracer and push it onto our stack of GUIDs getTracerParameters.tracer = Guid.NewGuid(); // return the GUID as the tracer object, we'll validate that FinishTracer gets it return(getTracerParameters.tracer); }); return(getTracerParameters); }
private void ValidateFinishTracer(GetTracerParameters getTracerParameters, FinishTracerParameters finishTracerParameters, object expectedReturnValue, Exception expectedException) { // validate that finish tracer was called Assert.IsTrue(finishTracerParameters.finishTracerCalled, "NewRelic.Agent.Core.FinishTracer was not called."); // validate that FinishTracer received all the right parameters Assert.AreEqual(getTracerParameters.tracer, finishTracerParameters.tracerObject, "The tracer received by finish tracer doesn't match the one we returned from GetTracer."); Assert.AreEqual(expectedReturnValue, finishTracerParameters.returnValue, "FinishTracer did not receive the correct returnValue."); Assert.AreEqual(expectedException, finishTracerParameters.exceptionObject, "FinishTracer did not receive the expected exception."); }
public void profiled_method_called_from_get_tracer() { var getTracerParameters = new GetTracerParameters(); // setup the code to execute when NewRelic.Agent.Core.GetTracer is called SetGetTracerDelegate((String tracerFactoryName, UInt32 tracerArguments, String metricName, String assemblyName, Type type, String typeName, String methodName, String argumentSignature, Object invocationTarget, Object[] args, UInt64 functionId) => { if (methodName == "StaticMethod") { // check to see if we are approaching a stack overflow var stackTrace = new System.Diagnostics.StackTrace(); if (stackTrace.FrameCount >= 100) { throw new Exception("Stack Overflow Immenent."); } // if StaticMethod was called, call another profiled method (whose GetTracer won't do anything). StaticMethod2(); // we can't assert in here so save off the variables and assert later getTracerParameters.tracerFactoryName = tracerFactoryName; getTracerParameters.tracerArguments = tracerArguments; getTracerParameters.metricName = metricName; getTracerParameters.assemblyName = assemblyName; getTracerParameters.type = type; getTracerParameters.typeName = typeName; getTracerParameters.methodName = methodName; getTracerParameters.argumentSignature = argumentSignature; getTracerParameters.invocationTarget = invocationTarget; getTracerParameters.args = args; // set the flag indicating the tracer was called, we'll be asserting on that later getTracerParameters.getTracerCalled = true; // create a new GUID for this tracer and push it onto our stack of GUIDs getTracerParameters.tracer = Guid.NewGuid(); // return the GUID as the tracer object, we'll validate that FinishTracer gets it return(getTracerParameters.tracer); } else { return(null); } }); var finishTracerParameters = DefaultFinishTracerImplementation(); Assert.DoesNotThrow(() => { StaticMethod(); }, "Exception should not have been thrown."); ValidateTracers(getTracerParameters, finishTracerParameters, "StaticMethod", "", null, new object[] { }, null, null); }
private void ValidateGetTracer(GetTracerParameters getTracerParameters, String expectedMethodName, String expectedArgumentSignature, object expectedInvocationTarget, object[] parameters, String expectedTypeName) { // validate that GetTracer was called at all Assert.IsTrue(getTracerParameters.getTracerCalled, "NewRelic.Agent.Core.GetTracer was not called. Are you instrumenting this test suite?"); // validate that GetTracer received all the right parameters Assert.AreEqual("ProfiledMethods", getTracerParameters.assemblyName, "GetTracer did not receive the expected assembly name."); Assert.AreEqual(expectedTypeName, getTracerParameters.typeName, "GetTracer did not receive the expected type name."); Assert.AreEqual(expectedMethodName, getTracerParameters.methodName, "GetTracer did not receive the expected method name."); Assert.AreEqual(expectedArgumentSignature, getTracerParameters.argumentSignature, "GetTracer did not receive the expected argument signature."); Assert.AreEqual(expectedInvocationTarget, getTracerParameters.invocationTarget, "GetTracer did not receive the expected invocation target."); Assert.AreEqual(parameters.Length, getTracerParameters.args.Length, "GetTracer did not receive the expected parameter array length."); int i = 0; foreach (var parameter in parameters) { Assert.AreEqual(parameter, getTracerParameters.args[i], "GetTracer did not receive the expected value of parameter #" + i + "."); ++i; } }
protected void ValidateTracers(GetTracerParameters getTracerParameters, FinishTracerParameters finishTracerParameters, String expectedMethodName, String expectedArgumentSignature, object expectedInvocationTarget, object[] expectedParameters, object expectedReturnValue, Exception expectedException, String expectedTypeName = "NewRelic.Agent.Tests.ProfiledMethods.ProfiledMethods") { ValidateGetTracer(getTracerParameters, expectedMethodName, expectedArgumentSignature, expectedInvocationTarget, expectedParameters, expectedTypeName); ValidateFinishTracer(getTracerParameters, finishTracerParameters, expectedReturnValue, expectedException); }
public void nested_overloads() { // setup the callbacks for GetTracer and FinishTracer var getOverloadObjectTracerParameters = new GetTracerParameters(); var getOverloadStringTracerParameters = new GetTracerParameters(); // setup the code to execute when NewRelic.Agent.Core.GetTracer is called SetGetTracerDelegate((String tracerFactoryName, UInt32 tracerArguments, String metricName, String assemblyName, Type type, String typeName, String methodName, String argumentSignature, Object invocationTarget, Object[] args, UInt64 functionId) => { if (argumentSignature == "System.Object") { // we can't assert in here so save off the variables and assert later getOverloadObjectTracerParameters.tracerFactoryName = tracerFactoryName; getOverloadObjectTracerParameters.tracerArguments = tracerArguments; getOverloadObjectTracerParameters.metricName = metricName; getOverloadObjectTracerParameters.assemblyName = assemblyName; getOverloadObjectTracerParameters.type = type; getOverloadObjectTracerParameters.typeName = typeName; getOverloadObjectTracerParameters.methodName = methodName; getOverloadObjectTracerParameters.argumentSignature = argumentSignature; getOverloadObjectTracerParameters.invocationTarget = invocationTarget; getOverloadObjectTracerParameters.args = args; // set the flag indicating the tracer was called, we'll be asserting on that later getOverloadObjectTracerParameters.getTracerCalled = true; // create a new GUID for this tracer and push it onto our stack of GUIDs getOverloadObjectTracerParameters.tracer = Guid.NewGuid(); // return the GUID as the tracer object, we'll validate that FinishTracer gets it return(getOverloadObjectTracerParameters.tracer); } else if (argumentSignature == "System.String") { // we can't assert in here so save off the variables and assert later getOverloadStringTracerParameters.tracerFactoryName = tracerFactoryName; getOverloadStringTracerParameters.tracerArguments = tracerArguments; getOverloadStringTracerParameters.metricName = metricName; getOverloadStringTracerParameters.assemblyName = assemblyName; getOverloadStringTracerParameters.type = type; getOverloadStringTracerParameters.typeName = typeName; getOverloadStringTracerParameters.methodName = methodName; getOverloadStringTracerParameters.argumentSignature = argumentSignature; getOverloadStringTracerParameters.invocationTarget = invocationTarget; getOverloadStringTracerParameters.args = args; // set the flag indicating the tracer was called, we'll be asserting on that later getOverloadStringTracerParameters.getTracerCalled = true; // create a new GUID for this tracer and push it onto our stack of GUIDs getOverloadStringTracerParameters.tracer = Guid.NewGuid(); // return the GUID as the tracer object, we'll validate that FinishTracer gets it return(getOverloadStringTracerParameters.tracer); } else { return(null); } }); var finishOverloadObjectTracerParameters = new FinishTracerParameters(); var finishOverloadStringTracerParameters = new FinishTracerParameters(); // setup the code to execute when NewRelic.Agent.Core.FinishTracer is called SetFinishTracerDelegate((Object tracerObject, Object returnValue, Object exceptionObject) => { if ((Guid)(tracerObject) == getOverloadObjectTracerParameters.tracer) { // we can't assert in here so save off the variables and assert later finishOverloadObjectTracerParameters.tracerObject = tracerObject; finishOverloadObjectTracerParameters.returnValue = returnValue; finishOverloadObjectTracerParameters.exceptionObject = exceptionObject; // set the flag indicating the tracer was called, we'll be asserting on that later finishOverloadObjectTracerParameters.finishTracerCalled = true; } else if ((Guid)(tracerObject) == getOverloadStringTracerParameters.tracer) { // we can't assert in here so save off the variables and assert later finishOverloadStringTracerParameters.tracerObject = tracerObject; finishOverloadStringTracerParameters.returnValue = returnValue; finishOverloadStringTracerParameters.exceptionObject = exceptionObject; // set the flag indicating the tracer was called, we'll be asserting on that later finishOverloadStringTracerParameters.finishTracerCalled = true; } }); // call the method that will be instrumented Object parameter = new Object(); Assert.DoesNotThrow(() => { Overloaded(parameter); }, "Exception should not have been thrown."); var expectedOverloadObjectParameters = new object[] { parameter }; var expectedOverloadStringParameters = new object[] { parameter.ToString() }; var expectedOverloadObjectArgumentSignature = "System.Object"; var expectedOverloadStringArgumentSignature = "System.String"; ValidateTracers(getOverloadObjectTracerParameters, finishOverloadObjectTracerParameters, "Overloaded", expectedOverloadObjectArgumentSignature, this, expectedOverloadObjectParameters, null, null); ValidateTracers(getOverloadStringTracerParameters, finishOverloadStringTracerParameters, "Overloaded", expectedOverloadStringArgumentSignature, this, expectedOverloadStringParameters, null, null); }