-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 1 - Use new Activity to Replace OT Span (#660)
- Loading branch information
1 parent
92963a3
commit 44b3a43
Showing
13 changed files
with
801 additions
and
6 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
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,89 @@ | ||
// <copyright file="TestConsoleActivity.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using OpenTelemetry.Exporter.Console; | ||
using OpenTelemetry.Trace.Configuration; | ||
|
||
namespace Samples | ||
{ | ||
internal class TestConsoleActivity | ||
{ | ||
internal static object Run(ConsoleActivityOptions options) | ||
{ | ||
// Enable OpenTelemetry for the source "MyCompany.MyProduct.MyWebServer" | ||
// and use Console exporter | ||
OpenTelemetrySdk.EnableOpenTelemetry( | ||
(builder) => builder.AddActivitySource("MyCompany.MyProduct.MyWebServer") | ||
.UseConsoleActivityExporter(opt => opt.DisplayAsJson = options.DisplayAsJson)); | ||
|
||
// The above line is required only in Applications | ||
// which decide to use OT. | ||
|
||
// Libraries would simply write the following lines of code to | ||
// emit activities, which are the .NET representation of OT Spans. | ||
var source = new ActivitySource("MyCompany.MyProduct.MyWebServer"); | ||
|
||
// The below commented out line shows more likely code in a real world webserver. | ||
// using (var parent = source.StartActivity("HttpIn", ActivityKind.Server, HttpContext.Request.Headers["traceparent"] )) | ||
using (var parent = source.StartActivity("HttpIn", ActivityKind.Server)) | ||
{ | ||
// TagNames can follow the OT guidelines | ||
// from https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions | ||
parent?.AddTag("http.method", "GET"); | ||
parent?.AddTag("http.host", "MyHostName"); | ||
if (parent != null) | ||
{ | ||
parent.DisplayName = "HttpIn DisplayName"; | ||
} | ||
|
||
try | ||
{ | ||
// Actual code to achieve the purpose of the library. | ||
// For websebserver example, this would be calling | ||
// user middlware pipeline. | ||
|
||
// There can be child activities. | ||
// In this example HttpOut is a child of HttpIn. | ||
using (var child = source.StartActivity("HttpOut", ActivityKind.Client)) | ||
{ | ||
child?.AddTag("http.url", "www.mydependencyapi.com"); | ||
try | ||
{ | ||
// do actual work. | ||
|
||
child?.AddEvent(new ActivityEvent("sample activity event.")); | ||
child?.AddTag("http.status_code", "200"); | ||
} | ||
catch (Exception) | ||
{ | ||
child?.AddTag("http.status_code", "500"); | ||
} | ||
} | ||
|
||
parent?.AddTag("http.status_code", "200"); | ||
} | ||
catch (Exception) | ||
{ | ||
parent?.AddTag("http.status_code", "500"); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
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,97 @@ | ||
// <copyright file="ConsoleActivityExporter.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OpenTelemetry.Trace.Export; | ||
|
||
namespace OpenTelemetry.Exporter.Console | ||
{ | ||
public class ConsoleActivityExporter : ActivityExporter | ||
{ | ||
private readonly JsonSerializerOptions serializerOptions; | ||
private bool displayAsJson; | ||
|
||
public ConsoleActivityExporter(ConsoleActivityExporterOptions options) | ||
{ | ||
this.serializerOptions = new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true, | ||
}; | ||
|
||
this.displayAsJson = options.DisplayAsJson; | ||
|
||
this.serializerOptions.Converters.Add(new JsonStringEnumConverter()); | ||
this.serializerOptions.Converters.Add(new ActivitySpanIdConverter()); | ||
this.serializerOptions.Converters.Add(new ActivityTraceIdConverter()); | ||
} | ||
|
||
public override Task<ExportResult> ExportAsync(IEnumerable<Activity> activityBatch, CancellationToken cancellationToken) | ||
{ | ||
foreach (var activity in activityBatch) | ||
{ | ||
if (this.displayAsJson) | ||
{ | ||
System.Console.WriteLine(JsonSerializer.Serialize(activity, this.serializerOptions)); | ||
} | ||
else | ||
{ | ||
System.Console.WriteLine("Activity ID - " + activity.Id); | ||
if (!string.IsNullOrEmpty(activity.ParentId)) | ||
{ | ||
System.Console.WriteLine("Activity ParentId - " + activity.ParentId); | ||
} | ||
|
||
System.Console.WriteLine("Activity OperationName - " + activity.OperationName); | ||
System.Console.WriteLine("Activity DisplayName - " + activity.DisplayName); | ||
System.Console.WriteLine("Activity StartTime - " + activity.StartTimeUtc); | ||
System.Console.WriteLine("Activity Duration - " + activity.Duration); | ||
if (activity.Tags.Count() > 0) | ||
{ | ||
System.Console.WriteLine("Activity Tags"); | ||
foreach (var tag in activity.Tags) | ||
{ | ||
System.Console.WriteLine($"\t {tag.Key} : {tag.Value}"); | ||
} | ||
} | ||
|
||
if (activity.Events.Any()) | ||
{ | ||
System.Console.WriteLine("Activity Events"); | ||
foreach (var activityEvent in activity.Events) | ||
{ | ||
System.Console.WriteLine($"Event Name: {activityEvent.Name} TimeStamp: {activityEvent.Timestamp}"); | ||
} | ||
} | ||
|
||
System.Console.WriteLine("\n"); | ||
} | ||
} | ||
|
||
return Task.FromResult(ExportResult.Success); | ||
} | ||
|
||
public override Task ShutdownAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/OpenTelemetry.Exporter.Console/ConsoleActivityExporterOptions.cs
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,23 @@ | ||
// <copyright file="ConsoleActivityExporterOptions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
namespace OpenTelemetry.Exporter.Console | ||
{ | ||
public class ConsoleActivityExporterOptions | ||
{ | ||
public bool DisplayAsJson { get; set; } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/OpenTelemetry.Exporter.Console/OpenTelemetryBuilderExtensions.cs
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,48 @@ | ||
// <copyright file="OpenTelemetryBuilderExtensions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using OpenTelemetry.Trace.Configuration; | ||
|
||
namespace OpenTelemetry.Exporter.Console | ||
{ | ||
public static class OpenTelemetryBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a ConsoleActivity exporter. | ||
/// </summary> | ||
/// <param name="builder">Open Telemetry builder to use.</param> | ||
/// <param name="configure">Exporter configuration options.</param> | ||
/// <returns>The instance of <see cref="OpenTelemetryBuilder"/> to chain the calls.</returns> | ||
public static OpenTelemetryBuilder UseConsoleActivityExporter(this OpenTelemetryBuilder builder, Action<ConsoleActivityExporterOptions> configure) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
if (configure == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configure)); | ||
} | ||
|
||
var exporterOptions = new ConsoleActivityExporterOptions(); | ||
configure(exporterOptions); | ||
var consoleExporter = new ConsoleActivityExporter(exporterOptions); | ||
return builder.SetProcessorPipeline(pipeline => pipeline.SetExporter(consoleExporter)); | ||
} | ||
} | ||
} |
Oops, something went wrong.