-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Step 1 - Use new Activity to Replace OT Span #660
Changes from all commits
de877fb
d063b01
14b5298
3ecb6f3
729cda1
f61e636
9552fa8
b40be27
8e4b3f9
9e452ec
017b289
ad47cdc
4b19fe1
620023a
9066f72
3cba75c
c988dd1
1019ba7
e895e42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the spec it is an integer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup. Tags are currently string,string only. This is a drift from OT spec. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current adapters use Attributes for storing status_code with ("http.status_code", 200). Attributes support int, but its replacement Activity.Tags only support string. On top of this, Spans have Status, which don't have a place in Activity yet - the suggestion was to use activity.SetCustomProperty, for the SpanStatus. This was not finalized as well, as there was plans to drop the Status from Span?. Agree this needs to be finalized - do we need it for this PR or we can do it when more clarity is achieved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussion in dotnet activity PR: https://github.com/dotnet/designs/pull/98/files#diff-25bba2a21bc64ddeccd374c1c28a0fefR406 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It will be good if exporters (at least the ones on this repo) have consistent behavior in this regard, eg.: by default have every
is anybody actively pursuing that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the discussion here regarding supporting the OT Status type, then SetCustomProperty can be used here. But if we are talking about adding simple HTTP status code, then I believe using Tags will be more reasonable as it will log a simple code and no further information attached to it (e.g. description or even the thrown exception). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For HTTP the existing semantic conventions are already enough (and even the OTel Status can be derived from that). For others wanting to set span status or equivalent, we need further conventions to do the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any pending work to support non-string values as activity tags? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. Tarek is working on proposals. |
||
} | ||
catch (Exception) | ||
{ | ||
parent?.AddTag("http.status_code", "500"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add example with the result setting. I bet the most of implementation will need |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If parent may be null (see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. Parent will be null if none is listening. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to be a real pain, many many people will forgot the null check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To sergey's qn - the using statement doesn't fail. |
||
|
||
return null; | ||
} | ||
} | ||
} |
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; | ||
} | ||
} | ||
} |
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; } | ||
} | ||
} |
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)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really do not like this API - it's very easy to missing a null check and introduce a non-obvious error. It also goes against the OpenTelemetry spec that a no-op tracer / span is returned in such a way no code needs to change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StartActivity is not different than any other API that can return a null value. we use the
?
for simplifying the code. Usually, users adopt new APIs will look at the doc and usage patterns. Also, usually, anyone writes code like that, is going to test it. simple case testing will catch this problem very easily.There is no code that needs to change anyway. right? The code will be the same either there are listeners or not. please notice, Activity is a type that is used for many years. In our design, we wanted to serve both users already using Activity and OT scenarios.
CC @noahfalk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @tarekgh - I didn't mean my comments to come across so negative, sorry. I'll try to be more constructive.
I guess my main issue is that it is significantly different in usage compared to the OpenTelemetry specification. The span & tracer interfaces are designed to be robust and easy to use, providing an efficient no-op design if configuration is incomplete.
The Activity API can return null and must be checked for manually every time a usr wishes to interact with it, which becomes error prone if using the shorthand null check. It also does not support non-string tags (see http status code) which deviates from the spec and will cause propagation issues between language implementations (all other languages allow non-strings).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MikeGoldsmith I appreciate your feedback, it is helpful. So, don't hesitate to send more :-)
Yes, I understand there are some differences between the OT specs and the .NET APIs. We have been working hard to get them close and fill the gaps but at the same time, we are restricted by some other constraints. We had to come with a design that not confuses a lot of developers who currently using the Activity APIs and at the same allow the OT scenarios. Think about it as we are not strictly implementing the OT spec as it is but we are enabling all scenarios that OT can do.
We'll continue to look at the feedback and try to figure out how we can help more with that. Thanks again for your feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries at all @MikeGoldsmith, your initial feedback was straightforward and the objections were grounded. It was fine feedback : )
I agree, it is different. From my perspective deciding to reuse Activity APIs and integrate into the platform rather than creating a competing API has a lot of advantages, but one disadvantage is that it creates additional constraints on the design. In this case OT's premise is that it is easy and cheap to create a no-op Span so that nobody has to do a null check. We considered a no-op Activity, but didn't like the options:
So we ended at option 3, which was let the user see the null result. Our hope is that compiler nullable reference analysis makes it obvious when you've done the wrong thing, or if you are using an older compiler then code snippets, docs, and testing. Certainly its possible that this will be more problematic than we estimated so getting this kind of feedback is important (thanks!). So far we haven't heard much concern raised around this, but our overall feedback volume is probably still low.
This one should probably be separated into its own issue. @cijothomas @reyang - have you guys experimented much with tags that would be non-string types in other languages? Certainly I see some potential for friction here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. @cijothomas @tarekgh and I were chatting about this topic earlier today. We do see a good value of having non-string types.