-
Notifications
You must be signed in to change notification settings - Fork 151
Introduce ActivityKey to remove string concatenation
#8037
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
Merged
Merged
Changes from all commits
Commits
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
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,50 @@ | ||
| // <copyright file="ActivityKey.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Datadog.Trace.Activity.Handlers; | ||
|
|
||
| internal readonly struct ActivityKey : IEquatable<ActivityKey> | ||
| { | ||
| private readonly string _traceId; | ||
| private readonly string _spanId; | ||
|
|
||
| public ActivityKey(string traceId, string spanId) | ||
| { | ||
| _traceId = traceId; | ||
| _spanId = spanId; | ||
| } | ||
|
|
||
| public ActivityKey(string id) | ||
| { | ||
| // Arbitrary which way around these are | ||
| _spanId = id; | ||
| _traceId = string.Empty; | ||
| } | ||
|
|
||
| public bool IsValid() => _traceId is not null && _spanId is not null; | ||
|
|
||
| public bool Equals(ActivityKey other) => | ||
| string.Equals(_traceId, other._traceId, StringComparison.Ordinal) && | ||
| string.Equals(_spanId, other._spanId, StringComparison.Ordinal); | ||
|
|
||
| public override bool Equals(object? obj) => | ||
| obj is ActivityKey other && Equals(other); | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| // TraceId and SpanId must be non-null, so can just just the values directly. | ||
| // If we allow this to be called with any null values, then we must add null checks here | ||
| unchecked | ||
| { | ||
| return (StringComparer.Ordinal.GetHashCode(_traceId) * 397) | ||
| ^ StringComparer.Ordinal.GetHashCode(_spanId); | ||
| } | ||
| } | ||
| } |
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
73 changes: 73 additions & 0 deletions
73
tracer/test/Datadog.Trace.Tests/Activity/ActivityKeyTests.cs
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,73 @@ | ||
| // <copyright file="ActivityKeyTests.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| using System.Collections.Generic; | ||
| using Datadog.Trace.Activity.Handlers; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Datadog.Trace.Tests.Activity; | ||
|
|
||
| public class ActivityKeyTests | ||
| { | ||
| [Theory] | ||
| [InlineData("spanId", "traceId")] | ||
| [InlineData("id", "")] | ||
| public void Equality(string spanId, string traceId) | ||
| { | ||
| var activity1 = new ActivityKey(traceId: traceId, spanId: spanId); | ||
| var activity2 = new ActivityKey(traceId: traceId, spanId: spanId); | ||
| activity1.Equals(activity2).Should().BeTrue(); | ||
| activity1.GetHashCode().Should().Be(activity2.GetHashCode()); | ||
|
|
||
| var other = new ActivityKey(traceId: traceId, spanId: "newId"); | ||
| activity1.Equals(other).Should().BeFalse(); | ||
| activity1.GetHashCode().Should().NotBe(other.GetHashCode()); | ||
| activity2.Equals(other).Should().BeFalse(); | ||
| activity2.GetHashCode().Should().NotBe(other.GetHashCode()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("spanId", "traceId")] | ||
| [InlineData("id", "")] | ||
| public void Equality_DefaultComparer(string spanId, string traceId) | ||
| { | ||
| var activity1 = new ActivityKey(traceId: traceId, spanId: spanId); | ||
| var activity2 = new ActivityKey(traceId: traceId, spanId: spanId); | ||
|
|
||
| EqualityComparer<ActivityKey>.Default.Equals(activity1, activity2).Should().BeTrue(); | ||
| var hashCode1 = EqualityComparer<ActivityKey>.Default.GetHashCode(activity1).GetHashCode(); | ||
| var hashCode2 = EqualityComparer<ActivityKey>.Default.GetHashCode(activity2); | ||
| hashCode1.Should().Be(hashCode2); | ||
|
|
||
| var other = new ActivityKey(traceId: traceId, spanId: "newId"); | ||
| var hashCodeOther = EqualityComparer<ActivityKey>.Default.GetHashCode(other).GetHashCode(); | ||
| EqualityComparer<ActivityKey>.Default.Equals(activity1, other).Should().BeFalse(); | ||
| hashCode1.Should().NotBe(hashCodeOther); | ||
| EqualityComparer<ActivityKey>.Default.Equals(activity2, other).Should().BeFalse(); | ||
| hashCode2.Should().NotBe(hashCodeOther); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("spanId", "traceId", true)] | ||
| [InlineData("spanId", "", true)] | ||
| [InlineData("", "traceId", true)] | ||
| [InlineData("", "", true)] | ||
| [InlineData("spanId", null, false)] | ||
| [InlineData(null, "traceId", false)] | ||
| public void IsValid_SpanId_TraceId(string traceId, string spanId, bool isValid) | ||
| { | ||
| new ActivityKey(traceId: traceId, spanId: spanId).IsValid().Should().Be(isValid); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("some_id", true)] | ||
| [InlineData("", true)] | ||
| [InlineData(null, false)] | ||
| public void IsValid_SpanId_Id(string id, bool isValid) | ||
| { | ||
| new ActivityKey(id).IsValid().Should().Be(isValid); | ||
| } | ||
| } |
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
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.
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.
I think this is the bug we were hitting with hierarchical IDs:
IW3CActivityis about the Version ofActivity, not the type of ID in use)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.
Ohhh wow nice catch