Skip to content

Commit 802cf58

Browse files
committed
convert large trace file
1 parent c06ab4c commit 802cf58

File tree

8 files changed

+300
-92
lines changed

8 files changed

+300
-92
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Feature: TraceConvert
2+
As a developer,
3+
I want to be able to correlate calls wihin a service, as well as across services,
4+
So that I can monitor and troubleshoot the system.
5+
6+
7+
@trace
8+
@prod
9+
Scenario: Export otel oltp trace to temp trace
10+
Given otlp trace file at "TestData/Traces/otlp-traces.json"
11+
When I export the trace to a temp folder "TestData/Traces/tempo"
12+
Then the temp files should exist
13+
14+
@trace
15+
@prod
16+
Scenario: Export large oltp trace to temp trace
17+
Given otlp trace file at "TestData/Traces/traces-2025-05-17T12-27-08.769.json"
18+
When I export the trace to a temp folder "TestData/Traces/tempo"
19+
Then the temp files should exist

src/Common.Monitoring.Tests/Features/TraceConvert.feature.cs

Lines changed: 184 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common.Monitoring.Tests/Features/Traces.feature

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,3 @@ So that I can monitor and troubleshoot the system.
1919
| Fibonacci | WhenICalculateFibonacciOfTheNumber | input.n: 4, result 3 |
2020
| WhenICalculateFibonacciOfTheNumber | | result: 3 |
2121
| ThenTheResultShouldBe | | expected: 3, actual: 3 |
22-
23-
@trace
24-
@prod
25-
Scenario: Export otel oltp trace to temp trace
26-
Given otlp trace file at "TestData/Traces/otlp-traces.json"
27-
When I export the trace to a temp folder "TestData/Traces/tempo"
28-
Then the temp files should exist

src/Common.Monitoring.Tests/Features/Traces.feature.cs

Lines changed: 0 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common.Monitoring.Tests/Hooks/SelfHost.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public SelfHost(ScenarioContext scenarioContext, FeatureContext featureContext,
6464
};
6565
}
6666

67-
[BeforeScenario(Order = 3)]
67+
// [BeforeScenario(Order = 3)]
6868
public void StartHost()
6969
{
7070
if (!this.featureContext.TryGetValue("UsedPorts", out List<int> usedPorts))
@@ -101,7 +101,7 @@ public void StartHost()
101101
this.scenarioContext.Set(httpClient); // automatically disposed after test
102102
}
103103

104-
[AfterScenario]
104+
// [AfterScenario]
105105
public void StopHost()
106106
{
107107
this.listener?.Stop();
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="TraceTestSteps.cs" company="Microsoft Corp.">
3+
// Copyright (c) Microsoft Corp. All rights reserved.
4+
// </copyright>
5+
// -----------------------------------------------------------------------
6+
7+
namespace Common.Monitoring.Tests.Steps
8+
{
9+
using System;
10+
using System.IO;
11+
using System.Linq;
12+
using System.Text.Json;
13+
using Common.Monitoring.Utils;
14+
using FluentAssertions;
15+
using Reqnroll;
16+
17+
[Binding]
18+
public class ConvertTraceSteps
19+
{
20+
private readonly ScenarioContext context;
21+
private readonly IReqnrollOutputHelper outputHelper;
22+
23+
public ConvertTraceSteps(ScenarioContext context, IReqnrollOutputHelper outputHelper)
24+
{
25+
this.context = context;
26+
this.outputHelper = outputHelper;
27+
}
28+
29+
[Given("otlp trace file at \"([^\"]+)\"")]
30+
public void GivenOtlpTraceFileAt(string otlpTraceFile)
31+
{
32+
this.context.Set(otlpTraceFile, "otlpTraceFile");
33+
}
34+
35+
[When("I export the trace to a temp folder \"([^\"]+)\"")]
36+
public void WhenIExportTheTraceToATempFile(string tempTraceFolder)
37+
{
38+
var otlpTraceFile = this.context.Get<string>("otlpTraceFile");
39+
this.outputHelper.WriteLine($"reading otlp trace file {otlpTraceFile}...");
40+
var parser = new OtlpTraceParser();
41+
var parsedTempTraces = parser.TempoTraceFromOtlpJsonFile(otlpTraceFile);
42+
this.outputHelper.WriteLine($"parsed {parsedTempTraces.Count} traces from otlp trace file {otlpTraceFile}...");
43+
44+
this.context.Set(tempTraceFolder, "tempTraceFolder");
45+
if (!Directory.Exists(tempTraceFolder))
46+
{
47+
Directory.CreateDirectory(tempTraceFolder);
48+
}
49+
50+
foreach (var trace in parsedTempTraces)
51+
{
52+
var traceId = trace.traceId;
53+
var root = trace.root;
54+
var firstSpan = root.Batches
55+
.SelectMany(b => b.InstrumentationLibrarySpans)
56+
.SelectMany(s => s.Spans)
57+
.OrderBy(s => s.StartTimeUnixNano)
58+
.FirstOrDefault();
59+
if (firstSpan == null)
60+
{
61+
continue;
62+
}
63+
64+
var unixMilliseconds = firstSpan.StartTimeUnixNano / 1_000_000;
65+
var spanStartTime = DateTimeOffset.FromUnixTimeMilliseconds((long)unixMilliseconds).UtcDateTime;
66+
var traceFileName = $"{spanStartTime.ToLocalTime():yyyyMMdd-HHmm}-{firstSpan.Name}-{traceId.Substring(0, 6)}.json";
67+
var invalidChars = Path.GetInvalidFileNameChars();
68+
var sanitizedFileName = new string(traceFileName.Select(c => invalidChars.Contains(c) ? '_' : c).ToArray());
69+
var tempTraceFile = Path.Combine(tempTraceFolder, sanitizedFileName);
70+
File.WriteAllText(tempTraceFile, JsonSerializer.Serialize(root, parser.Options));
71+
}
72+
}
73+
74+
[Then("the temp files should exist")]
75+
public void ThenTheTempFileShouldExist()
76+
{
77+
var tempTraceFolder = this.context.Get<string>("tempTraceFolder");
78+
var tempTraceFiles = Directory.GetFiles(tempTraceFolder);
79+
tempTraceFiles.Should().NotBeEmpty();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)