Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exporter.Stackdriver] Nullable #1130

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ExportInterval.get -> System.TimeSpan
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ExportInterval.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.get -> Google.Apis.Auth.OAuth2.GoogleCredential
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.get -> Google.Apis.Auth.OAuth2.GoogleCredential?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.get -> string
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.get -> string?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.get -> Google.Api.MonitoredResource
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.get -> Google.Api.MonitoredResource?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.get -> string
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.get -> string?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.StackdriverStatsConfiguration() -> void
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string projectId) -> void
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string! projectId) -> void
OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils
OpenTelemetry.Trace.TracerProviderBuilderExtensions
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string projectId) -> Google.Api.MonitoredResource
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
static OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils.Partition<T>(this System.Collections.Generic.IEnumerable<T> source, int size) -> System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>>
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.UseStackdriverExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string projectId) -> OpenTelemetry.Trace.TracerProviderBuilder
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity!> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string? projectId) -> Google.Api.MonitoredResource!
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string?
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration!
static OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils.Partition<T>(this System.Collections.Generic.IEnumerable<T>! source, int size) -> System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>!>!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.UseStackdriverExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string! projectId) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ExportInterval.get -> System.TimeSpan
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ExportInterval.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.get -> Google.Apis.Auth.OAuth2.GoogleCredential
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.get -> Google.Apis.Auth.OAuth2.GoogleCredential?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.GoogleCredential.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.get -> string
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.get -> string?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MetricNamePrefix.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.get -> Google.Api.MonitoredResource
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.get -> Google.Api.MonitoredResource?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.MonitoredResource.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.get -> string
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.get -> string?
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.ProjectId.set -> void
OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.StackdriverStatsConfiguration() -> void
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string projectId) -> void
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string! projectId) -> void
OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils
OpenTelemetry.Trace.TracerProviderBuilderExtensions
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string projectId) -> Google.Api.MonitoredResource
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
static OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils.Partition<T>(this System.Collections.Generic.IEnumerable<T> source, int size) -> System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>>
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.UseStackdriverExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, string projectId) -> OpenTelemetry.Trace.TracerProviderBuilder
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity!> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string? projectId) -> Google.Api.MonitoredResource!
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string?
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration!
static OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils.Partition<T>(this System.Collections.Generic.IEnumerable<T>! source, int size) -> System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<T>!>!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.UseStackdriverExporter(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string! projectId) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -136,25 +134,25 @@ public static AttributeValue ToAttributeValue(this object? av)
switch (av)
{
case string s:
return new AttributeValue()
return new AttributeValue
{
StringValue = new TruncatableString() { Value = s },
StringValue = new TruncatableString { Value = s },
};
case bool b:
return new AttributeValue() { BoolValue = b };
return new AttributeValue { BoolValue = b };
case long l:
return new AttributeValue() { IntValue = l };
return new AttributeValue { IntValue = l };
case double d:
return new AttributeValue()
return new AttributeValue
{
StringValue = new TruncatableString() { Value = d.ToString(CultureInfo.InvariantCulture) },
StringValue = new TruncatableString { Value = d.ToString(CultureInfo.InvariantCulture) },
};
case null:
return new AttributeValue();
default:
return new AttributeValue()
return new AttributeValue
{
StringValue = new TruncatableString() { Value = av.ToString() },
StringValue = new TruncatableString { Value = av.ToString() },
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,13 @@
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry.Exporter.Stackdriver.Implementation;

internal class Constants
{
public const string PackagVersionUndefined = "undefined";

public const string LabelDescription = "OpenTelemetry string";
public const string OpenTelemetryTask = "OpenTelemetry_task";
public const string OpenTelemetryTaskDescription = "OpenTelemetry task identifier";

public const string GcpGkeContainer = "k8s_container";
public const string GcpGceInstance = "gce_instance";
public const string AwsEc2Instance = "aws_ec2_instance";
public const string Global = "global";

public const string ProjectIdLabelKey = "project_id";

public const string GceGcpInstanceType = "cloud.google.com/gce/instance";
public const string GcpInstanceIdKey = "cloud.google.com/gce/instance_id";
public const string GcpAccountIdKey = "cloud.google.com/gce/project_id";
public const string GcpZoneKey = "cloud.google.com/gce/zone";

public const string K8sContainerType = "k8s.io/container";
public const string K8sClusterNameKey = "k8s.io/cluster/name";
public const string K8sContainerNameKey = "k8s.io/container/name";
public const string K8sNamespaceNameKey = "k8s.io/namespace/name";
public const string K8sPodNameKey = "k8s.io/pod/name";

public static readonly string OpenTelemetryTaskValueDefault = GenerateDefaultTaskValue();

private static string GenerateDefaultTaskValue()
{
// Something like '<pid>@<hostname>'
return $"dotnet-{System.Diagnostics.Process.GetCurrentProcess().Id}@{Environment.MachineName}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation;
[EventSource(Name = "OpenTelemetry-Exporter-Stackdriver")]
internal class ExporterStackdriverEventSource : EventSource
{
public static readonly ExporterStackdriverEventSource Log = new ExporterStackdriverEventSource();
public static readonly ExporterStackdriverEventSource Log = new();

[NonEvent]
public void ExportMethodException(Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class GoogleCloudResourceUtils
/// the method returns null.
/// </summary>
/// <returns>Google Cloud Project ID.</returns>
public static string GetProjectId()
public static string? GetProjectId()
{
// Try to detect projectId from the environment where the code is running
var instance = Google.Api.Gax.Platform.Instance();
Expand Down Expand Up @@ -60,7 +60,7 @@ public static string GetProjectId()
/// </summary>
/// <param name="projectId">The project id.</param>
/// <returns>Stackdriver Monitored Resource.</returns>
public static MonitoredResource GetDefaultResource(string projectId)
public static MonitoredResource GetDefaultResource(string? projectId)
{
var resource = new MonitoredResource();
resource.Type = Constants.Global;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public static StackdriverStatsConfiguration Default
/// <summary>
/// Gets or sets the prefix to append to every OpenTelemetry metric name in Stackdriver.
/// </summary>
public string MetricNamePrefix { get; set; }
public string? MetricNamePrefix { get; set; }

/// <summary>
/// Gets or sets google Cloud Project Id.
/// </summary>
public string ProjectId { get; set; }
public string? ProjectId { get; set; }

/// <summary>
/// Gets or sets credential used to authenticate against Google Stackdriver Monitoring APIs.
/// </summary>
public GoogleCredential GoogleCredential { get; set; }
public GoogleCredential? GoogleCredential { get; set; }

/// <summary>
/// Gets or sets monitored Resource associated with metrics collection.
/// By default, the exporter detects the environment where the export is happening,
/// such as GKE/AWS/GCE. If the exporter is running on a different environment,
/// monitored resource will be identified as "general".
/// </summary>
public MonitoredResource MonitoredResource { get; set; }
public MonitoredResource? MonitoredResource { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageTags>$(PackageTags);Stackdriver;Google;GCP;distributed-tracing</PackageTags>
<MinVerTagPrefix>Exporter.Stackdriver-</MinVerTagPrefix>
<IncludeSharedExceptionExtensionsSource>true</IncludeSharedExceptionExtensionsSource>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.Monitoring.V3" Version="[2.6.0,4.0)" />
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Exporter.Stackdriver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constructor for specifying path to the service account credential.
## Installation

```shell
dotnet add package OpenTelemetry.Contrib.Instrumentation.Stackdriver
dotnet add package OpenTelemetry.Contrib.Instrumentation.Stackdriver --prerelease
```

## Traces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class StackdriverTraceExporter : BaseExporter<Activity>

private readonly Google.Api.Gax.ResourceNames.ProjectName googleCloudProjectId;
private readonly TraceServiceSettings traceServiceSettings;
private readonly TraceServiceClient traceServiceClient;
private readonly TraceServiceClient? traceServiceClient;

#pragma warning disable CA1810 // Initialize reference type static fields inline
static StackdriverTraceExporter()
Expand Down Expand Up @@ -91,8 +91,8 @@ internal StackdriverTraceExporter(string projectId, TraceServiceClient traceServ
/// <inheritdoc/>
public override ExportResult Export(in Batch<Activity> batch)
{
TraceServiceClient traceWriter = this.traceServiceClient;
if (this.traceServiceClient == null)
TraceServiceClient? traceWriter = this.traceServiceClient;
if (traceWriter == null)
{
traceWriter = new TraceServiceClientBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ static StackdriverExporterTests()
ActivitySource.AddActivityListener(listener);
}

[Fact]
public void StackdriverExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.UseStackdriverExporter(string.Empty));
}

[Fact]
public void StackdriverExporter_CustomActivityProcessor()
{
Expand Down Expand Up @@ -91,7 +84,7 @@ public void StackdriverExporter_CustomActivityProcessor()
[Fact]
public void StackdriverExporter_TraceClientThrows_ExportResultFailure()
{
Exception exception = null;
Exception? exception;
ExportResult result = ExportResult.Success;
var exportedItems = new List<Activity>();
const string ActivitySourceName = "stackdriver.test";
Expand Down Expand Up @@ -132,7 +125,7 @@ void RunTest(Batch<Activity> batch)
[Fact]
public void StackdriverExporter_TraceClientDoesNotTrow_ExportResultSuccess()
{
Exception exception = null;
Exception? exception;
ExportResult result = ExportResult.Failure;
var exportedItems = new List<Activity>();
const string ActivitySourceName = "stackdriver.test";
Expand Down Expand Up @@ -171,10 +164,10 @@ void RunTest(Batch<Activity> batch)

internal static Activity CreateTestActivity(
bool setAttributes = true,
Dictionary<string, object> additionalAttributes = null,
Dictionary<string, object>? additionalAttributes = null,
bool addEvents = true,
bool addLinks = true,
Resource resource = null,
Resource? resource = null,
ActivityKind kind = ActivityKind.Client)
{
var startTimestamp = DateTime.UtcNow;
Expand All @@ -184,7 +177,7 @@ internal static Activity CreateTestActivity(

var parentSpanId = ActivitySpanId.CreateFromBytes(new byte[] { 12, 23, 34, 45, 56, 67, 78, 89 });

var attributes = new Dictionary<string, object>
var attributes = new Dictionary<string, object?>
{
{ "stringKey", "value" },
{ "longKey", 1L },
Expand All @@ -205,14 +198,14 @@ internal static Activity CreateTestActivity(

var events = new List<ActivityEvent>
{
new ActivityEvent(
new(
"Event1",
eventTimestamp,
new ActivityTagsCollection
{
{ "key", "value" },
}),
new ActivityEvent(
new(
"Event2",
eventTimestamp,
new ActivityTagsCollection
Expand All @@ -226,7 +219,7 @@ internal static Activity CreateTestActivity(
var activitySource = new ActivitySource(nameof(CreateTestActivity));

var tags = setAttributes ?
attributes.Select(kvp => new KeyValuePair<string, object>(kvp.Key, kvp.Value?.ToString()))
attributes.Select(kvp => new KeyValuePair<string, object?>(kvp.Key, kvp.Value?.ToString()))
: null;
var links = addLinks ?
new[]
Expand All @@ -244,7 +237,7 @@ internal static Activity CreateTestActivity(
parentContext: new ActivityContext(traceId, parentSpanId, ActivityTraceFlags.Recorded),
tags,
links,
startTime: startTimestamp);
startTime: startTimestamp)!;

if (addEvents)
{
Expand Down
Loading