Skip to content

Commit

Permalink
Merge branch 'main' into yunl/dropSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Oct 21, 2022
2 parents b7bc97d + bb9462c commit 1810c15
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-md.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- '**.md'
pull_request:
branches: [ main ]
paths-ignore:
paths:
- '**.md'

jobs:
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\dotnet-core-cov.yml = .github\workflows\dotnet-core-cov.yml
.github\workflows\dotnet-format-md.yml = .github\workflows\dotnet-format-md.yml
.github\workflows\dotnet-format.yml = .github\workflows\dotnet-format.yml
.github\workflows\integration-md.yml = .github\workflows\integration-md.yml
.github\workflows\integration.yml = .github\workflows\integration.yml
.github\workflows\linux-ci-md.yml = .github\workflows\linux-ci-md.yml
.github\workflows\linux-ci.yml = .github\workflows\linux-ci.yml
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.Hangfire/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add support to optionally record exceptions
([#719](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/719))

* Update OTel API version to `1.3.1`.
([#631](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/631))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="HangfireInstrumentationOptions.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.Trace;

/// <summary>
/// Options for hangfire jobs instrumentation.
/// </summary>
public class HangfireInstrumentationOptions
{
/// <summary>
/// Gets or sets a value indicating whether the exception will be recorded as ActivityEvent or not.
/// </summary>
/// <remarks>
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/exceptions.md.
/// </remarks>
public bool RecordException { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ namespace OpenTelemetry.Instrumentation.Hangfire.Implementation;
using global::Hangfire.Common;
using global::Hangfire.Server;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;

internal class HangfireInstrumentationJobFilterAttribute : JobFilterAttribute, IServerFilter, IClientFilter
{
private readonly HangfireInstrumentationOptions options;

public HangfireInstrumentationJobFilterAttribute(HangfireInstrumentationOptions options)
{
this.options = options;
}

public void OnPerforming(PerformingContext performingContext)
{
// Short-circuit if nobody is listening
Expand Down Expand Up @@ -72,7 +80,7 @@ public void OnPerformed(PerformedContext performedContext)
{
if (performedContext.Exception != null)
{
activity.SetStatus(ActivityStatusCode.Error, performedContext.Exception.Message);
this.SetStatusAndRecordException(activity, performedContext.Exception);
}

activity.Dispose();
Expand Down Expand Up @@ -111,4 +119,14 @@ private static IEnumerable<string> ExtractActivityProperties(Dictionary<string,
{
return telemetryData.ContainsKey(key) ? new[] { telemetryData[key] } : Enumerable.Empty<string>();
}

private void SetStatusAndRecordException(Activity activity, System.Exception exception)
{
activity.SetStatus(ActivityStatusCode.Error, exception.Message);

if (this.options.RecordException)
{
activity.RecordException(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace OpenTelemetry.Trace;

using System;
using OpenTelemetry.Instrumentation.Hangfire.Implementation;
using OpenTelemetry.Internal;

Expand All @@ -28,12 +29,18 @@ public static class TracerProviderBuilderExtensions
/// Adds Hangfire instrumentation to the tracer provider.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureHangfireInstrumentationOptions">Callback action for configuring <see cref="HangfireInstrumentationOptions"/>.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddHangfireInstrumentation(this TracerProviderBuilder builder)
public static TracerProviderBuilder AddHangfireInstrumentation(
this TracerProviderBuilder builder,
Action<HangfireInstrumentationOptions> configureHangfireInstrumentationOptions = null)
{
Guard.ThrowIfNull(builder);

Hangfire.GlobalJobFilters.Filters.Add(new HangfireInstrumentationJobFilterAttribute());
var options = new HangfireInstrumentationOptions();
configureHangfireInstrumentationOptions?.Invoke(options);

Hangfire.GlobalJobFilters.Filters.Add(new HangfireInstrumentationJobFilterAttribute(options));

return builder.AddSource(HangfireInstrumentation.ActivitySourceName);
}
Expand Down
21 changes: 19 additions & 2 deletions src/OpenTelemetry.Instrumentation.Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

## Unreleased

* Update OTel API version to `1.3.1`.
([#631](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/631))
* Update OTel API version to `1.4.0-beta.2`.

OTel API updated System.Diagnostics.DiagnosticSource to version 7.0.0
since [1.4.0-alpha.2](https://github.com/open-telemetry/opentelemetry-dotnet/releases/tag/core-1.4.0-alpha.2).

With this update, applications targeting .NET 5 and lower will receive a
warning at build time as described [here](https://github.com/dotnet/runtime/pull/72518)
(note: building using older versions of the .NET SDK produces an error at
build time). This is because .NET 5 reached EOL in May 2022 and .NET
Core 3.1 reaches EOL in December 2022.

There is no guarantee that System.Diagnostics.DiagnosticSource will continue
to work on older versions of .NET. However, the build warning can be
suppressed by setting the SuppressTfmSupportBuildWarnings MSBuild property.

This does not affect applications targeting .NET Framework.

* Change runtime metrics type to ObservableUpDownCounter from ObservableGauge
([#675](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/675))

## 1.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Api" Version="$(OpenTelemetryCoreLatestVersion)" />
<PackageReference Include="OpenTelemetry.Api" Version="1.4.0-beta.2" />
</ItemGroup>

<ItemGroup>
Expand Down
63 changes: 38 additions & 25 deletions src/OpenTelemetry.Instrumentation.Runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,11 @@ objects (the heap size) and some extra memory that is ready to handle newly
allocated objects in the future. The value will be unavailable until at least one
garbage collection has occurred.

Note: `ObservableGauge` should be changed to `ObservableUpDownCounter` once available,
as `ObservableUpDownCounter` is the best fit of instrument type. The same applies
to all the `ObservableGauge` below.

Note: This metric is only available when targeting .NET6 or later.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-----------------|------------|------------------|------------------|
| `bytes` | ObservableGauge | `Int64` | No Attributes | N/A |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-------------------------|------------|------------------|------------------|
| `bytes` | ObservableUpDownCounter | `Int64` | No Attributes | N/A |

The API used to retrieve the value is:

Expand All @@ -113,9 +109,9 @@ garbage collection has occurred.

Note: This metric is only available when targeting .NET6 or later.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-----------------|------------|------------------|----------------------------|
| `bytes` | ObservableGauge | `Int64` | generation | gen0, gen1, gen2, loh, poh |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-------------------------|------------|------------------|----------------------------|
| `bytes` | ObservableUpDownCounter | `Int64` | generation | gen0, gen1, gen2, loh, poh |

The API used to retrieve the value is:

Expand All @@ -134,9 +130,9 @@ The value will be unavailable until at least one garbage collection has occurred

Note: This metric is only available when targeting .NET 7 or later.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-----------------|------------|------------------|----------------------------|
| `bytes` | ObservableGauge | `Int64` | generation | gen0, gen1, gen2, loh, poh |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|---------|-------------------------|------------|------------------|----------------------------|
| `bytes` | ObservableUpDownCounter | `Int64` | generation | gen0, gen1, gen2, loh, poh |

The API used to retrieve the value is:

Expand Down Expand Up @@ -206,9 +202,9 @@ the lock keyword in C#, or by calling Monitor.Enter() and Monitor.TryEnter().

The number of thread pool threads that currently exist.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-------------|-------------------|------------|------------------|------------------|
| `{threads}` | ObservableGauge | `Int32` | No Attributes | N/A |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-------------|-------------------------|------------|------------------|------------------|
| `{threads}` | ObservableUpDownCounter | `Int32` | No Attributes | N/A |

#### process.runtime.dotnet.**thread_pool.completed_items.count**

Expand All @@ -224,9 +220,9 @@ since the process start.
The number of work items that are currently queued to be processed
by the thread pool.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-------------|-------------------|------------|------------------|------------------|
| `{items}` | ObservableGauge | `Int64` | No Attributes | N/A |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-----------|-------------------------|------------|------------------|------------------|
| `{items}` | ObservableUpDownCounter | `Int64` | No Attributes | N/A |

#### process.runtime.dotnet.**timer.count**

Expand All @@ -235,9 +231,9 @@ be created by many sources such as System.Threading.Timer, Task.Delay, or the
timeout in a CancellationSource. An active timer is registered to tick at some
point in the future and has not yet been canceled.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|-------------|-------------------|------------|------------------|------------------|
| `{timers}` | ObservableGauge | `Int64` | No Attributes | N/A |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|------------|-------------------------|------------|------------------|------------------|
| `{timers}` | ObservableUpDownCounter | `Int64` | No Attributes | N/A |

The APIs used to retrieve the values are:

Expand All @@ -260,9 +256,9 @@ The APIs used to retrieve the values are:

The number of .NET assemblies that are currently loaded.

| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|----------------|-----------------|------------|------------------|------------------|
| `{assemblies}` | ObservableGauge | `Int64` | No Attributes | N/A |
| Units | Instrument Type | Value Type | Attribute Key(s) | Attribute Values |
|----------------|-------------------------|------------|------------------|------------------|
| `{assemblies}` | ObservableUpDownCounter | `Int64` | No Attributes | N/A |

The API used to retrieve the value is:

Expand Down Expand Up @@ -301,6 +297,23 @@ metric is available in the .NET version you are running.
Some GC related metrics are unavailable until at least one garbage collection
has occurred.

## Note

OTel API updated System.Diagnostics.DiagnosticSource preview to version 7.0.0
since [1.4.0-alpha.2](https://github.com/open-telemetry/opentelemetry-dotnet/releases/tag/core-1.4.0-alpha.2).

With this update, applications targeting .NET 5 and lower will receive a
warning at build time as described [here](https://github.com/dotnet/runtime/pull/72518)
(note: building using older versions of the .NET SDK produces an error at
build time). This is because .NET 5 reached EOL in May 2022 and .NET
Core 3.1 reaches EOL in December 2022.

There is no guarantee that System.Diagnostics.DiagnosticSource will continue
to work on older versions of .NET. However, the build warning can be
suppressed by setting the SuppressTfmSupportBuildWarnings MSBuild property.

This does not affect applications targeting .NET Framework.

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
21 changes: 7 additions & 14 deletions src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ static RuntimeMetrics()
#endif

#if NET6_0_OR_GREATER
// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.committed_memory.size",
() =>
{
Expand Down Expand Up @@ -90,8 +89,7 @@ static RuntimeMetrics()
// Either Environment.Version is not 6 or (it's 6 but internal API GC.GetGenerationSize is valid)
if (!isCodeRunningOnBuggyRuntimeVersion || getGenerationSize != null)
{
// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.heap.size",
() =>
{
Expand Down Expand Up @@ -124,8 +122,7 @@ static RuntimeMetrics()
// Not valid until .NET 7 where the bug in the API is fixed. See context in https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/496
if (Environment.Version.Major >= 7)
{
// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.gc.heap.fragmentation.size",
() =>
{
Expand Down Expand Up @@ -174,8 +171,7 @@ static RuntimeMetrics()
() => Monitor.LockContentionCount,
description: "The number of times there was contention when trying to acquire a monitor lock since the process start. Monitor locks are commonly acquired by using the lock keyword in C#, or by calling Monitor.Enter() and Monitor.TryEnter().");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.thread_pool.threads.count",
() => (long)ThreadPool.ThreadCount,
description: "The number of thread pool threads that currently exist.");
Expand All @@ -185,21 +181,18 @@ static RuntimeMetrics()
() => ThreadPool.CompletedWorkItemCount,
description: "The number of work items that have been processed by the thread pool since the process start.");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.thread_pool.queue.length",
() => ThreadPool.PendingWorkItemCount,
description: "The number of work items that are currently queued to be processed by the thread pool.");

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.timer.count",
() => Timer.ActiveCount,
description: "The number of timer instances that are currently active. Timers can be created by many sources such as System.Threading.Timer, Task.Delay, or the timeout in a CancellationSource. An active timer is registered to tick at some point in the future and has not yet been canceled.");
#endif

// TODO: change to ObservableUpDownCounter
MeterInstance.CreateObservableGauge(
MeterInstance.CreateObservableUpDownCounter(
"process.runtime.dotnet.assemblies.count",
() => (long)AppDomain.CurrentDomain.GetAssemblies().Length,
description: "The number of .NET assemblies that are currently loaded.");
Expand Down
Loading

0 comments on commit 1810c15

Please sign in to comment.