forked from open-telemetry/opentelemetry-dotnet-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlParameterProcessor.cs
More file actions
36 lines (29 loc) · 1.05 KB
/
SqlParameterProcessor.cs
File metadata and controls
36 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Data;
using System.Diagnostics;
using System.Globalization;
namespace OpenTelemetry.Instrumentation;
internal static class SqlParameterProcessor
{
public static void AddQueryParameters(Activity activity, object? command)
{
if (command is not IDbCommand { Parameters.Count: > 0 } dbCommand)
{
return;
}
int index = 0;
foreach (var parameter in dbCommand.Parameters)
{
if (parameter is IDbDataParameter dbDataParameter)
{
// If a query parameter has no name and instead is referenced only by index, then {key} SHOULD be the 0-based index.
var key = string.IsNullOrEmpty(dbDataParameter.ParameterName)
? index.ToString(CultureInfo.InvariantCulture)
: dbDataParameter.ParameterName;
activity.SetTag($"db.query.parameter.{key}", dbDataParameter.Value);
}
index++;
}
}
}