Skip to content

Commit

Permalink
Merge branch 'main' into net9prep-persistentstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Oct 29, 2024
2 parents ba96e6e + 637219b commit 60c308b
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ private static IMetricsWriter CreateMetricsWriter(MetricsSchema metricsSchema)
return metricsSchema switch
{
MetricsSchema.TelegrafPrometheusV2 => new TelegrafPrometheusWriterV2(),
MetricsSchema.TelegrafPrometheusV1 => new TelegrafPrometheusWriterV1(),
MetricsSchema.None => new TelegrafPrometheusWriterV1(),
_ => new TelegrafPrometheusWriterV1(),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public InfluxDBMetricsExporter(IMetricsWriter writer, InfluxDBClient influxDbCli
case WriteErrorEvent writeErrorEvent:
InfluxDBEventSource.Log.FailedToExport(writeErrorEvent.Exception.Message);
break;
default:
break;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);

if (dataPoint.TryGetHistogramMinMaxValues(out double min, out double max))
if (dataPoint.TryGetHistogramMinMaxValues(out var min, out var max))
{
pointData = pointData.Field("min", min)
.Field("max", max);
Expand All @@ -142,6 +142,10 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi)
writeApi.WritePoint(pointData);
}

break;
case MetricType.ExponentialHistogram:
break;
default:
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi)
.Field($"{metricName}_count", metricPoint.GetHistogramCount())
.Field($"{metricName}_sum", metricPoint.GetHistogramSum());

if (metricPoint.TryGetHistogramMinMaxValues(out double min, out double max))
if (metricPoint.TryGetHistogramMinMaxValues(out var min, out var max))
{
headPoint = headPoint
.Field($"{metricName}_min", min)
Expand All @@ -149,6 +149,10 @@ public void Write(Metric metric, Resource? resource, WriteApi writeApi)
}
}

break;
case MetricType.ExponentialHistogram:
break;
default:
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityInstrumentationHelper.cs" Link="Includes\ActivityInstrumentationHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\DatabaseSemanticConventionHelper.cs" Link="Includes\DatabaseSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\DiagnosticSourceListener.cs" Link="Includes\DiagnosticSourceListener.cs" />
Expand Down
10 changes: 10 additions & 0 deletions src/Shared/ActivityInstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ internal static class ActivityInstrumentationHelper

private static Action<Activity, ActivitySource> CreateActivitySourceSetter()
{
#if NET
return typeof(Activity).GetProperty("Source")!
.SetMethod!.CreateDelegate<Action<Activity, ActivitySource>>();
#else
return (Action<Activity, ActivitySource>)typeof(Activity).GetProperty("Source")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivitySource>));
#endif
}

private static Action<Activity, ActivityKind> CreateActivityKindSetter()
{
#if NET
return typeof(Activity).GetProperty("Kind")!
.SetMethod!.CreateDelegate<Action<Activity, ActivityKind>>();
#else
return (Action<Activity, ActivityKind>)typeof(Activity).GetProperty("Kind")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivityKind>));
#endif
}
}
4 changes: 2 additions & 2 deletions src/Shared/DatabaseSemanticConventionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace OpenTelemetry.Internal;
internal static class DatabaseSemanticConventionHelper
{
internal const string SemanticConventionOptInKeyName = "OTEL_SEMCONV_STABILITY_OPT_IN";
internal static readonly char[] Separator = new[] { ',', ' ' };
internal static readonly char[] Separator = [',', ' '];

[Flags]
public enum DatabaseSemanticConvention
internal enum DatabaseSemanticConvention
{
/// <summary>
/// Instructs an instrumentation library to emit the old experimental Database attributes.
Expand Down
25 changes: 18 additions & 7 deletions src/Shared/GrpcTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public static bool TryGetGrpcStatusCodeFromActivity(Activity activity, out int s
{
statusCode = -1;
var grpcStatusCodeTag = activity.GetTagValue(GrpcStatusCodeTagName);
if (grpcStatusCodeTag == null)
{
return false;
}

return int.TryParse(grpcStatusCodeTag as string, out statusCode);
return grpcStatusCodeTag != null && int.TryParse(grpcStatusCodeTag as string, out statusCode);
}

public static bool TryParseRpcServiceAndRpcMethod(string grpcMethod, out string rpcService, out string rpcMethod)
Expand Down Expand Up @@ -66,9 +61,25 @@ public static ActivityStatusCode ResolveSpanStatusForGrpcStatusCode(int statusCo

if (typeof(GrpcStatusCanonicalCode).IsEnumDefined(statusCode))
{
status = ((GrpcStatusCanonicalCode)statusCode) switch
status = (GrpcStatusCanonicalCode)statusCode switch
{
GrpcStatusCanonicalCode.Ok => ActivityStatusCode.Unset,
GrpcStatusCanonicalCode.Cancelled => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unknown => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.InvalidArgument => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.DeadlineExceeded => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.NotFound => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.AlreadyExists => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.PermissionDenied => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.ResourceExhausted => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.FailedPrecondition => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Aborted => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.OutOfRange => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unimplemented => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Internal => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unavailable => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.DataLoss => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unauthenticated => ActivityStatusCode.Error,
_ => ActivityStatusCode.Error,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/NullableAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal sealed class MemberNotNullAttribute : Attribute
/// <param name="member">
/// The field or property member that is promised to be not-null.
/// </param>
public MemberNotNullAttribute(string member) => Members = new[] { member };
public MemberNotNullAttribute(string member) => Members = [member];

/// <summary>Initializes the attribute with the list of field and property members.</summary>
/// <param name="members">
Expand Down
18 changes: 6 additions & 12 deletions src/Shared/PropertyFetcher.AOT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public bool TryFetch(
out T? value)
{
var innerFetcher = this.innerFetcher;
if (innerFetcher is null)
{
return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value);
}

return innerFetcher.TryFetch(obj, out value);
return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value);
}

#if NET
Expand Down Expand Up @@ -189,7 +184,11 @@ private sealed class PropertyFetchInstantiated<TDeclaredObject> : PropertyFetch
public PropertyFetchInstantiated(PropertyInfo property)
{
this.propertyName = property.Name;
#if NET
this.propertyFetch = property.GetMethod!.CreateDelegate<Func<TDeclaredObject, T>>();
#else
this.propertyFetch = (Func<TDeclaredObject, T>)property.GetMethod!.CreateDelegate(typeof(Func<TDeclaredObject, T>));
#endif
}

public override int NumberOfInnerFetchers => this.innerFetcher == null
Expand All @@ -210,12 +209,7 @@ public override bool TryFetch(
}

var innerFetcher = this.innerFetcher;
if (innerFetcher is null)
{
return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value);
}

return innerFetcher.TryFetch(obj, out value);
return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/RequestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class RequestDataHelper
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes
private const string OtherHttpMethod = "_OTHER";

private static readonly char[] SplitChars = new[] { ',' };
private static readonly char[] SplitChars = [','];

#if NET
private readonly FrozenDictionary<string, string> knownHttpMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ namespace OpenTelemetry.Internal.Tests;

public class DatabaseSemanticConventionHelperTests
{
public static IEnumerable<object[]> TestCases => new List<object[]>
{
new object[] { null!, DatabaseSemanticConvention.Old },
new object[] { string.Empty, DatabaseSemanticConvention.Old },
new object[] { " ", DatabaseSemanticConvention.Old },
new object[] { "junk", DatabaseSemanticConvention.Old },
new object[] { "none", DatabaseSemanticConvention.Old },
new object[] { "NONE", DatabaseSemanticConvention.Old },
new object[] { "database", DatabaseSemanticConvention.New },
new object[] { "DATABASE", DatabaseSemanticConvention.New },
new object[] { "database/dup", DatabaseSemanticConvention.Dupe },
new object[] { "DATABASE/DUP", DatabaseSemanticConvention.Dupe },
new object[] { "junk,,junk", DatabaseSemanticConvention.Old },
new object[] { "junk,JUNK", DatabaseSemanticConvention.Old },
new object[] { "junk1,junk2", DatabaseSemanticConvention.Old },
new object[] { "junk,database", DatabaseSemanticConvention.New },
new object[] { "junk,database , database ,junk", DatabaseSemanticConvention.New },
new object[] { "junk,database/dup", DatabaseSemanticConvention.Dupe },
new object[] { "junk, database/dup ", DatabaseSemanticConvention.Dupe },
new object[] { "database/dup,database", DatabaseSemanticConvention.Dupe },
new object[] { "database,database/dup", DatabaseSemanticConvention.Dupe },
};
public static IEnumerable<object[]> TestCases =>
[
[null!, DatabaseSemanticConvention.Old],
[string.Empty, DatabaseSemanticConvention.Old],
[" ", DatabaseSemanticConvention.Old],
["junk", DatabaseSemanticConvention.Old],
["none", DatabaseSemanticConvention.Old],
["NONE", DatabaseSemanticConvention.Old],
["database", DatabaseSemanticConvention.New],
["DATABASE", DatabaseSemanticConvention.New],
["database/dup", DatabaseSemanticConvention.Dupe],
["DATABASE/DUP", DatabaseSemanticConvention.Dupe],
["junk,,junk", DatabaseSemanticConvention.Old],
["junk,JUNK", DatabaseSemanticConvention.Old],
["junk1,junk2", DatabaseSemanticConvention.Old],
["junk,database", DatabaseSemanticConvention.New],
["junk,database , database ,junk", DatabaseSemanticConvention.New],
["junk,database/dup", DatabaseSemanticConvention.Dupe],
["junk, database/dup ", DatabaseSemanticConvention.Dupe],
["database/dup,database", DatabaseSemanticConvention.Dupe],
["database,database/dup", DatabaseSemanticConvention.Dupe],
];

[Fact]
public void VerifyFlags()
Expand All @@ -56,7 +56,11 @@ public void VerifyGetSemanticConventionOptIn_UsingEnvironmentVariable(string inp
{
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, input);

#if NET
var expected = Enum.Parse<DatabaseSemanticConvention>(expectedValue);
#else
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue);
#endif
Assert.Equal(expected, GetSemanticConventionOptIn(new ConfigurationBuilder().AddEnvironmentVariables().Build()));
}
finally
Expand All @@ -73,7 +77,11 @@ public void VerifyGetSemanticConventionOptIn_UsingIConfiguration(string input, s
.AddInMemoryCollection(new Dictionary<string, string?> { [SemanticConventionOptInKeyName] = input })
.Build();

#if NET
var expected = Enum.Parse<DatabaseSemanticConvention>(expectedValue);
#else
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue);
#endif
Assert.Equal(expected, GetSemanticConventionOptIn(configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ namespace OpenTelemetry.Internal.Tests;
public class RequestDataHelperTests : IDisposable
{
public static IEnumerable<object[]> MappingVersionProtocolToVersionData =>
new List<object[]>
{
new object[] { new Version(1, 0), "1.0" },
new object[] { new Version(1, 1), "1.1" },
new object[] { new Version(2, 0), "2" },
new object[] { new Version(3, 0), "3" },
new object[] { new Version(7, 6, 5), "7.6.5" },
};
[
[new Version(1, 0), "1.0"],
[new Version(1, 1), "1.1"],
[new Version(2, 0), "2"],
[new Version(3, 0), "3"],
[new Version(7, 6, 5), "7.6.5"],
];

[Theory]
[InlineData("GET", "GET")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ namespace OpenTelemetry.Exporter.InfluxDB.Tests;
public class InfluxDBMetricsExporterTests
{
private static readonly string OpenTelemetrySdkVersion;
private static readonly double[] TestBoundaries = new[] { 10D, 20D, 100D, 200D };
private static readonly double[] TestBoundaries = [10D, 20D, 100D, 200D];

#pragma warning disable CA1810 // Initialize reference type static fields inline
static InfluxDBMetricsExporterTests()
#pragma warning restore CA1810 // Initialize reference type static fields inline
{
var sdkVersion = typeof(Sdk).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version;
if (sdkVersion != null)
{
OpenTelemetrySdkVersion = Version.Parse(sdkVersion).ToString(3);
}
else
{
OpenTelemetrySdkVersion = "0.0.0";
}
OpenTelemetrySdkVersion = sdkVersion != null ? Version.Parse(sdkVersion).ToString(3) : "0.0.0";
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils;

public static class AssertUtils
internal static class AssertUtils
{
public static void HasField<TKey, TValue>(TKey expectedKey, TValue expectedValue, IReadOnlyDictionary<TKey, object> collection)
where TKey : notnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils;

public class InfluxDBFakeServer : IDisposable
internal class InfluxDBFakeServer : IDisposable
{
private static readonly char[] SplitChars = Environment.NewLine.ToCharArray();
private readonly IDisposable httpServer;
private readonly BlockingCollection<string> lines;

public InfluxDBFakeServer()
{
this.lines = new BlockingCollection<string>();
this.lines = [];
this.httpServer = TestHttpServer.RunServer(
context =>
{
byte[] buffer = new byte[context.Request.ContentLength64];
var buffer = new byte[context.Request.ContentLength64];
_ = context.Request.InputStream.Read(buffer, 0, buffer.Length);
string text = Encoding.UTF8.GetString(buffer);
var text = Encoding.UTF8.GetString(buffer);
foreach (var line in text.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries))
{
this.lines.Add(line);
Expand All @@ -45,11 +45,9 @@ public void Dispose()

public PointData ReadPoint()
{
if (this.lines.TryTake(out var line, TimeSpan.FromSeconds(5)))
{
return LineProtocolParser.ParseLine(line);
}

throw new InvalidOperationException("Failed to read a data point from the InfluxDB server within the 5-second timeout.");
return this.lines.TryTake(out var line, TimeSpan.FromSeconds(5))
? LineProtocolParser.ParseLine(line)
: throw new InvalidOperationException(
"Failed to read a data point from the InfluxDB server within the 5-second timeout.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace OpenTelemetry.Exporter.InfluxDB.Tests.Utils;

public static class InfluxDBMetricsExporterOptionsTestExtensions
internal static class InfluxDBMetricsExporterOptionsTestExtensions
{
public static void WithDefaultTestConfiguration(this InfluxDBMetricsExporterOptions options)
{
options.Bucket = "MyBucket";
options.Org = "MyOrg";
options.Token = "MyToken";

// For tests we want to flush the metrics ASAP
// For tests, we want to flush the metrics ASAP
options.FlushInterval = 1;
}
}
Loading

0 comments on commit 60c308b

Please sign in to comment.