Skip to content

Commit 3ab0cb4

Browse files
authored
fix: Preserve custom query name even if statement can't be parsed. (#2708) (#2709)
1 parent 3b13591 commit 3ab0cb4

File tree

2 files changed

+40
-4
lines changed
  • src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Parsing
  • tests/Agent/UnitTests/NewRelic.Agent.Extensions.Tests/Parsing

2 files changed

+40
-4
lines changed

src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Parsing/SqlParser.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ private static ParseStatement CreateCompoundStatementParser(params ParseStatemen
239239
}
240240
}
241241

242-
return _nullParsedStatementStore.GetOrAdd(datastoreVendor, x => new ParsedSqlStatement(datastoreVendor, null, null));
242+
// return a null statement with the model name if found, otherwise cache and return a null statement with no model name
243+
return !string.IsNullOrEmpty(explicitModel) ? new ParsedSqlStatement(datastoreVendor, explicitModel, null) : _nullParsedStatementStore.GetOrAdd(datastoreVendor, x => new ParsedSqlStatement(datastoreVendor, null, null));
243244
};
244245
}
245246

@@ -295,7 +296,7 @@ public virtual ParsedSqlStatement ParseStatement(DatastoreVendor vendor, Command
295296
model = "ParseError";
296297
}
297298
}
298-
299+
299300
return CreateParsedDatabaseStatement(vendor, GetFullModel(model, explicitModel));
300301
}
301302

@@ -323,7 +324,7 @@ public ParsedSqlStatement ParseStatement(DatastoreVendor vendor, CommandType com
323324
string model = FromMatcher.Match(statement).Success
324325
? "(subquery)"
325326
: "VARIABLE";
326-
327+
327328
return new ParsedSqlStatement(vendor, GetFullModel(model, explicitModel), "select");
328329
}
329330
return null;
@@ -414,7 +415,7 @@ public static bool FixParameterizedSql(IDbCommand command)
414415
Log.Debug("Not executing explain plan since DbType is Object.");
415416
return false;
416417
}
417-
418+
418419
// Parameter names can be supplied with the prefix @ or without
419420
// if is supplied, remove the @ to the beginning of the param name
420421
// This is to deal with regex issues, see below

tests/Agent/UnitTests/NewRelic.Agent.Extensions.Tests/Parsing/SqlParserTests.cs

+35
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,41 @@ public void SqlParserTest_PullNameFromComment_ReplaceSlashes()
254254
Assert.That(parsedDatabaseStatement.ToString(), Is.EqualTo("dude - [dudeservice|getalldudes]/select"));
255255
});
256256
}
257+
258+
[Test]
259+
public void SqlParserTest_PullNameFromComment_IfStatementCannotBeParsed()
260+
{
261+
var parsedDatabaseStatement = SqlParser.GetParsedDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text,
262+
"""
263+
/* NewRelicQueryName: MyCustomQueryName */
264+
265+
-- This is a full-line comment
266+
SELECT
267+
(
268+
SELECT COUNT(*)
269+
FROM
270+
SomeTable
271+
WHERE
272+
SomeColumn = @SomeValue
273+
) +
274+
(
275+
SELECT COUNT(*)
276+
FROM
277+
SomeOtherTable
278+
WHERE
279+
SomeOtherColumn = @SomeOtherValue
280+
);
281+
""");
282+
283+
Assert.That(parsedDatabaseStatement, Is.Not.Null);
284+
285+
Assert.Multiple(() =>
286+
{
287+
Assert.That(parsedDatabaseStatement.Model, Is.EqualTo("MyCustomQueryName"));
288+
289+
Assert.That(parsedDatabaseStatement.ToString(), Is.EqualTo("MyCustomQueryName/other"));
290+
});
291+
}
257292

258293

259294
[Test]

0 commit comments

Comments
 (0)