Skip to content

Commit 3fecc41

Browse files
authored
Merge branch 'main' into redis-keys
2 parents 2f9bb74 + 71dcc6b commit 3fecc41

12 files changed

Lines changed: 339 additions & 318 deletions

File tree

src/OpenTelemetry.Api/Internal/OpenTelemetryApiEventSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void ActivityContextExtractException(string format, Exception ex)
4040
[NonEvent]
4141
public void BaggageExtractException(string format, Exception ex)
4242
{
43-
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
43+
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
4444
{
4545
this.FailedToExtractBaggage(format, ex.ToInvariantString());
4646
}
@@ -49,7 +49,7 @@ public void BaggageExtractException(string format, Exception ex)
4949
[NonEvent]
5050
public void TracestateExtractException(Exception ex)
5151
{
52-
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
52+
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
5353
{
5454
this.TracestateExtractError(ex.ToInvariantString());
5555
}
@@ -58,7 +58,7 @@ public void TracestateExtractException(Exception ex)
5858
[NonEvent]
5959
public void TracestateKeyIsInvalid(ReadOnlySpan<char> key)
6060
{
61-
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
61+
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
6262
{
6363
this.TracestateKeyIsInvalid(key.ToString());
6464
}
@@ -67,7 +67,7 @@ public void TracestateKeyIsInvalid(ReadOnlySpan<char> key)
6767
[NonEvent]
6868
public void TracestateValueIsInvalid(ReadOnlySpan<char> value)
6969
{
70-
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
70+
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
7171
{
7272
this.TracestateValueIsInvalid(value.ToString());
7373
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// <copyright file="BaseOtlpExporter.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System;
18+
using System.Threading.Tasks;
19+
using Grpc.Core;
20+
using OpenTelemetry.Exporter.OpenTelemetryProtocol;
21+
#if NETSTANDARD2_1
22+
using Grpc.Net.Client;
23+
#endif
24+
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
25+
using OtlpResource = Opentelemetry.Proto.Resource.V1;
26+
27+
namespace OpenTelemetry.Exporter
28+
{
29+
/// <summary>
30+
/// Implements exporter that exports telemetry objects over OTLP/gRPC.
31+
/// </summary>
32+
/// <typeparam name="T">The type of telemetry object to be exported.</typeparam>
33+
public abstract class BaseOtlpExporter<T> : BaseExporter<T>
34+
where T : class
35+
{
36+
private OtlpResource.Resource processResource;
37+
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="BaseOtlpExporter{T}"/> class.
40+
/// </summary>
41+
/// <param name="options">The <see cref="OtlpExporterOptions"/> for configuring the exporter.</param>
42+
protected BaseOtlpExporter(OtlpExporterOptions options)
43+
{
44+
this.Options = options ?? throw new ArgumentNullException(nameof(options));
45+
this.Headers = options.GetMetadataFromHeaders();
46+
if (this.Options.TimeoutMilliseconds <= 0)
47+
{
48+
throw new ArgumentException("Timeout value provided is not a positive number.", nameof(this.Options.TimeoutMilliseconds));
49+
}
50+
}
51+
52+
internal OtlpResource.Resource ProcessResource => this.processResource ??= this.ParentProvider.GetResource().ToOtlpResource();
53+
54+
#if NETSTANDARD2_1
55+
internal GrpcChannel Channel { get; set; }
56+
#else
57+
internal Channel Channel { get; set; }
58+
#endif
59+
60+
internal OtlpExporterOptions Options { get; }
61+
62+
internal Metadata Headers { get; }
63+
64+
/// <inheritdoc/>
65+
protected override bool OnShutdown(int timeoutMilliseconds)
66+
{
67+
if (this.Channel == null)
68+
{
69+
return true;
70+
}
71+
72+
if (timeoutMilliseconds == -1)
73+
{
74+
this.Channel.ShutdownAsync().Wait();
75+
return true;
76+
}
77+
else
78+
{
79+
return Task.WaitAny(new Task[] { this.Channel.ShutdownAsync(), Task.Delay(timeoutMilliseconds) }) == 0;
80+
}
81+
}
82+
}
83+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// <copyright file="ResourceExtensions.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using OpenTelemetry.Resources;
20+
using OtlpCommon = Opentelemetry.Proto.Common.V1;
21+
using OtlpResource = Opentelemetry.Proto.Resource.V1;
22+
23+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
24+
{
25+
internal static class ResourceExtensions
26+
{
27+
public static OtlpResource.Resource ToOtlpResource(this Resource resource)
28+
{
29+
var processResource = new OtlpResource.Resource();
30+
31+
foreach (KeyValuePair<string, object> attribute in resource.Attributes)
32+
{
33+
var oltpAttribute = attribute.ToOtlpAttribute();
34+
if (oltpAttribute != null)
35+
{
36+
processResource.Attributes.Add(oltpAttribute);
37+
}
38+
}
39+
40+
if (!processResource.Attributes.Any(kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceName))
41+
{
42+
var serviceName = (string)ResourceBuilder.CreateDefault().Build().Attributes.FirstOrDefault(
43+
kvp => kvp.Key == ResourceSemanticConventions.AttributeServiceName).Value;
44+
processResource.Attributes.Add(new OtlpCommon.KeyValue
45+
{
46+
Key = ResourceSemanticConventions.AttributeServiceName,
47+
Value = new OtlpCommon.AnyValue { StringValue = serviceName },
48+
});
49+
}
50+
51+
return processResource;
52+
}
53+
}
54+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// <copyright file="OtlpExporterOptionsGrpcExtensions.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System;
18+
using Grpc.Core;
19+
#if NETSTANDARD2_1
20+
using Grpc.Net.Client;
21+
#endif
22+
23+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
24+
{
25+
internal static class OtlpExporterOptionsGrpcExtensions
26+
{
27+
#if NETSTANDARD2_1
28+
public static GrpcChannel CreateChannel(this OtlpExporterOptions options)
29+
#else
30+
public static Channel CreateChannel(this OtlpExporterOptions options)
31+
#endif
32+
{
33+
if (options.Endpoint.Scheme != Uri.UriSchemeHttp && options.Endpoint.Scheme != Uri.UriSchemeHttps)
34+
{
35+
throw new NotSupportedException($"Endpoint URI scheme ({options.Endpoint.Scheme}) is not supported. Currently only \"http\" and \"https\" are supported.");
36+
}
37+
38+
#if NETSTANDARD2_1
39+
return GrpcChannel.ForAddress(options.Endpoint);
40+
#else
41+
ChannelCredentials channelCredentials;
42+
if (options.Endpoint.Scheme == Uri.UriSchemeHttps)
43+
{
44+
channelCredentials = new SslCredentials();
45+
}
46+
else
47+
{
48+
channelCredentials = ChannelCredentials.Insecure;
49+
}
50+
51+
return new Channel(options.Endpoint.Authority, channelCredentials);
52+
#endif
53+
}
54+
55+
public static Metadata GetMetadataFromHeaders(this OtlpExporterOptions options)
56+
{
57+
var headers = options.Headers;
58+
var metadata = new Metadata();
59+
if (!string.IsNullOrEmpty(headers))
60+
{
61+
Array.ForEach(
62+
headers.Split(','),
63+
(pair) =>
64+
{
65+
// Specify the maximum number of substrings to return to 2
66+
// This treats everything that follows the first `=` in the string as the value to be added for the metadata key
67+
var keyValueData = pair.Split(new char[] { '=' }, 2);
68+
if (keyValueData.Length != 2)
69+
{
70+
throw new ArgumentException("Headers provided in an invalid format.");
71+
}
72+
73+
var key = keyValueData[0].Trim();
74+
var value = keyValueData[1].Trim();
75+
metadata.Add(key, value);
76+
});
77+
}
78+
79+
return metadata;
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)