Skip to content

Commit

Permalink
Merge branch 'main' into feature/enable-successfulcommandtest-by-leve…
Browse files Browse the repository at this point in the history
…raging-testcontainers
  • Loading branch information
alanwest authored Mar 29, 2023
2 parents 6d80187 + 63de729 commit 0aae64c
Show file tree
Hide file tree
Showing 39 changed files with 656 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v7
- uses: actions/stale@v8
with:
stale-pr-message: 'This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day.'
close-pr-message: 'Closed as inactive. Feel free to reopen if this PR is still being worked on.'
Expand Down
20 changes: 20 additions & 0 deletions examples/AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System.Diagnostics.Metrics;
using Examples.AspNetCore;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
Expand All @@ -33,6 +34,9 @@
// Note: Switch between Console/OTLP by setting UseLogExporter in appsettings.json.
var logExporter = appBuilder.Configuration.GetValue<string>("UseLogExporter").ToLowerInvariant();

// Note: Switch between Explicit/Exponential by setting HistogramAggregation in appsettings.json
var histogramAggregation = appBuilder.Configuration.GetValue<string>("HistogramAggregation").ToLowerInvariant();

// Build a resource configuration action to set service information.
Action<ResourceBuilder> configureResource = r => r.AddService(
serviceName: appBuilder.Configuration.GetValue<string>("ServiceName"),
Expand Down Expand Up @@ -111,6 +115,22 @@
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation();

switch (histogramAggregation)
{
case "exponential":
builder.AddView(instrument =>
{
return instrument.GetType().GetGenericTypeDefinition() == typeof(Histogram<>)
? new Base2ExponentialBucketHistogramConfiguration()
: null;
});
break;
default:
// Explicit bounds histogram is the default.
// No additional configuration necessary.
break;
}

switch (metricsExporter)
{
case "prometheus":
Expand Down
1 change: 1 addition & 0 deletions examples/AspNetCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"UseTracingExporter": "console",
"UseMetricsExporter": "console",
"UseLogExporter": "console",
"HistogramAggregation": "explicit",
"Jaeger": {
"AgentHost": "localhost",
"AgentPort": 6831,
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/Internal/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Globalization;

namespace OpenTelemetry.Internal
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/Internal/SemanticConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

namespace OpenTelemetry.Trace
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/Internal/SpanAttributeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

namespace OpenTelemetry.Trace
{
/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions src/OpenTelemetry.Api/Internal/StatusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Runtime.CompilerServices;
using OpenTelemetry.Trace;

Expand All @@ -26,7 +28,7 @@ internal static class StatusHelper
public const string ErrorStatusCodeTagValue = "ERROR";

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetTagValueForStatusCode(StatusCode statusCode)
public static string? GetTagValueForStatusCode(StatusCode statusCode)
{
return statusCode switch
{
Expand All @@ -43,7 +45,7 @@ public static string GetTagValueForStatusCode(StatusCode statusCode)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static StatusCode? GetStatusCodeForTagValue(string statusCodeTagValue)
public static StatusCode? GetStatusCodeForTagValue(string? statusCodeTagValue)
{
return statusCodeTagValue switch
{
Expand All @@ -52,15 +54,15 @@ public static string GetTagValueForStatusCode(StatusCode statusCode)
* first because assumption is most spans will be
* Unset, then Error. Ok is not set by the SDK.
*/
string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
not null when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
not null when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
not null when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
_ => null,
};
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetStatusCodeForTagValue(string statusCodeTagValue, out StatusCode statusCode)
public static bool TryGetStatusCodeForTagValue(string? statusCodeTagValue, out StatusCode statusCode)
{
StatusCode? tempStatusCode = GetStatusCodeForTagValue(statusCodeTagValue);

Expand Down
15 changes: 15 additions & 0 deletions src/OpenTelemetry.Api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,21 @@ and
[extract](../../examples/MicroserviceExample/Utils/Messaging/MessageReceiver.cs)
context.
**Note on instrumentation libraries**: If you are using the instrumentation
libraries shipped from this repo [e.g. [ASP.NET
Core](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.AspNetCore)
or
[HttpClient](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.Http)],
context propagation is done by using the
[default](https://github.com/open-telemetry/opentelemetry-dotnet/blob/d924663dc3d6bfdf737bc49ceaa1addcec90a2d6/src/OpenTelemetry/Sdk.cs#L34-L38)
propagator. The default can be updated by calling
`Sdk.SetDefaultTextMapPropagator` and passing the propagator of your choice.
Propagator Api used by the instrumentation libraries is different than
[DistributedContextPropagator](https://learn.microsoft.com/dotnet/api/system.diagnostics.distributedcontextpropagator)
available in `System.Diagnostics`. Implementing this will have no impact on the
propagation, if used alongside instrumentation libraries.
## Introduction to OpenTelemetry .NET Metrics API
Metrics in OpenTelemetry .NET are a somewhat unique implementation of the
Expand Down
58 changes: 33 additions & 25 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override ExportResult Export(in Batch<Metric> batch)

var metricType = metric.MetricType;

if (metricType.IsHistogram())
if (metricType == MetricType.Histogram || metricType == MetricType.ExponentialHistogram)
{
var bucketsBuilder = new StringBuilder();
var sum = metricPoint.GetHistogramSum();
Expand All @@ -105,41 +105,49 @@ public override ExportResult Export(in Batch<Metric> batch)

bucketsBuilder.AppendLine();

bool isFirstIteration = true;
double previousExplicitBound = default;
foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets())
if (metricType == MetricType.Histogram)
{
if (isFirstIteration)
bool isFirstIteration = true;
double previousExplicitBound = default;
foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets())
{
bucketsBuilder.Append("(-Infinity,");
bucketsBuilder.Append(histogramMeasurement.ExplicitBound);
bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(histogramMeasurement.BucketCount);
previousExplicitBound = histogramMeasurement.ExplicitBound;
isFirstIteration = false;
}
else
{
bucketsBuilder.Append('(');
bucketsBuilder.Append(previousExplicitBound);
bucketsBuilder.Append(',');
if (histogramMeasurement.ExplicitBound != double.PositiveInfinity)
if (isFirstIteration)
{
bucketsBuilder.Append("(-Infinity,");
bucketsBuilder.Append(histogramMeasurement.ExplicitBound);
bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(histogramMeasurement.BucketCount);
previousExplicitBound = histogramMeasurement.ExplicitBound;
isFirstIteration = false;
}
else
{
bucketsBuilder.Append("+Infinity");
bucketsBuilder.Append('(');
bucketsBuilder.Append(previousExplicitBound);
bucketsBuilder.Append(',');
if (histogramMeasurement.ExplicitBound != double.PositiveInfinity)
{
bucketsBuilder.Append(histogramMeasurement.ExplicitBound);
previousExplicitBound = histogramMeasurement.ExplicitBound;
}
else
{
bucketsBuilder.Append("+Infinity");
}

bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(histogramMeasurement.BucketCount);
}

bucketsBuilder.Append(']');
bucketsBuilder.Append(':');
bucketsBuilder.Append(histogramMeasurement.BucketCount);
bucketsBuilder.AppendLine();
}

bucketsBuilder.AppendLine();
}
else
{
// TODO: Consider how/if to display buckets for exponential histograms.
bucketsBuilder.AppendLine("Buckets are not displayed for exponential histograms.");
}

valueDisplay = bucketsBuilder.ToString();
Expand Down
9 changes: 9 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Unreleased

* Add support for exporting histograms aggregated using the
[Base2 Exponential Bucket Histogram Aggregation](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#base2-exponential-bucket-histogram-aggregation).
([#4337](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4337))

* Added support to set `TraceState` when converting the
System.Diagnostics.Activity object to its corresponding
OpenTelemetry.Proto.Trace.V1.Span object.
([#4331](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4331))

## 1.5.0-alpha.1

Released 2023-Mar-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ internal static Span ToOtlpSpan(this Activity activity, SdkLimitOptions sdkLimit
TraceId = UnsafeByteOperations.UnsafeWrap(traceIdBytes),
SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes),
ParentSpanId = parentSpanIdString,
TraceState = activity.TraceStateString ?? string.Empty,

StartTimeUnixNano = (ulong)startTimeUnixNano,
EndTimeUnixNano = (ulong)(startTimeUnixNano + activity.Duration.ToNanoseconds()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,58 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
otlpMetric.Histogram = histogram;
break;
}

case MetricType.ExponentialHistogram:
{
var histogram = new OtlpMetrics.ExponentialHistogram
{
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.ExponentialHistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
};

AddAttributes(metricPoint.Tags, dataPoint.Attributes);
dataPoint.Count = (ulong)metricPoint.GetHistogramCount();
dataPoint.Sum = metricPoint.GetHistogramSum();

if (metricPoint.TryGetHistogramMinMaxValues(out double min, out double max))
{
dataPoint.Min = min;
dataPoint.Max = max;
}

var exponentialHistogramData = metricPoint.GetExponentialHistogramData();
dataPoint.Scale = exponentialHistogramData.Scale;
dataPoint.ZeroCount = (ulong)exponentialHistogramData.ZeroCount;

dataPoint.Positive = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive.Offset = exponentialHistogramData.PositiveBuckets.Offset;
foreach (var bucketCount in exponentialHistogramData.PositiveBuckets)
{
dataPoint.Positive.BucketCounts.Add((ulong)bucketCount);
}

dataPoint.Negative = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Negative.Offset = exponentialHistogramData.NegativeBuckets.Offset;
foreach (var bucketCount in exponentialHistogramData.NegativeBuckets)
{
dataPoint.Negative.BucketCounts.Add((ulong)bucketCount);
}

// TODO: exemplars.

histogram.DataPoints.Add(dataPoint);
}

otlpMetric.ExponentialHistogram = histogram;
break;
}
}

return otlpMetric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ UpDownCounter becomes gauge

public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
{
if (metric.MetricType == MetricType.ExponentialHistogram)
{
// Exponential histograms are not yet support by Prometheus.
// They are ignored for now.
return cursor;
}

int metricType = (int)metric.MetricType >> 4;
cursor = WriteTypeMetadata(buffer, cursor, metric.Name, metric.Unit, MetricTypes[metricType]);
cursor = WriteUnitMetadata(buffer, cursor, metric.Name, metric.Unit);
Expand Down
Loading

0 comments on commit 0aae64c

Please sign in to comment.