Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
yml: [ '**.yml', '**.yaml', '.yamllint' ]
build: ['build/**', '.github/**/*.yml', '!.github/workflows/package-*', '**/*.targets', '**/*.props', 'global.json']
shared: ['src/Shared/**', 'test/Shared/**']
contrib-shared-tests: ['test/OpenTelemetry.Contrib.Shared.Tests/**']
contrib-shared-tests: ['test/OpenTelemetry.Contrib.Shared.Tests/**', 'test/OpenTelemetry.Contrib.Shared.Benchmarks/**']
code: ['**.cs', '**.csproj', '.editorconfig']
aottestapp: ['test/OpenTelemetry.AotCompatibility.TestApp/**']
exporter-geneva: ['*/OpenTelemetry.Exporter.Geneva*/**', '!**/*.md']
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/markdownlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
globs: |
**/*.md
!.github/**/*.md
!**/*-report-github.md
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,10 @@ $RECYCLE.BIN/

# Test results
TestResults/

# Ignore everything in BenchmarkResults (including subfolders)
test/**/BenchmarkResults/**
# Do NOT ignore the results directory
!test/**/BenchmarkResults/results/
# Do NOT ignore files ending with -report-github.md anywhere under BenchmarkResults
!test/**/BenchmarkResults/results/*-report-github.md
1 change: 1 addition & 0 deletions build/Projects/OpenTelemetry.Contrib.Shared.Tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<SolutionProjects Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Shared.Tests\OpenTelemetry.Contrib.Shared.Tests.csproj" />
<SolutionProjects Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Shared.Benchmarks\OpenTelemetry.Contrib.Shared.Benchmarks.csproj" />
<TestProjects Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Shared.Tests\OpenTelemetry.Contrib.Shared.Tests.csproj" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.OpAmp.Client"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.OpAmp.Client.Tests", "test\OpenTelemetry.OpAmp.Client.Tests\OpenTelemetry.OpAmp.Client.Tests.csproj", "{044B896A-FDC6-422F-B349-483BDE19F685}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.Contrib.Shared.Benchmarks", "test\OpenTelemetry.Contrib.Shared.Benchmarks\OpenTelemetry.Contrib.Shared.Benchmarks.csproj", "{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AWS", "AWS", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
ProjectSection(SolutionItems) = preProject
src\Shared\AWS\AWSSemanticConventions.Base.cs = src\Shared\AWS\AWSSemanticConventions.Base.cs
Expand Down Expand Up @@ -851,6 +853,10 @@ Global
{044B896A-FDC6-422F-B349-483BDE19F685}.Debug|Any CPU.Build.0 = Debug|Any CPU
{044B896A-FDC6-422F-B349-483BDE19F685}.Release|Any CPU.ActiveCfg = Release|Any CPU
{044B896A-FDC6-422F-B349-483BDE19F685}.Release|Any CPU.Build.0 = Release|Any CPU
{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC}.Release|Any CPU.Build.0 = Release|Any CPU
{EB5F0E63-61E3-43AE-A383-48EA7878DA29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB5F0E63-61E3-43AE-A383-48EA7878DA29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB5F0E63-61E3-43AE-A383-48EA7878DA29}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -987,6 +993,7 @@ Global
{00C9F0B8-3D51-483C-821A-5889B38A144C} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{B5AF4876-3970-4686-A4A9-4EA369843F1F} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{044B896A-FDC6-422F-B349-483BDE19F685} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{4B1A2D22-C3EE-341A-D4AC-8B32F0F093FC} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {1FCC8EEC-9E75-4FEA-AFCF-363DD33FF0B9}
{EB5F0E63-61E3-43AE-A383-48EA7878DA29} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{14D441DE-5C76-7700-152E-7E8E01E5CA19} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
Expand Down
23 changes: 16 additions & 7 deletions src/Shared/SqlProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Collections;
using System.Text;

namespace OpenTelemetry.Instrumentation;

internal static class SqlProcessor
{
private const int CacheCapacity = 1000;
private static readonly Hashtable Cache = [];
private static readonly Dictionary<string, SqlStatementInfo> Cache = [];

public static SqlStatementInfo GetSanitizedSql(string? sql)
{
Expand All @@ -18,7 +17,7 @@ public static SqlStatementInfo GetSanitizedSql(string? sql)
return default;
}

if (Cache[sql] is not SqlStatementInfo sqlStatementInfo)
if (!Cache.TryGetValue(sql, out var sqlStatementInfo))
{
sqlStatementInfo = SanitizeSql(sql);

Expand All @@ -29,7 +28,7 @@ public static SqlStatementInfo GetSanitizedSql(string? sql)

lock (Cache)
{
if ((Cache[sql] as string) == null)
if (!Cache.ContainsKey(sql))
{
if (Cache.Count < CacheCapacity)
{
Expand All @@ -44,7 +43,7 @@ public static SqlStatementInfo GetSanitizedSql(string? sql)

private static SqlStatementInfo SanitizeSql(string sql)
{
var state = new SqlProcessorState();
var state = new SqlProcessorState(sql.Length);

for (var i = 0; i < sql.Length; ++i)
{
Expand Down Expand Up @@ -271,8 +270,12 @@ private static void WriteToken(string sql, ref int index, SqlProcessorState stat
if (state.CaptureNextTokenAsTarget)
{
state.CaptureNextTokenAsTarget = false;
#if NET
state.DbQuerySummary.Append(' ').Append(sql.AsSpan(index, i - index));
#else
var collection = sql.Substring(index, i - index);
state.DbQuerySummary.Append(' ').Append(collection);
#endif
}

i -= 1;
Expand Down Expand Up @@ -384,9 +387,15 @@ private static bool LookAhead(string compare, string sql, ref int index, SqlProc

internal class SqlProcessorState
{
public StringBuilder SanitizedSql { get; set; } = new StringBuilder();
public SqlProcessorState(int maxCapacity)
{
this.SanitizedSql = new StringBuilder(maxCapacity);
this.DbQuerySummary = new StringBuilder(maxCapacity);
}

public StringBuilder SanitizedSql { get; set; }

public StringBuilder DbQuerySummary { get; set; } = new StringBuilder();
public StringBuilder DbQuerySummary { get; set; }

public bool CaptureNextTokenAsTarget { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```

BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.4946)
Unknown processor
.NET SDK 9.0.304
[Host] : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
DefaultJob : .NET 9.0.8 (9.0.825.36511), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI


```
| Method | Sql | Iterations | Mean | Error | StdDev | Allocated |
|------- |--------------------- |----------- |----------:|----------:|----------:|----------:|
| **Simple** | **SELEC(...)ls od [39]** | **1** | **16.42 ns** | **0.362 ns** | **0.818 ns** | **-** |
| **Simple** | **SELEC(...)ls od [39]** | **20** | **335.73 ns** | **6.616 ns** | **9.698 ns** | **-** |
| **Simple** | **SELE(...)_id) [101]** | **1** | **33.42 ns** | **0.700 ns** | **1.445 ns** | **-** |
| **Simple** | **SELE(...)_id) [101]** | **20** | **654.29 ns** | **12.859 ns** | **13.759 ns** | **-** |
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(SupportedNetTargets)</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="$(BenchmarkDotNetPkgVer)" />
<PackageReference Include="JetBrains.Profiler.Api" Version="1.4.10" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\SqlProcessor.cs" Link="Includes\SqlProcessor.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SqlStatementInfo.cs" Link="Includes\SqlStatementInfo.cs" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions test/OpenTelemetry.Contrib.Shared.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using JetBrains.Profiler.Api;
using OpenTelemetry.Instrumentation.Benchmarks;

if (Debugger.IsAttached || (args.Length > 0 && args[0] == "execute"))
{
var benchmarks = new SqlProcessorBenchmarks
{
Sql = "SELECT * FROM Orders o, OrderDetails od",
Iterations = 100,
};

MemoryProfiler.CollectAllocations(true);

MemoryProfiler.GetSnapshot();

benchmarks.Simple();

MemoryProfiler.GetSnapshot();
}
else
{
var config = ManualConfig.Create(DefaultConfig.Instance)
.WithArtifactsPath(@"..\..\..\BenchmarkResults");

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"OpenTelemetry.Contrib.Shared.Benchmarks": {
"commandName": "Project"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using BenchmarkDotNet.Attributes;

namespace OpenTelemetry.Instrumentation.Benchmarks;

[MemoryDiagnoser]
public class SqlProcessorBenchmarks
{
[Params("SELECT * FROM Orders o, OrderDetails od", "SELECT order_date\nFROM (SELECT *\nFROM orders o\nJOIN customers c\nON o.customer_id = c.customer_id)")]
public string Sql { get; set; } = string.Empty;

[Params(1, 20)]
public int Iterations { get; set; }

[Benchmark]
public void Simple()
{
for (int i = 0; i < this.Iterations; i++)
{
SqlProcessor.GetSanitizedSql(this.Sql);
}
}
}
Loading