-
Notifications
You must be signed in to change notification settings - Fork 863
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
Add Observability APIs to the SDK and add telemetry provider reference to AWSConfigs and ClientConfig #3344
Changes from all commits
fbc143f
e2c4114
c31588a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Amazon.Runtime.Telemetry | ||
{ | ||
/// <summary> | ||
/// Represents a collection of attributes used for telemetry purposes. | ||
/// Attributes are key-value pairs where the key is a string and the value is an object. | ||
/// These attributes provide additional information for spans and metrics in telemetry data. | ||
/// </summary> | ||
public class Attributes | ||
{ | ||
private readonly Dictionary<string, object> _attributes; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Attributes"/> class. | ||
/// </summary> | ||
public Attributes() | ||
{ | ||
_attributes = new Dictionary<string, object>(); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Attributes"/> class. | ||
/// If duplicate keys are present in the provided collection, the value of the last occurrence of the key will be used. | ||
/// </summary> | ||
/// <param name="attributes">An optional initial set of attributes to populate the collection. | ||
/// If duplicate keys are found, the last value will override previous ones.</param> | ||
public Attributes(IEnumerable<KeyValuePair<string, object>> attributes) | ||
{ | ||
_attributes = new Dictionary<string, object>(); | ||
|
||
foreach (var kvp in attributes) | ||
{ | ||
_attributes[kvp.Key] = kvp.Value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Sets the value for the given attribute key. | ||
/// If the key already exists, the value is updated. | ||
/// </summary> | ||
/// <param name="key">The key of the attribute.</param> | ||
/// <param name="value">The value of the attribute.</param> | ||
public void Set(string key, object value) | ||
{ | ||
_attributes[key] = value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the value for the given attribute key. | ||
/// </summary> | ||
/// <param name="key">The attribute key.</param> | ||
/// <returns>The attribute value, or null if the key does not exist.</returns> | ||
public object Get(string key) | ||
{ | ||
_attributes.TryGetValue(key, out var value); | ||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Removes the attribute with the specified key. | ||
/// </summary> | ||
/// <param name="key">The attribute key.</param> | ||
/// <returns>True if the attribute was successfully removed; otherwise, false.</returns> | ||
public bool Remove(string key) | ||
{ | ||
return _attributes.Remove(key); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the collection of all the attributes. | ||
/// </summary> | ||
public IEnumerable<KeyValuePair<string, object>> AllAttributes | ||
{ | ||
get { return _attributes; } | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.Runtime.Telemetry.Metrics; | ||
using Amazon.Runtime.Telemetry.Metrics.NoOp; | ||
using Amazon.Runtime.Telemetry.Tracing; | ||
using Amazon.Runtime.Telemetry.Tracing.NoOp; | ||
|
||
namespace Amazon.Runtime.Telemetry | ||
{ | ||
/// <summary> | ||
/// Default implementation of <see cref="TelemetryProvider"/> that uses no-op providers by default. | ||
/// </summary> | ||
public class DefaultTelemetryProvider : TelemetryProvider | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultTelemetryProvider"/> class with no-op providers. | ||
/// This setup prevents any telemetry actions from being performed unless explicitly registered using the | ||
/// registration methods. | ||
/// </summary> | ||
public DefaultTelemetryProvider() | ||
{ | ||
TracerProvider = new NoOpTracerProvider(); | ||
normj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MeterProvider = new NoOpMeterProvider(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="TracerProvider"/> used to create new tracers. | ||
/// This property is initialized with a no-op implementation by default. | ||
/// </summary> | ||
public override TracerProvider TracerProvider { get; protected set; } | ||
|
||
/// <summary> | ||
/// Gets the <see cref="MeterProvider"/> used to create new metrics. | ||
/// This property is initialized with a no-op implementation by default. | ||
/// </summary> | ||
public override MeterProvider MeterProvider { get; protected set; } | ||
|
||
/// <summary> | ||
/// Registers a new <see cref="TracerProvider"/>. | ||
/// This method sets a custom tracer provider to replace the default no-op provider, | ||
/// enabling the collection of tracing data. | ||
/// </summary> | ||
/// <param name="tracerProvider">The tracer provider to register.</param> | ||
public override void RegisterTracerProvider(TracerProvider tracerProvider) | ||
{ | ||
TracerProvider = tracerProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Registers a new <see cref="MeterProvider"/>. | ||
/// This method sets a custom meter provider to replace the default no-op provider, | ||
/// enabling the collection of metrics data. | ||
/// </summary> | ||
/// <param name="meterProvider">The meter provider to register.</param> | ||
public override void RegisterMeterProvider(MeterProvider meterProvider) | ||
{ | ||
MeterProvider = meterProvider; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
namespace Amazon.Runtime.Telemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// Represents an async measurement. | ||
/// An async measurement records values, such as periodically or based on events. | ||
/// The generic type parameter T specifies the type of value being recorded, such as int, long, etc. | ||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you intend these lines to show up with newlines in intelliSense? They won't without . This same comment applies to other /// comments in this review There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me a while to understand This is how it looks without And here after adding I don't think it looks this bad without the newlines, but I'm open to change it if you think it is better this way, I've seen both ways done in our SDK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it looks fine/better without the |
||
/// </summary> | ||
/// <typeparam name="T">The type of value being measured.</typeparam> | ||
public abstract class AsyncMeasurement<T> where T : struct | ||
{ | ||
/// <summary> | ||
/// Records a value. | ||
/// This method is called to record a measurement value. | ||
/// </summary> | ||
/// <param name="value">The value to be recorded. This value is of type T.</param> | ||
/// <param name="attributes">Optional attributes associated with the measurement.</param> | ||
public abstract void Record(T value, Attributes attributes = null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
namespace Amazon.Runtime.Telemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// Represents a handle for async measurements. | ||
/// The handle is used to manage and stop the async recording of measurements. | ||
/// </summary> | ||
/// <typeparam name="T">The type of value being measured.</typeparam> | ||
public abstract class AsyncMeasurementHandle<T> where T : struct | ||
{ | ||
/// <summary> | ||
/// Gets the meter that created this handle. | ||
/// </summary> | ||
public Meter Meter { get; protected set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AsyncMeasurementHandle{T}"/> class. | ||
/// </summary> | ||
/// <param name="meter">The meter that created this handle.</param> | ||
protected AsyncMeasurementHandle(Meter meter) | ||
{ | ||
Meter = meter; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
namespace Amazon.Runtime.Telemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// Abstract class representing a histogram used to record values of a specified type. | ||
/// A histogram is a type of metric instrument that tracks the distribution of values. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the histogram value. Must be a struct.</typeparam> | ||
public abstract class Histogram<T> where T : struct | ||
{ | ||
/// <summary> | ||
/// Records a value in the histogram. | ||
/// </summary> | ||
/// <param name="value">The value to record. This represents a measurement or observation.</param> | ||
/// <param name="attributes">Optional attributes associated with the measurement. These can be used to | ||
/// provide additional context or dimensions to the recorded value.</param> | ||
public abstract void Record(T value, Attributes attributes = null); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this allow setting null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update it to remove the existing item if the value is null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not have a
Remove
method? Havingnull
remove within aSet
method is a code smell. It is also less discoverable by a user of this class. Or are you following a spec where this is the expected behavior?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec wasn't very detailed and didn't contain any directions on the Attributes API other than set and get in addition to
may contain additional methods
I was trying to match how it is implemented in the ActivityTagsCollection https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivityTagsCollection.cs#L57, but we don't have to stick to what they have if we think it is a code smell, will update it.