Skip to content

Refactor log directory resolution into smaller methods, add tests#7255

Open
lucaspimentel wants to merge 3 commits intomasterfrom
lpimentel/refactor-log-directory
Open

Refactor log directory resolution into smaller methods, add tests#7255
lucaspimentel wants to merge 3 commits intomasterfrom
lpimentel/refactor-log-directory

Conversation

@lucaspimentel
Copy link
Member

@lucaspimentel lucaspimentel commented Jul 21, 2025

Summary of changes

Refactor DatadogLoggingFactory log directory resolution logic into smaller, testable methods and add unit tests.

Reason for change

The log directory resolution logic was a single large method mixing concerns:

  • config reading
  • default directory lookup
  • directory creation 😦
  • fallback logic

Breaking it apart makes each piece independently testable and easier to understand.

Implementation details

  • Extract GetDefaultLogDirectory() — determines the default log directory based on platform and Azure environment
  • Extract GetProgramDataDirectory() — resolves the ProgramData path on Windows (with Nano Server fallbacks)
  • Extract TryCreateLogDirectory() — attempts to create a log directory, returning success/failure
  • Make extracted methods internal with [TestingAndPrivateOnly] for testability

Simplify GetLogDirectory() to orchestrate the above methods with clear fallback chain:

  1. DD_TRACE_LOG_DIRECTORY env var
  2. DD_TRACE_LOG_PATH (deprecated, directory extracted from file path)
  3. GetDefaultLogDirectory() (platform/Azure-aware default)
  4. Path.GetTempPath() (last resort)

Test coverage

  • GetDefaultLogDirectoryTests — verifies default paths on Windows/Linux, and Azure App Services detection
  • GetProgramDataDirectoryTests — verifies ProgramData resolution on Windows
  • TryCreateLogDirectoryTests — verifies directory creation success, existing directory, invalid path, and nested path scenarios

Other details

"I refactored a method into smaller methods. Now I have more methods to refactor into even smaller methods." — Claude 🤖

@lucaspimentel lucaspimentel added the type:cleanup Minor code clean up label Jul 21, 2025
@github-actions github-actions bot added the area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) label Jul 21, 2025
@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch from 8d547a5 to dccf49d Compare August 19, 2025 20:30
@dd-trace-dotnet-ci-bot
Copy link

dd-trace-dotnet-ci-bot bot commented Aug 19, 2025

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (7255) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration69.28 ± (69.28 - 69.51) ms69.22 ± (69.33 - 69.64) ms-0.1%
.NET Framework 4.8 - Bailout
duration73.43 ± (73.36 - 73.59) ms73.16 ± (73.11 - 73.37) ms-0.4%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1036.60 ± (1040.06 - 1047.35) ms1036.18 ± (1038.30 - 1044.04) ms-0.0%
.NET Core 3.1 - Baseline
process.internal_duration_ms21.92 ± (21.88 - 21.96) ms21.97 ± (21.92 - 22.01) ms+0.2%✅⬆️
process.time_to_main_ms79.80 ± (79.64 - 79.95) ms80.91 ± (80.47 - 81.34) ms+1.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.91 - 10.92) MB10.92 ± (10.91 - 10.92) MB+0.0%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms21.73 ± (21.70 - 21.75) ms21.85 ± (21.83 - 21.88) ms+0.6%✅⬆️
process.time_to_main_ms80.93 ± (80.76 - 81.10) ms81.17 ± (81.02 - 81.32) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.95 ± (10.94 - 10.95) MB10.93 ± (10.93 - 10.94) MB-0.1%
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms247.71 ± (244.64 - 250.77) ms250.87 ± (247.68 - 254.06) ms+1.3%✅⬆️
process.time_to_main_ms467.67 ± (467.13 - 468.20) ms470.56 ± (470.06 - 471.07) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed47.70 ± (47.68 - 47.72) MB47.70 ± (47.68 - 47.73) MB+0.0%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+1.0%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms20.69 ± (20.66 - 20.73) ms20.84 ± (20.81 - 20.87) ms+0.7%✅⬆️
process.time_to_main_ms69.42 ± (69.28 - 69.55) ms69.83 ± (69.71 - 69.94) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.63 ± (10.63 - 10.64) MB10.64 ± (10.64 - 10.64) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms20.70 ± (20.66 - 20.74) ms20.75 ± (20.72 - 20.78) ms+0.2%✅⬆️
process.time_to_main_ms70.77 ± (70.66 - 70.89) ms70.92 ± (70.80 - 71.03) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.73 ± (10.72 - 10.73) MB10.75 ± (10.74 - 10.75) MB+0.2%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms253.53 ± (252.60 - 254.45) ms253.46 ± (252.69 - 254.23) ms-0.0%
process.time_to_main_ms448.80 ± (448.36 - 449.25) ms450.36 ± (449.87 - 450.84) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.42 ± (48.39 - 48.45) MB48.42 ± (48.40 - 48.45) MB+0.0%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%
.NET 8 - Baseline
process.internal_duration_ms18.90 ± (18.87 - 18.93) ms19.10 ± (19.07 - 19.13) ms+1.1%✅⬆️
process.time_to_main_ms68.50 ± (68.37 - 68.63) ms69.19 ± (69.02 - 69.36) ms+1.0%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.67 ± (7.67 - 7.68) MB7.68 ± (7.68 - 7.69) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms18.93 ± (18.90 - 18.96) ms19.08 ± (19.05 - 19.11) ms+0.8%✅⬆️
process.time_to_main_ms69.60 ± (69.51 - 69.70) ms69.78 ± (69.68 - 69.89) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.75 ± (7.74 - 7.76) MB7.74 ± (7.73 - 7.74) MB-0.1%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms180.12 ± (179.14 - 181.10) ms180.19 ± (179.18 - 181.20) ms+0.0%✅⬆️
process.time_to_main_ms429.68 ± (429.11 - 430.25) ms429.59 ± (428.94 - 430.24) ms-0.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed35.86 ± (35.83 - 35.90) MB35.99 ± (35.95 - 36.02) MB+0.3%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)+0.3%✅⬆️

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration191.84 ± (191.77 - 192.50) ms194.49 ± (194.43 - 195.25) ms+1.4%✅⬆️
.NET Framework 4.8 - Bailout
duration196.78 ± (196.41 - 197.12) ms197.50 ± (197.45 - 198.17) ms+0.4%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1139.81 ± (1138.81 - 1145.14) ms1150.72 ± (1152.79 - 1160.73) ms+1.0%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms186.60 ± (186.24 - 186.96) ms188.93 ± (188.51 - 189.35) ms+1.2%✅⬆️
process.time_to_main_ms81.01 ± (80.79 - 81.23) ms82.03 ± (81.80 - 82.25) ms+1.3%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.23 ± (16.21 - 16.26) MB16.00 ± (15.97 - 16.03) MB-1.4%
runtime.dotnet.threads.count20 ± (19 - 20)20 ± (19 - 20)+0.1%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms185.90 ± (185.58 - 186.22) ms188.25 ± (187.87 - 188.63) ms+1.3%✅⬆️
process.time_to_main_ms81.78 ± (81.65 - 81.91) ms82.92 ± (82.77 - 83.06) ms+1.4%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.20 ± (16.17 - 16.23) MB16.18 ± (16.16 - 16.21) MB-0.1%
runtime.dotnet.threads.count21 ± (20 - 21)21 ± (21 - 21)+0.8%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms438.06 ± (436.09 - 440.04) ms437.03 ± (434.07 - 439.98) ms-0.2%
process.time_to_main_ms472.05 ± (471.45 - 472.66) ms477.23 ± (476.64 - 477.82) ms+1.1%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed57.93 ± (57.82 - 58.05) MB58.02 ± (57.89 - 58.15) MB+0.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 30)29 ± (29 - 29)-0.1%
.NET 6 - Baseline
process.internal_duration_ms190.99 ± (190.59 - 191.39) ms191.98 ± (191.70 - 192.27) ms+0.5%✅⬆️
process.time_to_main_ms70.51 ± (70.32 - 70.70) ms70.67 ± (70.48 - 70.86) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.07 ± (15.92 - 16.22) MB15.95 ± (15.79 - 16.10) MB-0.8%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)-0.1%
.NET 6 - Bailout
process.internal_duration_ms189.61 ± (189.28 - 189.94) ms192.00 ± (191.62 - 192.39) ms+1.3%✅⬆️
process.time_to_main_ms71.19 ± (71.04 - 71.34) ms72.19 ± (72.00 - 72.38) ms+1.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.83 ± (15.65 - 16.01) MB16.13 ± (15.98 - 16.29) MB+1.9%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 20)+2.4%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms448.57 ± (447.12 - 450.01) ms451.32 ± (449.15 - 453.49) ms+0.6%✅⬆️
process.time_to_main_ms448.07 ± (447.50 - 448.63) ms454.00 ± (453.45 - 454.55) ms+1.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed58.24 ± (58.14 - 58.35) MB58.07 ± (57.96 - 58.19) MB-0.3%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.1%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms188.14 ± (187.79 - 188.48) ms191.33 ± (190.93 - 191.74) ms+1.7%✅⬆️
process.time_to_main_ms69.35 ± (69.16 - 69.55) ms70.62 ± (70.40 - 70.83) ms+1.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.78 ± (11.75 - 11.81) MB11.76 ± (11.73 - 11.78) MB-0.2%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+0.3%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms187.55 ± (187.15 - 187.96) ms190.61 ± (190.24 - 190.98) ms+1.6%✅⬆️
process.time_to_main_ms70.52 ± (70.38 - 70.65) ms71.56 ± (71.43 - 71.69) ms+1.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.84 ± (11.81 - 11.87) MB11.82 ± (11.78 - 11.86) MB-0.2%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.7%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms363.88 ± (362.51 - 365.24) ms367.45 ± (366.09 - 368.82) ms+1.0%✅⬆️
process.time_to_main_ms431.73 ± (430.99 - 432.46) ms435.90 ± (435.20 - 436.60) ms+1.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed47.70 ± (47.67 - 47.74) MB47.77 ± (47.73 - 47.80) MB+0.1%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 29)29 ± (29 - 29)+1.0%✅⬆️
Comparison explanation

Execution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

Duration charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (69ms)  : 67, 72
    master - mean (69ms)  : 68, 71

    section Bailout
    This PR (7255) - mean (73ms)  : 72, 75
    master - mean (73ms)  : 72, 75

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (1,041ms)  : 1000, 1082
    master - mean (1,044ms)  : 990, 1098

Loading
FakeDbCommand (.NET Core 3.1)
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (108ms)  : 102, 115
    master - mean (108ms)  : 105, 110

    section Bailout
    This PR (7255) - mean (108ms)  : 106, 111
    master - mean (108ms)  : 106, 110

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (743ms)  : 682, 805
    master - mean (744ms)  : 699, 789

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (96ms)  : 94, 99
    master - mean (95ms)  : 93, 98

    section Bailout
    This PR (7255) - mean (97ms)  : 95, 99
    master - mean (97ms)  : 95, 99

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (733ms)  : 709, 756
    master - mean (736ms)  : 710, 762

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (95ms)  : 92, 98
    master - mean (94ms)  : 91, 97

    section Bailout
    This PR (7255) - mean (95ms)  : 93, 98
    master - mean (95ms)  : 93, 97

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (637ms)  : 621, 653
    master - mean (638ms)  : 626, 651

Loading
HttpMessageHandler (.NET Framework 4.8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (195ms)  : 190, 199
    master - mean (192ms)  : 189, 196

    section Bailout
    This PR (7255) - mean (198ms)  : 194, 201
    master - mean (197ms)  : 193, 200

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (1,157ms)  : 1100, 1214
    master - mean (1,142ms)  : 1096, 1188

Loading
HttpMessageHandler (.NET Core 3.1)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (280ms)  : 274, 286
    master - mean (276ms)  : 270, 282

    section Bailout
    This PR (7255) - mean (280ms)  : 276, 284
    master - mean (276ms)  : 272, 280

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (944ms)  : 902, 986
    master - mean (942ms)  : 906, 978

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (271ms)  : 267, 276
    master - mean (270ms)  : 264, 276

    section Bailout
    This PR (7255) - mean (273ms)  : 267, 278
    master - mean (269ms)  : 265, 273

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (936ms)  : 888, 984
    master - mean (926ms)  : 901, 951

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7255) - mean (272ms)  : 265, 280
    master - mean (267ms)  : 262, 272

    section Bailout
    This PR (7255) - mean (272ms)  : 267, 276
    master - mean (267ms)  : 262, 273

    section CallTarget+Inlining+NGEN
    This PR (7255) - mean (833ms)  : 809, 858
    master - mean (828ms)  : 813, 844

Loading

@pr-commenter
Copy link

pr-commenter bot commented Aug 19, 2025

Benchmarks

Benchmark execution time: 2026-02-27 01:07:35

Comparing candidate commit c2121d7 in PR branch lpimentel/refactor-log-directory with baseline commit 8b4147e in branch master.

Found 11 performance improvements and 7 performance regressions! Performance is the same for 165 metrics, 9 unstable metrics.

scenario:Benchmarks.Trace.ActivityBenchmark.StartStopWithChild netcoreapp3.1

  • 🟥 throughput [-15548.849op/s; -14081.324op/s] or [-15.840%; -14.345%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs net6.0

  • 🟩 execution_time [-22.389ms; -21.957ms] or [-11.128%; -10.913%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1

  • 🟩 execution_time [-21.757ms; -21.132ms] or [-10.784%; -10.474%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟥 execution_time [+14.224ms; +18.182ms] or [+7.383%; +9.437%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • 🟩 execution_time [-13.332ms; -10.483ms] or [-7.805%; -6.137%]
  • 🟩 throughput [+99.092op/s; +124.852op/s] or [+6.610%; +8.328%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟥 execution_time [+7.523ms; +13.234ms] or [+5.029%; +8.847%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice net472

  • 🟥 execution_time [+105.868µs; +110.386µs] or [+5.530%; +5.766%]
  • 🟥 throughput [-28.507op/s; -27.344op/s] or [-5.457%; -5.235%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OriginalCharSlice netcoreapp3.1

  • 🟩 execution_time [-1.558ms; -1.187ms] or [-39.417%; -30.023%]

scenario:Benchmarks.Trace.DbCommandBenchmark.ExecuteNonQuery netcoreapp3.1

  • 🟩 throughput [+22935.518op/s; +27525.948op/s] or [+6.408%; +7.690%]

scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog netcoreapp3.1

  • 🟩 execution_time [-18.268ms; -12.955ms] or [-8.608%; -6.104%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog netcoreapp3.1

  • 🟩 execution_time [-33.988ms; -31.728ms] or [-17.012%; -15.881%]

scenario:Benchmarks.Trace.RedisBenchmark.SendReceive net472

  • 🟩 throughput [+28658.853op/s; +30015.011op/s] or [+8.416%; +8.814%]

scenario:Benchmarks.Trace.SerilogBenchmark.EnrichedLog net472

  • 🟩 throughput [+13410.540op/s; +14479.410op/s] or [+9.462%; +10.217%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore netcoreapp3.1

  • 🟥 throughput [-15126874.265op/s; -13964143.906op/s] or [-6.296%; -5.812%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net472

  • 🟥 throughput [-79387.071op/s; -75917.262op/s] or [-7.039%; -6.732%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net6.0

  • 🟩 execution_time [-21.513ms; -15.831ms] or [-10.042%; -7.390%]

@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch from dccf49d to 3d77f7a Compare December 5, 2025 14:21
@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch from a69e50b to f0bf2b3 Compare December 17, 2025 23:27
@datadog-datadog-prod-us1

This comment has been minimized.

@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch 2 times, most recently from 65a9c16 to 956d0c9 Compare December 18, 2025 22:29
@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch from 956d0c9 to a5f7c9b Compare February 27, 2026 00:09
@lucaspimentel lucaspimentel changed the title small refactoring around GetDefaultLogDirectory() [Tracer] Refactor log directory resolution into smaller methods Feb 27, 2026
@lucaspimentel lucaspimentel marked this pull request as ready for review February 27, 2026 00:15
@lucaspimentel lucaspimentel requested a review from a team as a code owner February 27, 2026 00:15
@lucaspimentel lucaspimentel changed the title [Tracer] Refactor log directory resolution into smaller methods Refactor log directory resolution into smaller methods, add tests Feb 27, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a5f7c9bc77

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +324 to +326
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/root/nonexistent/invalid/path/that/cannot/be/created";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use a guaranteed non-creatable path in invalid-path test

The Linux branch in this test uses /root/... as an "invalid" directory, but that path is writable when tests run as root (for example in containerized CI), so TryCreateLogDirectory returns true and the assertion fails even though production code is correct. I reproduced this by running DatadogLoggingFactoryTests in this environment, where WithInvalidPath_ReturnsFalse fails for exactly this reason; the test should use a path that is invalid regardless of user privileges.

Useful? React with 👍 / 👎.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI is right I'm afraid, as shown by the failing tests 😅 Try this:

Suggested change
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/root/nonexistent/invalid/path/that/cannot/be/created";
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/dev/null/nonexistent/invalid/path/that/cannot/be/created";

@lucaspimentel lucaspimentel force-pushed the lpimentel/refactor-log-directory branch from a5f7c9b to c2121d7 Compare February 27, 2026 00:25
if (string.IsNullOrEmpty(logDirectory))
var isWindows = FrameworkDescription.Instance.IsWindows();

if (ImmutableAzureAppServiceSettings.IsRunningInAzureAppServices(source, telemetry))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you've removed the || ImmutableAzureAppServiceSettings.IsRunningInAzureFunctions() - is that correct?

Comment on lines +253 to +265
if (StringUtil.IsNullOrEmpty(programData))
{
try
{
Directory.CreateDirectory(logDirectory);
}
catch
// fallback #1: try reading from the env var
programData = EnvironmentHelpersNoLogging.ProgramData();

if (StringUtil.IsNullOrEmpty(programData))
{
// Unable to create the directory meaning that the user
// will have to create it on their own.
// Last effort at writing logs
logDirectory = Path.GetTempPath();
// fallback #2: hard-coded
programData = @"C:\ProgramData";
}
}

return logDirectory!;
return programData;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think early returns are neater in this case and reduces nesting

Suggested change
if (StringUtil.IsNullOrEmpty(programData))
{
try
{
Directory.CreateDirectory(logDirectory);
}
catch
// fallback #1: try reading from the env var
programData = EnvironmentHelpersNoLogging.ProgramData();
if (StringUtil.IsNullOrEmpty(programData))
{
// Unable to create the directory meaning that the user
// will have to create it on their own.
// Last effort at writing logs
logDirectory = Path.GetTempPath();
// fallback #2: hard-coded
programData = @"C:\ProgramData";
}
}
return logDirectory!;
return programData;
if (!StringUtil.IsNullOrEmpty(programData))
{
return programData;
}
// fallback #1: try reading from the env var
// fallback #2: hard-coded
var envProgramData = EnvironmentHelpersNoLogging.ProgramData();
return StringUtil.IsNullOrEmpty(envProgramData)
? @"C:\ProgramData"
: envProgramData;

}
}

public class GetDefaultLogDirectoryTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think these should be separate classes based on our typical code style

Comment on lines +247 to +260
[Fact]
public void ReturnsNonEmptyDirectory()
{
// Skip on non-Windows platforms since this method is only called on Windows
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var result = DatadogLoggingFactory.GetProgramDataDirectory();

result.Should().NotBeNullOrEmpty();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh, covered by the next test

Suggested change
[Fact]
public void ReturnsNonEmptyDirectory()
{
// Skip on non-Windows platforms since this method is only called on Windows
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}
var result = DatadogLoggingFactory.GetProgramDataDirectory();
result.Should().NotBeNullOrEmpty();
}

Comment on lines +324 to +326
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/root/nonexistent/invalid/path/that/cannot/be/created";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI is right I'm afraid, as shown by the failing tests 😅 Try this:

Suggested change
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/root/nonexistent/invalid/path/that/cannot/be/created";
var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? @"Z:\nonexistent\invalid\path\that\cannot\be\created"
: "/dev/null/nonexistent/invalid/path/that/cannot/be/created";

Comment on lines +287 to +306
public void WithValidPath_CreatesDirectoryAndReturnsTrue()
{
var logDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.Exists(logDirectory).Should().BeFalse();

var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory);

result.Should().BeTrue();
Directory.Exists(logDirectory).Should().BeTrue();

// Cleanup
try
{
Directory.Delete(logDirectory);
}
catch
{
// Ignore cleanup errors
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd just combine this and the final tests. Plus the cleanup theoretically needs "fixing" to cleanup in case of failure:

Suggested change
public void WithValidPath_CreatesDirectoryAndReturnsTrue()
{
var logDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.Exists(logDirectory).Should().BeFalse();
var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory);
result.Should().BeTrue();
Directory.Exists(logDirectory).Should().BeTrue();
// Cleanup
try
{
Directory.Delete(logDirectory);
}
catch
{
// Ignore cleanup errors
}
}
public void WithValidPath_CreatesDirectoryAndReturnsTrue()
{
var parentDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var logDirectory = Path.Combine(parentDir, "nested", "log", "directory");
Directory.Exists(logDirectory).Should().BeFalse();
Directory.Exists(parentDir).Should().BeFalse();
try
{
var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory);
result.Should().BeTrue();
Directory.Exists(logDirectory).Should().BeTrue();
}
finally
{
// Cleanup
try
{
Directory.Delete(logDirectory, recursive: true);
}
catch
{
// Ignore cleanup errors
}
}
}

Comment on lines +331 to +355

[Fact]
public void WithNestedPath_CreatesAllDirectories()
{
var parentDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var logDirectory = Path.Combine(parentDir, "nested", "log", "directory");
Directory.Exists(logDirectory).Should().BeFalse();
Directory.Exists(parentDir).Should().BeFalse();

var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory);

result.Should().BeTrue();
Directory.Exists(logDirectory).Should().BeTrue();
Directory.Exists(parentDir).Should().BeTrue();

// Cleanup
try
{
Directory.Delete(parentDir, recursive: true);
}
catch
{
// Ignore cleanup errors
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: covered by first test

Suggested change
[Fact]
public void WithNestedPath_CreatesAllDirectories()
{
var parentDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var logDirectory = Path.Combine(parentDir, "nested", "log", "directory");
Directory.Exists(logDirectory).Should().BeFalse();
Directory.Exists(parentDir).Should().BeFalse();
var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory);
result.Should().BeTrue();
Directory.Exists(logDirectory).Should().BeTrue();
Directory.Exists(parentDir).Should().BeTrue();
// Cleanup
try
{
Directory.Delete(parentDir, recursive: true);
}
catch
{
// Ignore cleanup errors
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) type:cleanup Minor code clean up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants