-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into pbsampler-nolinks
- Loading branch information
Showing
20 changed files
with
779 additions
and
45 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,53 @@ | ||
// <copyright file="Program.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; | ||
using OpenTelemetry.Trace; | ||
|
||
public class Program | ||
{ | ||
private static readonly ActivitySource MyActivitySource = new ActivitySource( | ||
"MyCompany.MyProduct.MyLibrary"); | ||
|
||
public static void Main() | ||
{ | ||
using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => | ||
{ | ||
options.SetErrorStatusOnException = true; | ||
}) | ||
.AddSource("MyCompany.MyProduct.MyLibrary") | ||
.SetSampler(new AlwaysOnSampler()) | ||
.AddConsoleExporter() | ||
.Build(); | ||
|
||
try | ||
{ | ||
using (MyActivitySource.StartActivity("Foo")) | ||
{ | ||
using (MyActivitySource.StartActivity("Bar")) | ||
{ | ||
throw new Exception("Oops!"); | ||
} | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
// swallow the exception | ||
} | ||
} | ||
} |
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,115 @@ | ||
# Exception Handling | ||
|
||
## User-handled Exception | ||
|
||
The term `User-handled Exception` is used to describe exceptions that are | ||
handled by the application. | ||
|
||
While using `Activity` API, the common pattern would be: | ||
|
||
```csharp | ||
using (var activity = MyActivitySource.StartActivity("Foo")) | ||
{ | ||
try | ||
{ | ||
Func(); | ||
} | ||
catch (SomeException ex) | ||
{ | ||
activity?.SetStatus(Status.Error); | ||
DoSomething(); | ||
} | ||
catch (Exception) | ||
{ | ||
activity?.SetStatus(Status.Error); | ||
throw; | ||
} | ||
} | ||
``` | ||
|
||
The above approach could become hard to manage if there are deeply nested | ||
`Activity` objects, or there are activities created in a 3rd party library. | ||
|
||
The following configuration will automatically detect exception and set the | ||
activity status to `Error`: | ||
|
||
```csharp | ||
Sdk.CreateTracerProviderBuilder(options => { | ||
options.SetErrorStatusOnException = true; | ||
}); | ||
``` | ||
|
||
A complete example can be found [here](./Program.cs). | ||
|
||
Note: this feature is platform dependent as it relies on | ||
[System.Runtime.InteropServices.Marshal.GetExceptionPointers](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getexceptionpointers). | ||
|
||
## Unhandled Exception | ||
|
||
The term `Unhandled Exception` is used to describe exceptions that are not | ||
handled by the application. When an unhandled exception happened, the behavior | ||
will depend on the presence of a debugger: | ||
|
||
* If there is no debugger, the exception will normally crash the process or | ||
terminate the thread. | ||
* If a debugger is attached, the debugger will be notified that an unhandled | ||
exception happened. | ||
* In case a postmortem debugger is configured, the postmortem debugger will be | ||
activited and normally it will collect a crash dump. | ||
|
||
It might be useful to automatically capture the unhandled exceptions, travel | ||
through the unfinished activities and export them for troubleshooting. Here goes | ||
one possible way of doing this: | ||
|
||
**WARNING:** Use `AppDomain.UnhandledException` with caution. A throw in the | ||
handler puts the process into an unrecoverable state. | ||
|
||
<!-- markdownlint-disable MD013 --> | ||
```csharp | ||
using System; | ||
using System.Diagnostics; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Trace; | ||
|
||
public class Program | ||
{ | ||
private static readonly ActivitySource MyActivitySource = new ActivitySource("MyCompany.MyProduct.MyLibrary"); | ||
|
||
public static void Main() | ||
{ | ||
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; | ||
|
||
using var tracerProvider = Sdk.CreateTracerProviderBuilder(options => | ||
{ | ||
options.SetErrorStatusOnException = true; | ||
}) | ||
.AddSource("MyCompany.MyProduct.MyLibrary") | ||
.SetSampler(new AlwaysOnSampler()) | ||
.AddConsoleExporter() | ||
.Build(); | ||
|
||
using (MyActivitySource.StartActivity("Foo")) | ||
{ | ||
using (MyActivitySource.StartActivity("Bar")) | ||
{ | ||
throw new Exception("Oops!"); | ||
} | ||
} | ||
} | ||
|
||
private static void UnhandledExceptionHandler(object source, UnhandledExceptionEventArgs args) | ||
{ | ||
var ex = (Exception)args.ExceptionObject; | ||
|
||
var activity = Activity.Current; | ||
|
||
while (activity != null) | ||
{ | ||
activity.RecordException(ex); | ||
activity.Dispose(); | ||
activity = activity.Parent; | ||
} | ||
} | ||
} | ||
``` | ||
<!-- markdownlint-enable MD013 --> |
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,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<!--- | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="$(OpenTelemetryExporterConsolePkgVer)" /> | ||
--> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void | ||
OpenTelemetry.Trace.TracerProviderOptions | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void | ||
OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void | ||
static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action<OpenTelemetry.Trace.TracerProviderOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void | ||
OpenTelemetry.Trace.TracerProviderOptions | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void | ||
OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void | ||
static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action<OpenTelemetry.Trace.TracerProviderOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool |
14 changes: 14 additions & 0 deletions
14
src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(System.Action<object, TState> callback, TState state) -> void | ||
OpenTelemetry.Logs.LogRecord.Message.get -> string | ||
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>> | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void | ||
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void | ||
OpenTelemetry.Trace.TracerProviderOptions | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void | ||
OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void | ||
static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action<OpenTelemetry.Trace.TracerProviderOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool |
14 changes: 14 additions & 0 deletions
14
src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(System.Action<object, TState> callback, TState state) -> void | ||
OpenTelemetry.Logs.LogRecord.Message.get -> string | ||
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>> | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.set -> void | ||
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void | ||
OpenTelemetry.Trace.TracerProviderOptions | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.get -> bool | ||
OpenTelemetry.Trace.TracerProviderOptions.SetErrorStatusOnException.set -> void | ||
OpenTelemetry.Trace.TracerProviderOptions.TracerProviderOptions() -> void | ||
static OpenTelemetry.Sdk.CreateTracerProviderBuilder(System.Action<OpenTelemetry.Trace.TracerProviderOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacyActivity(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder | ||
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool |
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
Oops, something went wrong.