Skip to content

Commit 68a4780

Browse files
Avoid ArgumentException when no DbContext
Avoid `ArgumentException` if the `DbContext` is not available in the event payload. See dotnet/efcore#36286.
1 parent 48f49d0 commit 68a4780

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,29 @@ public override void OnEventWritten(string name, object? payload)
8080

8181
if (activity.IsAllDataRequested)
8282
{
83-
var dbContext = this.dbContextFetcher.Fetch(payload);
84-
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
85-
var providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
83+
string? providerOrCommandName = null;
8684

87-
switch (providerName)
85+
if (this.dbContextFetcher.Fetch(payload) is { } dbContext)
86+
{
87+
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
88+
providerOrCommandName = this.providerNameFetcher.Fetch(dbContextDatabase);
89+
}
90+
else
91+
{
92+
// Try to infer the database name from the command
93+
// type if the DbContext is not available.
94+
providerOrCommandName = command.GetType().FullName;
95+
}
96+
97+
switch (providerOrCommandName)
8898
{
8999
case "Microsoft.EntityFrameworkCore.SqlServer":
90100
activity.AddTag(AttributeDbSystem, "mssql");
91101
break;
92102
case "Microsoft.EntityFrameworkCore.Cosmos":
93103
activity.AddTag(AttributeDbSystem, "cosmosdb");
94104
break;
105+
case "Microsoft.Data.Sqlite.SqliteCommand":
95106
case "Microsoft.EntityFrameworkCore.Sqlite":
96107
case "Devart.Data.SQLite.Entity.EFCore":
97108
activity.AddTag(AttributeDbSystem, "sqlite");
@@ -136,7 +147,7 @@ public override void OnEventWritten(string name, object? payload)
136147
break;
137148
default:
138149
activity.AddTag(AttributeDbSystem, "other_sql");
139-
activity.AddTag("ef.provider", providerName);
150+
activity.AddTag("ef.provider", providerOrCommandName);
140151
break;
141152
}
142153

@@ -179,9 +190,13 @@ public override void OnEventWritten(string name, object? payload)
179190

180191
try
181192
{
182-
var dbContext = this.dbContextFetcher.Fetch(payload);
183-
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
184-
var providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
193+
string? providerName = null;
194+
195+
if (this.dbContextFetcher.Fetch(payload) is { } dbContext)
196+
{
197+
var dbContextDatabase = this.dbContextDatabaseFetcher.Fetch(dbContext);
198+
providerName = this.providerNameFetcher.Fetch(dbContextDatabase);
199+
}
185200

186201
if (command is IDbCommand typedCommand && this.options.Filter?.Invoke(providerName, typedCommand) == false)
187202
{

test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkIntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool ActivityFilter(string? providerName, IDbCommand command)
105105
{
106106
filtered = true;
107107

108-
Assert.Equal(provider, providerName);
108+
Assert.True(providerName == provider || providerName == null, $"The provider name {providerName} is not null or the expected value.");
109109
Assert.IsType(expectedCommandType, command, false);
110110

111111
return true;
@@ -191,7 +191,7 @@ bool ActivityFilter(string? providerName, IDbCommand command)
191191
{
192192
filtered = true;
193193

194-
Assert.Equal(provider, providerName);
194+
Assert.True(providerName == provider || providerName == null, $"The provider name {providerName} is not null or the expected value.");
195195
Assert.IsType(expectedCommandType, command, false);
196196

197197
return true;

0 commit comments

Comments
 (0)