-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal Tracing : Adds tracing library to the SDK (#1841)
* drafted out trace library * added no op trace * got trace writer working * made tracewriter static * refactored out repeat code * made character set configurable * resolved iteration comments * added support for multiple serialization formats * fixed NRE do to stack frame not having file path * removed unnecessary using
- Loading branch information
Showing
18 changed files
with
1,537 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
294 changes: 153 additions & 141 deletions
294
Microsoft.Azure.Cosmos/src/Query/Core/Metrics/QueryMetricsTextWriter.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
using System; | ||
|
||
internal readonly struct CallerInfo | ||
{ | ||
public CallerInfo(string memberName, string filePath, int lineNumber) | ||
{ | ||
this.MemberName = memberName ?? throw new ArgumentNullException(nameof(memberName)); | ||
this.FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath)); | ||
this.LineNumber = lineNumber < 0 ? throw new ArgumentOutOfRangeException(nameof(lineNumber)) : lineNumber; | ||
} | ||
|
||
public string MemberName { get; } | ||
public string FilePath { get; } | ||
public int LineNumber { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
internal interface ITrace : IDisposable | ||
{ | ||
string Name { get; } | ||
|
||
Guid Id { get; } | ||
|
||
CallerInfo CallerInfo { get; } | ||
|
||
DateTime StartTime { get; } | ||
|
||
TimeSpan Duration { get; } | ||
|
||
TraceLevel Level { get; } | ||
|
||
TraceComponent Component { get; } | ||
|
||
ITrace Parent { get; } | ||
|
||
IReadOnlyList<ITrace> Children { get; } | ||
|
||
IReadOnlyDictionary<string, object> Data { get; } | ||
|
||
ITrace StartChild( | ||
string name, | ||
[CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0); | ||
|
||
ITrace StartChild( | ||
string name, | ||
TraceComponent component, | ||
TraceLevel level, | ||
[CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0); | ||
|
||
void AddDatum(string key, ITraceDatum traceDatum); | ||
|
||
void AddDatum(string key, object value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
internal interface ITraceDatum | ||
{ | ||
void Accept(ITraceDatumVisitor traceDatumVisitor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
using Microsoft.Azure.Cosmos.Tracing.TraceData; | ||
|
||
internal interface ITraceDatumVisitor | ||
{ | ||
void Visit(CosmosDiagnosticsTraceDatum cosmosDiagnosticsTraceDatum); | ||
|
||
void Visit(QueryMetricsTraceDatum queryMetricsTraceDatum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
internal sealed class NoOpTrace : ITrace | ||
{ | ||
public static readonly NoOpTrace Singleton = new NoOpTrace(); | ||
|
||
private static readonly IReadOnlyList<ITrace> NoOpChildren = new List<ITrace>(); | ||
private static readonly IReadOnlyDictionary<string, object> NoOpData = new Dictionary<string, object>(); | ||
private static readonly CallerInfo NoOpCallerInfo = new CallerInfo(memberName: "NoOp", filePath: "NoOp", lineNumber: 9001); | ||
|
||
private NoOpTrace() | ||
{ | ||
} | ||
|
||
public string Name => "NoOp"; | ||
|
||
public Guid Id => default; | ||
|
||
public CallerInfo CallerInfo => NoOpCallerInfo; | ||
|
||
public DateTime StartTime => default; | ||
|
||
public TimeSpan Duration => default; | ||
|
||
public TraceLevel Level => default; | ||
|
||
public TraceComponent Component => default; | ||
|
||
public ITrace Parent => null; | ||
|
||
public IReadOnlyList<ITrace> Children => NoOpChildren; | ||
|
||
public IReadOnlyDictionary<string, object> Data => NoOpData; | ||
|
||
public void Dispose() | ||
{ | ||
// NoOp | ||
} | ||
|
||
public ITrace StartChild( | ||
string name, | ||
string memberName = "", | ||
string sourceFilePath = "", | ||
int sourceLineNumber = 0) => this.StartChild( | ||
name, | ||
component: this.Component, | ||
level: TraceLevel.Info); | ||
|
||
public ITrace StartChild( | ||
string name, | ||
TraceComponent component, | ||
TraceLevel level, | ||
string memberName = "", | ||
string sourceFilePath = "", | ||
int sourceLineNumber = 0) | ||
{ | ||
return this; | ||
} | ||
|
||
public void AddDatum(string key, ITraceDatum traceDatum) | ||
{ | ||
// NoOp | ||
} | ||
|
||
public void AddDatum(string key, object value) | ||
{ | ||
// NoOp | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
internal sealed class Trace : ITrace | ||
{ | ||
private readonly List<Trace> children; | ||
private readonly Dictionary<string, object> data; | ||
private readonly Stopwatch stopwatch; | ||
|
||
private Trace( | ||
string name, | ||
CallerInfo callerInfo, | ||
TraceLevel level, | ||
TraceComponent component, | ||
Trace parent) | ||
{ | ||
this.Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
this.Id = Guid.NewGuid(); | ||
this.CallerInfo = callerInfo; | ||
this.StartTime = DateTime.UtcNow; | ||
this.stopwatch = Stopwatch.StartNew(); | ||
this.Level = level; | ||
this.Component = component; | ||
this.Parent = parent; | ||
this.children = new List<Trace>(); | ||
this.data = new Dictionary<string, object>(); | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public Guid Id { get; } | ||
|
||
public CallerInfo CallerInfo { get; } | ||
|
||
public DateTime StartTime { get; } | ||
|
||
public TimeSpan Duration => this.stopwatch.Elapsed; | ||
|
||
public TraceLevel Level { get; } | ||
|
||
public TraceComponent Component { get; } | ||
|
||
public ITrace Parent { get; } | ||
|
||
public IReadOnlyList<ITrace> Children => this.children; | ||
|
||
public IReadOnlyDictionary<string, object> Data => this.data; | ||
|
||
public void Dispose() | ||
{ | ||
this.stopwatch.Stop(); | ||
} | ||
|
||
public ITrace StartChild( | ||
string name, | ||
[CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0) => this.StartChild( | ||
name, | ||
level: TraceLevel.Verbose, | ||
component: this.Component, | ||
memberName: memberName, | ||
sourceFilePath: sourceFilePath, | ||
sourceLineNumber: sourceLineNumber); | ||
|
||
public ITrace StartChild( | ||
string name, | ||
TraceComponent component, | ||
TraceLevel level, | ||
[CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0) | ||
{ | ||
Trace child = new Trace( | ||
name: name, | ||
callerInfo: new CallerInfo(memberName, sourceFilePath, sourceLineNumber), | ||
level: level, | ||
component: component, | ||
parent: this); | ||
this.children.Add(child); | ||
return child; | ||
} | ||
|
||
public static Trace GetRootTrace(string name) => Trace.GetRootTrace( | ||
name, | ||
component: TraceComponent.Unknown, | ||
level: TraceLevel.Verbose); | ||
|
||
public static Trace GetRootTrace( | ||
string name, | ||
TraceComponent component, | ||
TraceLevel level, | ||
[CallerMemberName] string memberName = "", | ||
[CallerFilePath] string sourceFilePath = "", | ||
[CallerLineNumber] int sourceLineNumber = 0) => new Trace( | ||
name: name, | ||
callerInfo: new CallerInfo(memberName, sourceFilePath, sourceLineNumber), | ||
level: level, | ||
component: component, | ||
parent: null); | ||
|
||
public void AddDatum(string key, ITraceDatum traceDatum) => this.data.Add(key, traceDatum); | ||
|
||
public void AddDatum(string key, object value) => this.data.Add(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tracing | ||
{ | ||
internal enum TraceComponent | ||
{ | ||
Transport, | ||
Query, | ||
Unknown, | ||
} | ||
} |
Oops, something went wrong.