-
-
Notifications
You must be signed in to change notification settings - Fork 229
chore: Refactor SentryAttributes into a separate class #4936
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
Open
jamescrosswell
wants to merge
3
commits into
main
Choose a base branch
from
sentry-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| using Sentry.Extensibility; | ||
|
|
||
| namespace Sentry.Protocol; | ||
|
|
||
| internal class SentryAttributes : Dictionary<string, SentryAttribute>, ISentryJsonSerializable | ||
| { | ||
| public SentryAttributes() : base(StringComparer.Ordinal) | ||
| { | ||
| } | ||
|
|
||
| public SentryAttributes(int capacity) : base(capacity, StringComparer.Ordinal) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the attribute value associated with the specified key. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Returns <see langword="true"/> if this <see cref="SentryMetric"/> contains an attribute with the specified key which is of type <typeparamref name="TAttribute"/> and it's value is not <see langword="null"/>. | ||
| /// Otherwise <see langword="false"/>. | ||
| /// Supported types: | ||
| /// <list type="table"> | ||
| /// <listheader> | ||
| /// <term>Type</term> | ||
| /// <description>Range</description> | ||
| /// </listheader> | ||
| /// <item> | ||
| /// <term>string</term> | ||
| /// <description><see langword="string"/> and <see langword="char"/></description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <term>boolean</term> | ||
| /// <description><see langword="false"/> and <see langword="true"/></description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <term>integer</term> | ||
| /// <description>64-bit signed integral numeric types</description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <term>double</term> | ||
| /// <description>64-bit floating-point numeric types</description> | ||
| /// </item> | ||
| /// </list> | ||
| /// Unsupported types: | ||
| /// <list type="table"> | ||
| /// <listheader> | ||
| /// <term>Type</term> | ||
| /// <description>Result</description> | ||
| /// </listheader> | ||
| /// <item> | ||
| /// <term><see langword="object"/></term> | ||
| /// <description><c>ToString</c> as <c>"type": "string"</c></description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <term>Collections</term> | ||
| /// <description><c>ToString</c> as <c>"type": "string"</c></description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <term><see langword="null"/></term> | ||
| /// <description>ignored</description> | ||
| /// </item> | ||
| /// </list> | ||
| /// </remarks> | ||
| /// <seealso href="https://develop.sentry.dev/sdk/telemetry/metrics/"/> | ||
| public bool TryGetAttribute<TAttribute>(string key, [MaybeNullWhen(false)] out TAttribute value) | ||
| { | ||
| if (TryGetValue(key, out var attribute) && attribute.Value is TAttribute attributeValue) | ||
| { | ||
| value = attributeValue; | ||
| return true; | ||
| } | ||
|
|
||
| value = default; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set a key-value pair of data attached to the metric. | ||
| /// </summary> | ||
| public void SetAttribute<TAttribute>(string key, TAttribute value) where TAttribute : notnull | ||
| { | ||
| if (value is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| this[key] = new SentryAttribute(value); | ||
| } | ||
|
|
||
| internal void SetAttribute(string key, string value) | ||
| { | ||
| this[key] = new SentryAttribute(value, "string"); | ||
| } | ||
|
|
||
| internal void SetAttribute(string key, char value) | ||
| { | ||
| this[key] = new SentryAttribute(value.ToString(), "string"); | ||
| } | ||
|
|
||
| internal void SetAttribute(string key, int value) | ||
| { | ||
| this[key] = new SentryAttribute(value, "integer"); | ||
| } | ||
|
|
||
| internal void SetDefaultAttributes(SentryOptions options, SdkVersion sdk) | ||
| { | ||
| var environment = options.SettingLocator.GetEnvironment(); | ||
| SetAttribute("sentry.environment", environment); | ||
|
|
||
| var release = options.SettingLocator.GetRelease(); | ||
| if (release is not null) | ||
| { | ||
| SetAttribute("sentry.release", release); | ||
| } | ||
|
|
||
| if (sdk.Name is { } name) | ||
| { | ||
| SetAttribute("sentry.sdk.name", name); | ||
| } | ||
| if (sdk.Version is { } version) | ||
| { | ||
| SetAttribute("sentry.sdk.version", version); | ||
| } | ||
| } | ||
|
|
||
| internal void SetAttributes(IEnumerable<KeyValuePair<string, object>>? attributes) | ||
| { | ||
| if (attributes is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
| if (attributes.TryGetNonEnumeratedCount(out var count)) | ||
| { | ||
| _ = EnsureCapacity(Count + count); | ||
| } | ||
| #endif | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| this[attribute.Key] = new SentryAttribute(attribute.Value); | ||
| } | ||
| } | ||
|
|
||
| internal void SetAttributes(ReadOnlySpan<KeyValuePair<string, object>> attributes) | ||
| { | ||
| if (attributes.IsEmpty) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
| _ = EnsureCapacity(Count + attributes.Length); | ||
| #endif | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| this[attribute.Key] = new SentryAttribute(attribute.Value); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc cref="ISentryJsonSerializable.WriteTo(Utf8JsonWriter, IDiagnosticLogger)" /> | ||
| public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger) | ||
| { | ||
| writer.WritePropertyName("attributes"); | ||
| writer.WriteStartObject(); | ||
|
|
||
| foreach (var attribute in this) | ||
| { | ||
| SentryAttributeSerializer.WriteAttribute(writer, attribute.Key, attribute.Value, logger); | ||
| } | ||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Sentry.Testing; | ||
|
|
||
| public static class SentryAttributesExtensions | ||
| { | ||
| internal static void ShouldContain<T>(this SentryAttributes attributes, string key, T expected) | ||
| { | ||
| attributes.TryGetAttribute<T>(key, out var value).Should().BeTrue(); | ||
| value.Should().Be(expected); | ||
| } | ||
|
|
||
| internal static void ShouldNotContain<T>(this SentryAttributes attributes, string key, T expected) | ||
| { | ||
| attributes.TryGetAttribute<T>(key, out var value).Should().BeFalse(); | ||
| value.Should().Be(default(T)); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.