public void ShouldDisableCacheIfTooManyConnectionStrings() { const string connectionStringTemplate = "Server=myServerName{0};Database=myDataBase;User Id=myUsername;Password=myPassword;"; // Fill-up the cache and test the logic with cache enabled for (int i = 0; i <= DbCommandCache.MaxConnectionStrings; i++) { var connectionString = string.Format(connectionStringTemplate, i); var commandTags = DbCommandCache.GetTagsFromDbCommand(CreateDbCommand(connectionString)); Assert.NotNull(DbCommandCache.Cache); Assert.Equal("myServerName" + i, commandTags.OutHost); } // Test the logic with cache disabled for (int i = 0; i <= 10; i++) { var connectionString = string.Format(connectionStringTemplate, "NoCache" + i); var commandTags = DbCommandCache.GetTagsFromDbCommand(CreateDbCommand(connectionString)); Assert.Null(DbCommandCache.Cache); Assert.Equal("myServerName" + "NoCache" + i, commandTags.OutHost); } }
/// <summary> /// Adds standard tags to a span with values taken from the specified <see cref="DbCommand"/>. /// </summary> /// <param name="span">The span to add the tags to.</param> /// <param name="command">The db command to get tags values from.</param> public static void AddTagsFromDbCommand(this Span span, IDbCommand command) { span.ResourceName = command.CommandText; span.Type = SpanTypes.Sql; var tags = DbCommandCache.GetTagsFromDbCommand(command); foreach (var pair in tags) { span.SetTag(pair.Key, pair.Value); } }
public void ExtractProperTagsFromConnectionString( string connectionString, string expectedDbName, string expectedUserId, string expectedHost) { var commandTags = DbCommandCache.GetTagsFromDbCommand(CreateDbCommand(connectionString)); Assert.Equal(expectedDbName, commandTags.DbName); Assert.Equal(expectedUserId, commandTags.DbUser); Assert.Equal(expectedHost, commandTags.OutHost); }
/// <summary> /// Adds standard tags to a span with values taken from the specified <see cref="DbCommand"/>. /// </summary> /// <param name="span">The span to add the tags to.</param> /// <param name="command">The db command to get tags values from.</param> public static void AddTagsFromDbCommand(this ISpan span, IDbCommand command) { var commandText = command.CommandText; span.ResourceName = commandText; span.Type = SpanTypes.Sql; span.SetTag(Tags.DbStatement, commandText); var tag = DbCommandCache.GetTagsFromDbCommand(command); span.SetTag(Tags.DbName, tag.DbName); span.SetTag(Tags.DbUser, tag.DbUser); span.SetTag(Tags.OutHost, tag.OutHost); }
private static DbCommandCache.TagsCacheItem GetTagsFromConnectionString(IDbCommand command) { var connectionString = command.Connection?.ConnectionString; // Check if the connection string is the one in the cache var tagsByConnectionString = _tagsByConnectionStringCache; if (tagsByConnectionString.Key == connectionString) { // Fastpath return(tagsByConnectionString.Value); } // Cache the new tags by connection string // Slowpath var tags = DbCommandCache.GetTagsFromDbCommand(command); _tagsByConnectionStringCache = new KeyValuePair <string, DbCommandCache.TagsCacheItem>(connectionString, tags); return(tags); }