Example #1
0
        public static object ExecuteReaderWithBehavior(
            object command,
            int behavior,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string methodName      = AdoNetConstants.MethodNames.ExecuteReader;
            var          commandBehavior = (CommandBehavior)behavior;
            Func <DbCommand, CommandBehavior, DbDataReader> instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(DbCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, CommandBehavior, DbDataReader> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(targetType)
                    .WithParameters(commandBehavior)
                    .WithNamespaceAndNameFilters(AdoNetConstants.TypeNames.DbDataReader, AdoNetConstants.TypeNames.CommandBehavior)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: DbCommandTypeName,
                    methodName: methodName,
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            var dbCommand = command as DbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand))
            {
                try
                {
                    return(instrumentedMethod(dbCommand, commandBehavior));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
Example #2
0
        private static async Task <DbDataReader> ExecuteReaderAsyncInternal(
            DbCommand command,
            CommandBehavior commandBehavior,
            CancellationToken cancellationToken,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string methodName = AdoNetConstants.MethodNames.ExecuteReaderAsync;
            Func <DbCommand, CommandBehavior, CancellationToken, Task <DbDataReader> > instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(DbCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, CommandBehavior, CancellationToken, Task <DbDataReader> > >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(targetType)
                    .WithParameters(commandBehavior, cancellationToken)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask, AdoNetConstants.TypeNames.CommandBehavior, ClrNames.CancellationToken)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: DbCommandTypeName,
                    methodName: methodName,
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command))
            {
                try
                {
                    return(await instrumentedMethod(command, commandBehavior, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        private static object ExecuteScalar(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr,
            string sqlClientNamespace)
        {
            const string             methodName = AdoNetConstants.MethodNames.ExecuteScalar;
            Func <DbCommand, object> instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(sqlClientNamespace, SqlCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(targetType)
                    .WithNamespaceAndNameFilters(ClrNames.Object)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: $"{sqlClientNamespace}.{SqlCommandTypeName}",
                    methodName: methodName,
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            var dbCommand = command as DbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand))
            {
                try
                {
                    return(instrumentedMethod(dbCommand));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        public static int ExecuteNonQuery(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string           methodName = AdoNetConstants.MethodNames.ExecuteNonQuery;
            Func <IDbCommand, int> instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedInterface(IDbCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <IDbCommand, int> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(targetType)
                    .WithNamespaceAndNameFilters(ClrNames.Int32)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: IDbCommandTypeName,
                    methodName: methodName,
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            var dbCommand = command as IDbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand))
            {
                try
                {
                    return(instrumentedMethod(dbCommand));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        public void CreateDbCommandScope_IgnoresReplacementServiceNameWhenNotProvided(IDbCommand command)
        {
            // Set up tracer
            var collection = new NameValueCollection
            {
                { ConfigurationKeys.ServiceNameMappings, $"something:my-custom-type" }
            };
            IConfigurationSource source = new NameValueConfigurationSource(collection);
            var tracerSettings          = new TracerSettings(source);
            var tracer = new Tracer(tracerSettings);

            // Create scope
            using (var outerScope = ScopeFactory.CreateDbCommandScope(tracer, command))
            {
                Assert.NotEqual("my-custom-type", outerScope.Span.ServiceName);
            }
        }
Example #6
0
        public static object ExecuteReader(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string          methodName = AdoNetConstants.MethodNames.ExecuteReader;
            Func <object, object> instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(SqlCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <object, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(targetType)
                    .WithNamespaceAndNameFilters(SqlDataReaderTypeName)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: SqlCommandTypeName,
                    methodName: methodName,
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command as DbCommand, IntegrationName))
            {
                try
                {
                    return(instrumentedMethod(command));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        public static object ExecuteReader(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <IDbCommand, IDataReader> instrumentedMethod;

            try
            {
                instrumentedMethod =
                    MethodBuilder <Func <IDbCommand, IDataReader> >
                    .Start(moduleVersionPtr, mdToken, opCode, AdoNetConstants.MethodNames.ExecuteReader)
                    .WithConcreteType(typeof(IDbCommand))
                    .WithNamespaceAndNameFilters(DataReaderTypeName)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: DbCommandTypeName,
                    methodName: nameof(ExecuteReader),
                    instanceType: command.GetType().AssemblyQualifiedName);
                throw;
            }

            var dbCommand = command as IDbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand, IntegrationName))
            {
                try
                {
                    return(instrumentedMethod(dbCommand));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
 /// <summary>
 /// Calls the underlying ExecuteReaderAsync and traces the request.
 /// </summary>
 /// <typeparam name="T">The type of the generic Task instantiation</typeparam>
 /// <param name="command">The object referenced by this in the instrumented method.</param>
 /// <param name="cancellationToken">The cancellation token</param>
 /// <param name="instrumentedMethod">A delegate for the method we are instrumenting</param>
 /// <returns>A task with the result</returns>
 private static async Task <T> ExecuteReaderAsyncInternal <T>(
     DbCommand command,
     CancellationToken cancellationToken,
     Func <DbCommand, CancellationToken, object> instrumentedMethod)
 {
     using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command))
     {
         try
         {
             var task = (Task <T>)instrumentedMethod(command, cancellationToken);
             return(await task.ConfigureAwait(false));
         }
         catch (Exception ex)
         {
             scope?.Span.SetException(ex);
             throw;
         }
     }
 }
        public void CreateDbCommandScope_UsesReplacementServiceNameWhenProvided(IDbCommand command)
        {
            // Set up tracer
            var t          = command.GetType();
            var dbType     = ScopeFactory.GetDbType(t.Namespace, t.Name);
            var collection = new NameValueCollection
            {
                { ConfigurationKeys.ServiceNameMappings, $"{dbType}:my-custom-type" }
            };
            IConfigurationSource source = new NameValueConfigurationSource(collection);
            var tracerSettings          = new TracerSettings(source);
            var tracer = new Tracer(tracerSettings);

            // Create scope
            using (var outerScope = ScopeFactory.CreateDbCommandScope(tracer, command))
            {
                Assert.Equal("my-custom-type", outerScope.Span.ServiceName);
            }
        }
        public static object ExecuteReaderWithBehavior(
            object command,
            int behavior,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <object, CommandBehavior, object> instrumentedMethod;
            var commandBehavior = (CommandBehavior)behavior;

            try
            {
                var targetType = command.GetInstrumentedType(NpgsqlCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <object, CommandBehavior, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, AdoNetConstants.MethodNames.ExecuteReader)
                    .WithConcreteType(targetType)
                    .WithParameters(commandBehavior)
                    .WithNamespaceAndNameFilters(NpgsqlDataReaderTypeName, AdoNetConstants.TypeNames.CommandBehavior)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorException($"Error resolving {NpgsqlCommandTypeName}.{AdoNetConstants.MethodNames.ExecuteReader}(...)", ex);
                throw;
            }

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command as DbCommand, IntegrationName))
            {
                try
                {
                    return(instrumentedMethod(command, commandBehavior));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        private static async Task <DbDataReader> ExecuteReaderAsyncInternalTwoParams(
            DbCommand command,
            CommandBehavior commandBehavior,
            CancellationToken cancellationToken,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <DbCommand, CommandBehavior, CancellationToken, Task <DbDataReader> > instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(NpgsqlCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, CommandBehavior, CancellationToken, Task <DbDataReader> > >
                    .Start(moduleVersionPtr, mdToken, opCode, nameof(ExecuteReaderAsync))
                    .WithConcreteType(targetType)
                    .WithParameters(commandBehavior, cancellationToken)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask, AdoNetConstants.TypeNames.CommandBehavior, ClrNames.CancellationToken)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorException($"Error resolving {NpgsqlCommandTypeName}.{AdoNetConstants.MethodNames.ExecuteReaderAsync}(...)", ex);
                throw;
            }

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command, IntegrationName))
            {
                try
                {
                    return(await instrumentedMethod(command, commandBehavior, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        public static object ExecuteScalar(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <DbCommand, object> instrumentedMethod;

            try
            {
                var targetType = command.GetInstrumentedType(NpgsqlCommandTypeName);

                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, AdoNetConstants.MethodNames.ExecuteScalar)
                    .WithConcreteType(targetType)
                    .WithNamespaceAndNameFilters(ClrNames.Object)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorException($"Error resolving {NpgsqlCommandTypeName}.{AdoNetConstants.MethodNames.ExecuteScalar}(...)", ex);
                throw;
            }

            var dbCommand = command as DbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand, IntegrationName))
            {
                try
                {
                    return(instrumentedMethod(dbCommand));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        public void CreateDbCommandScope_ReturnsNullForExcludedAdoNetTypes(IDbCommand command)
        {
            // Set up tracer
            var collection = new NameValueCollection
            {
                { ConfigurationKeys.AdoNetExcludedTypes, command.GetType().FullName }
            };
            IConfigurationSource source = new NameValueConfigurationSource(collection);
            var tracerSettings          = new TracerSettings(source);
            var tracer = new Tracer(tracerSettings);

            // Create scope
            using (var outerScope = ScopeFactory.CreateDbCommandScope(tracer, new CustomDbCommand()))
            {
                using (var innerScope = ScopeFactory.CreateDbCommandScope(tracer, command))
                {
                    Assert.Null(innerScope);
                }

                Assert.NotNull(outerScope);
            }
        }
        public static int ExecuteNonQuery(
            object command,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <IDbCommand, int> instrumentedMethod;

            try
            {
                instrumentedMethod =
                    MethodBuilder <Func <IDbCommand, int> >
                    .Start(moduleVersionPtr, mdToken, opCode, AdoNetConstants.MethodNames.ExecuteNonQuery)
                    .WithConcreteType(typeof(IDbCommand))
                    .WithNamespaceAndNameFilters(ClrNames.Int32)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error resolving {DbCommandTypeName}.{AdoNetConstants.MethodNames.ExecuteNonQuery}(...)");
                throw;
            }

            var dbCommand = command as IDbCommand;

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, dbCommand, IntegrationName))
            {
                try
                {
                    return(instrumentedMethod(dbCommand));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
Example #15
0
        private static async Task <object> ExecuteScalarAsyncInternal(
            DbCommand command,
            CancellationToken cancellationToken,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            Func <DbCommand, CancellationToken, Task <object> > instrumentedMethod;

            try
            {
                instrumentedMethod =
                    MethodBuilder <Func <DbCommand, CancellationToken, Task <object> > >
                    .Start(moduleVersionPtr, mdToken, opCode, AdoNetConstants.MethodNames.ExecuteScalarAsync)
                    .WithConcreteType(typeof(DbCommand))
                    .WithParameters(cancellationToken)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask, ClrNames.CancellationToken)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error resolving {DbCommandTypeName}.{AdoNetConstants.MethodNames.ExecuteScalarAsync}(...)");
                throw;
            }

            using (var scope = ScopeFactory.CreateDbCommandScope(Tracer.Instance, command, IntegrationName))
            {
                try
                {
                    return(await instrumentedMethod(command, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
 /// <summary>
 /// OnMethodBegin callback
 /// </summary>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
 /// <param name="cancellationToken">CancellationToken value</param>
 /// <returns>Calltarget state value</returns>
 public static CallTargetState OnMethodBegin <TTarget>(TTarget instance, CancellationToken cancellationToken)
 {
     return(new CallTargetState(ScopeFactory.CreateDbCommandScope(Tracer.Instance, instance as DbCommand)));
 }
 private static Scope MicrosoftDataSqlClientSqlCommandCreateScope(Tracer tracer) => ScopeFactory.CreateDbCommandScope(tracer, new Microsoft.Data.SqlClient.SqlCommand());
 private static Scope CustomCreateScope(Tracer tracer) => ScopeFactory.CreateDbCommandScope(tracer, new CustomDbCommand());
 private static Scope PostgresCreateScope(Tracer tracer) => ScopeFactory.CreateDbCommandScope(tracer, new NpgsqlCommand());
 private static Scope SystemDataSqlClientSqlCommandCreateScope(Tracer tracer) => ScopeFactory.CreateDbCommandScope(tracer, new System.Data.SqlClient.SqlCommand());
Example #21
0
 /// <summary>
 /// OnMethodBegin callback
 /// </summary>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <typeparam name="TBehavior">Command Behavior type</typeparam>
 /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
 /// <param name="commandBehavior">Command behavior</param>
 /// <returns>Calltarget state value</returns>
 public static CallTargetState OnMethodBegin <TTarget, TBehavior>(TTarget instance, TBehavior commandBehavior)
 {
     return(new CallTargetState(ScopeFactory.CreateDbCommandScope(Tracer.Instance, instance as DbCommand)));
 }