Skip to content

Commit

Permalink
Merge branch 'main' into 2361
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Mar 18, 2022
2 parents 83af046 + bdcf942 commit cfb8773
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 37 deletions.
4 changes: 4 additions & 0 deletions docs/logs/customizing-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.Logging;

using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

public class Program
{
Expand All @@ -26,6 +27,9 @@ public static void Main()
{
builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
options.AddConsoleExporter();
});
});
Expand Down
35 changes: 34 additions & 1 deletion docs/logs/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,40 @@ TODO

### SetResourceBuilder

TODO
[Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md)
is the immutable representation of the entity producing the telemetry.
If no `Resource` is explicitly configured, the default is to use a resource
indicating this [Telemetry
SDK](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#telemetry-sdk).
The `SetResourceBuilder` method on `OpenTelemetryLoggerOptions` can be used to
set a single `ResourceBuilder`. If `SetResourceBuilder` is called multiple
times, only the last is kept. It is not possible to change the resource builder
*after* creating the `LoggerFactory`.

The snippet below shows configuring a custom `ResourceBuilder` to the provider.

```csharp
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"
));
});
});
```

See [Program.cs](Program.cs) for complete example.

It is also possible to configure the `Resource` by using following
environmental variables:

| Environment variable | Description |
| -------------------------- | -------------------------------------------------- |
| `OTEL_RESOURCE_ATTRIBUTES` | Key-value pairs to be used as resource attributes. See the [Resource SDK specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable) for more details. |
| `OTEL_SERVICE_NAME` | Sets the value of the `service.name` resource attribute. If `service.name` is also provided in `OTEL_RESOURCE_ATTRIBUTES`, then `OTEL_SERVICE_NAME` takes precedence. |

## Filtering LogLevels

Expand Down
6 changes: 4 additions & 2 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## Unreleased

Added StatusCode, StatusDescription support to
`ConsoleActivityExporter`.
* Added StatusCode, StatusDescription support to
`ConsoleActivityExporter`.
([#2929](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2929)
[#3061](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3061))

## 1.2.0-rc3

Expand Down
19 changes: 12 additions & 7 deletions src/OpenTelemetry.Exporter.Console/ConsoleActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,30 @@ public override ExportResult Export(in Batch<Activity> batch)
continue;
}

var array = tag.Value as Array;

if (array == null)
if (tag.Value is not Array array)
{
this.WriteLine($" {tag.Key}: {tag.Value}");
continue;
}

this.WriteLine($" {tag.Key}: [{string.Join(", ", array.Cast<object>())}]");
}
}

if (!string.IsNullOrEmpty(statusCode))
if (activity.Status != ActivityStatusCode.Unset)
{
this.WriteLine($"StatusCode : {activity.Status}");
if (!string.IsNullOrEmpty(activity.StatusDescription))
{
this.WriteLine($" StatusCode : {statusCode}");
this.WriteLine($"Activity.StatusDescription : {activity.StatusDescription}");
}

}
else if (!string.IsNullOrEmpty(statusCode))
{
this.WriteLine($" StatusCode : {statusCode}");
if (!string.IsNullOrEmpty(statusDesc))
{
this.WriteLine($" StatusDescription : {statusDesc}");
this.WriteLine($" Activity.StatusDescription : {statusDesc}");
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
Fixes issues building on Apple Silicon (M1).
([#2963](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2963))

* Fixed issue where the configuration of an OTLP exporter could be changed
after instantiation by altering the original `OtlpExporterOptions` provided.
([#3066](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3066))

## 1.2.0-rc3

Released 2022-Mar-04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
Expand All @@ -35,20 +36,23 @@ protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)

ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);

this.Options = options;
this.Endpoint = new UriBuilder(options.Endpoint).Uri;
this.Headers = options.GetMetadataFromHeaders();
this.TimeoutMilliseconds = options.TimeoutMilliseconds;
}

internal OtlpExporterOptions Options { get; }

#if NETSTANDARD2_1 || NET5_0_OR_GREATER
internal GrpcChannel Channel { get; set; }
#else
internal Channel Channel { get; set; }
#endif

internal Uri Endpoint { get; }

internal Metadata Headers { get; }

internal int TimeoutMilliseconds { get; }

/// <inheritdoc/>
public abstract bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
Expand All @@ -31,15 +32,15 @@ protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpC
Guard.ThrowIfNull(httpClient);
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);

this.Options = options;
this.Endpoint = new UriBuilder(options.Endpoint).Uri;
this.Headers = options.GetHeaders<Dictionary<string, string>>((d, k, v) => d.Add(k, v));
this.HttpClient = httpClient;
}

internal OtlpExporterOptions Options { get; }

internal HttpClient HttpClient { get; }

internal Uri Endpoint { get; set; }

internal IReadOnlyDictionary<string, string> Headers { get; }

/// <inheritdoc/>
Expand All @@ -55,7 +56,7 @@ public bool SendExportRequest(TRequest request, CancellationToken cancellationTo
}
catch (HttpRequestException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Options.Endpoint, ex);
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public OtlpGrpcLogExportClient(OtlpExporterOptions options, OtlpCollector.LogsSe
/// <inheritdoc/>
public override bool SendExportRequest(OtlpCollector.ExportLogsServiceRequest request, CancellationToken cancellationToken = default)
{
var deadline = DateTime.UtcNow.AddMilliseconds(this.Options.TimeoutMilliseconds);
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);

try
{
this.logsClient.Export(request, headers: this.Headers, deadline: deadline);
}
catch (RpcException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Options.Endpoint, ex);
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public OtlpGrpcMetricsExportClient(OtlpExporterOptions options, OtlpCollector.Me
/// <inheritdoc/>
public override bool SendExportRequest(OtlpCollector.ExportMetricsServiceRequest request, CancellationToken cancellationToken = default)
{
var deadline = DateTime.UtcNow.AddMilliseconds(this.Options.TimeoutMilliseconds);
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);

try
{
this.metricsClient.Export(request, headers: this.Headers, deadline: deadline);
}
catch (RpcException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Options.Endpoint, ex);
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public OtlpGrpcTraceExportClient(OtlpExporterOptions options, OtlpCollector.Trac
/// <inheritdoc/>
public override bool SendExportRequest(OtlpCollector.ExportTraceServiceRequest request, CancellationToken cancellationToken = default)
{
var deadline = DateTime.UtcNow.AddMilliseconds(this.Options.TimeoutMilliseconds);
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);

try
{
this.traceClient.Export(request, headers: this.Headers, deadline: deadline);
}
catch (RpcException ex)
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Options.Endpoint, ex);
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class OtlpHttpMetricsExportClient : BaseOtlpHttpExportClient<Otl
public OtlpHttpMetricsExportClient(OtlpExporterOptions options, HttpClient httpClient)
: base(options, httpClient)
{
this.exportMetricsUri = this.Options.Endpoint;
this.exportMetricsUri = this.Endpoint;
}

protected override HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportMetricsServiceRequest exportRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class OtlpHttpTraceExportClient : BaseOtlpHttpExportClient<OtlpC
public OtlpHttpTraceExportClient(OtlpExporterOptions options, HttpClient httpClient)
: base(options, httpClient)
{
this.exportTracesUri = this.Options.Endpoint;
this.exportTracesUri = this.Endpoint;
}

protected override HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportTraceServiceRequest exportRequest)
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
to `-1`.
([#3038](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3038))

* Marked members of the `MetricPoint` `struct` which do not mutate state as
`readonly`
([#3065](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3065))

## 1.2.0-rc3

Released 2022-Mar-04
Expand Down
24 changes: 12 additions & 12 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal MetricPoint(
/// <summary>
/// Gets the tags associated with the metric point.
/// </summary>
public ReadOnlyTagCollection Tags
public readonly ReadOnlyTagCollection Tags
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
Expand All @@ -86,7 +86,7 @@ public ReadOnlyTagCollection Tags
public DateTimeOffset StartTime
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
readonly get;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set;
Expand All @@ -98,7 +98,7 @@ public DateTimeOffset StartTime
public DateTimeOffset EndTime
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
readonly get;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set;
Expand All @@ -107,7 +107,7 @@ public DateTimeOffset EndTime
internal MetricPointStatus MetricPointStatus
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
readonly get;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private set;
Expand All @@ -121,7 +121,7 @@ internal MetricPointStatus MetricPointStatus
/// </remarks>
/// <returns>Long sum value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long GetSumLong()
public readonly long GetSumLong()
{
if (this.aggType != AggregationType.LongSumIncomingDelta && this.aggType != AggregationType.LongSumIncomingCumulative)
{
Expand All @@ -139,7 +139,7 @@ public long GetSumLong()
/// </remarks>
/// <returns>Double sum value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double GetSumDouble()
public readonly double GetSumDouble()
{
if (this.aggType != AggregationType.DoubleSumIncomingDelta && this.aggType != AggregationType.DoubleSumIncomingCumulative)
{
Expand All @@ -157,7 +157,7 @@ public double GetSumDouble()
/// </remarks>
/// <returns>Long gauge value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long GetGaugeLastValueLong()
public readonly long GetGaugeLastValueLong()
{
if (this.aggType != AggregationType.LongGauge)
{
Expand All @@ -175,7 +175,7 @@ public long GetGaugeLastValueLong()
/// </remarks>
/// <returns>Double gauge value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double GetGaugeLastValueDouble()
public readonly double GetGaugeLastValueDouble()
{
if (this.aggType != AggregationType.DoubleGauge)
{
Expand All @@ -193,7 +193,7 @@ public double GetGaugeLastValueDouble()
/// </remarks>
/// <returns>Count value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long GetHistogramCount()
public readonly long GetHistogramCount()
{
if (this.aggType != AggregationType.Histogram && this.aggType != AggregationType.HistogramSumCount)
{
Expand All @@ -211,7 +211,7 @@ public long GetHistogramCount()
/// </remarks>
/// <returns>Sum value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double GetHistogramSum()
public readonly double GetHistogramSum()
{
if (this.aggType != AggregationType.Histogram && this.aggType != AggregationType.HistogramSumCount)
{
Expand All @@ -229,7 +229,7 @@ public double GetHistogramSum()
/// </remarks>
/// <returns><see cref="HistogramBuckets"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HistogramBuckets GetHistogramBuckets()
public readonly HistogramBuckets GetHistogramBuckets()
{
if (this.aggType != AggregationType.Histogram && this.aggType != AggregationType.HistogramSumCount)
{
Expand Down Expand Up @@ -542,7 +542,7 @@ internal void TakeSnapshot(bool outputDelta)
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void ThrowNotSupportedMetricTypeException(string methodName)
private readonly void ThrowNotSupportedMetricTypeException(string methodName)
{
throw new NotSupportedException($"{methodName} is not supported for this metric type.");
}
Expand Down

0 comments on commit cfb8773

Please sign in to comment.