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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>

<PackageId>CodingWithCalvin.Otel4Vsix</PackageId>
<Version>0.0.15-local</Version>
<Version>0.0.1</Version>
<Authors>Calvin A. Allen</Authors>
<Company>Coding With Calvin</Company>
<Product>CodingWithCalvin.Otel4Vsix</Product>
Expand Down Expand Up @@ -40,10 +40,10 @@
<PackageReference Include="OpenTelemetry" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="Google.Protobuf" Version="3.22.5" />
<PackageReference Include="Grpc.Core.Api" Version="2.52.0" />
<PackageReference Include="Microsoft.VisualStudio.Shell.15.0" Version="17.0.31903.59" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions src/CodingWithCalvin.Otel4Vsix/Exporters/DebugActivityExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Diagnostics;
using System.Text;
using OpenTelemetry;

namespace CodingWithCalvin.Otel4Vsix.Exporters;

/// <summary>
/// An activity exporter that writes trace data to <see cref="System.Diagnostics.Debug"/>.
/// Output appears in the Visual Studio Output window when debugging.
/// </summary>
internal sealed class DebugActivityExporter : BaseExporter<Activity>
{
private readonly string _prefix;

public DebugActivityExporter(string serviceName, string serviceVersion)
{
_prefix = $"[{serviceName} v{serviceVersion}]";
}

/// <inheritdoc />
public override ExportResult Export(in Batch<Activity> batch)
{
foreach (var activity in batch)
{
var sb = new StringBuilder();
sb.AppendLine($"{_prefix} [TRACE] {activity.DisplayName}");
sb.AppendLine($" TraceId: {activity.TraceId}");
sb.AppendLine($" SpanId: {activity.SpanId}");
sb.AppendLine($" Duration: {activity.Duration.TotalMilliseconds:F2}ms");
sb.AppendLine($" Status: {activity.Status}");

if (activity.Tags != null)
{
foreach (var tag in activity.Tags)
{
sb.AppendLine($" {tag.Key}: {tag.Value}");
}
}

if (activity.Events != null)
{
foreach (var evt in activity.Events)
{
sb.AppendLine($" Event: {evt.Name} at {evt.Timestamp}");
}
}

System.Diagnostics.Trace.WriteLine(sb.ToString());
}

return ExportResult.Success;
}
}
75 changes: 75 additions & 0 deletions src/CodingWithCalvin.Otel4Vsix/Exporters/DebugLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using Microsoft.Extensions.Logging;

namespace CodingWithCalvin.Otel4Vsix.Exporters;

/// <summary>
/// A logger provider that writes to <see cref="System.Diagnostics.Debug"/>.
/// </summary>
internal sealed class DebugLoggerProvider : ILoggerProvider
{
private readonly string _serviceName;
private readonly string _serviceVersion;

public DebugLoggerProvider(string serviceName, string serviceVersion)
{
_serviceName = serviceName;
_serviceVersion = serviceVersion;
}

public ILogger CreateLogger(string categoryName) => new DebugLogger(_serviceName, _serviceVersion, categoryName);

public void Dispose() { }
}

/// <summary>
/// A logger that writes to <see cref="System.Diagnostics.Debug"/>.
/// </summary>
internal sealed class DebugLogger : ILogger
{
private readonly string _prefix;
private readonly string _categoryName;

public DebugLogger(string serviceName, string serviceVersion, string categoryName)
{
_prefix = $"[{serviceName} v{serviceVersion}]";
_categoryName = categoryName;
}

public IDisposable BeginScope<TState>(TState state) => NullScope.Instance;

public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}

var message = formatter(state, exception);
var levelStr = logLevel switch
{
LogLevel.Trace => "TRACE",
LogLevel.Debug => "DEBUG",
LogLevel.Information => "INFO",
LogLevel.Warning => "WARN",
LogLevel.Error => "ERROR",
LogLevel.Critical => "CRIT",
_ => "NONE"
};

System.Diagnostics.Trace.WriteLine($"{_prefix} [{levelStr}] {_categoryName}: {message}");

if (exception != null)
{
System.Diagnostics.Trace.WriteLine($" Exception: {exception}");
}
}

private sealed class NullScope : IDisposable
{
public static readonly NullScope Instance = new NullScope();
public void Dispose() { }
}
}
77 changes: 77 additions & 0 deletions src/CodingWithCalvin.Otel4Vsix/Exporters/DebugMetricExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Text;
using OpenTelemetry;
using OpenTelemetry.Metrics;

namespace CodingWithCalvin.Otel4Vsix.Exporters;

/// <summary>
/// A metric exporter that writes metric data to <see cref="System.Diagnostics.Debug"/>.
/// Output appears in the Visual Studio Output window when debugging.
/// </summary>
internal sealed class DebugMetricExporter : BaseExporter<Metric>
{
private readonly string _prefix;

public DebugMetricExporter(string serviceName, string serviceVersion)
{
_prefix = $"[{serviceName} v{serviceVersion}]";
}

/// <inheritdoc />
public override ExportResult Export(in Batch<Metric> batch)
{
foreach (var metric in batch)
{
var sb = new StringBuilder();
sb.AppendLine($"{_prefix} [METRIC] {metric.Name}");

if (!string.IsNullOrEmpty(metric.Unit))
{
sb.AppendLine($" Unit: {metric.Unit}");
}

if (!string.IsNullOrEmpty(metric.Description))
{
sb.AppendLine($" Description: {metric.Description}");
}

foreach (var metricPoint in metric.GetMetricPoints())
{
sb.Append(" ");

switch (metric.MetricType)
{
case MetricType.LongSum:
case MetricType.LongSumNonMonotonic:
sb.AppendLine($"Value: {metricPoint.GetSumLong()}");
break;
case MetricType.DoubleSum:
case MetricType.DoubleSumNonMonotonic:
sb.AppendLine($"Value: {metricPoint.GetSumDouble()}");
break;
case MetricType.LongGauge:
sb.AppendLine($"Value: {metricPoint.GetGaugeLastValueLong()}");
break;
case MetricType.DoubleGauge:
sb.AppendLine($"Value: {metricPoint.GetGaugeLastValueDouble()}");
break;
case MetricType.Histogram:
sb.AppendLine($"Count: {metricPoint.GetHistogramCount()}, Sum: {metricPoint.GetHistogramSum()}");
break;
default:
sb.AppendLine($"Type: {metric.MetricType}");
break;
}

foreach (var tag in metricPoint.Tags)
{
sb.AppendLine($" {tag.Key}: {tag.Value}");
}
}

System.Diagnostics.Trace.WriteLine(sb.ToString());
}

return ExportResult.Success;
}
}
Loading
Loading