Skip to content

Commit

Permalink
Add scope property to UserDetails (#2706)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpi authored Apr 21, 2022
1 parent 5df80e1 commit f74af6a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tracer/src/Datadog.Trace/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public static void SetUser(this ISpan span, UserDetails userDetails)
{
localRootSpan.SetTag(Tags.User.Role, userDetails.Role);
}

if (userDetails.Scope is not null)
{
localRootSpan.SetTag(Tags.User.Scope, userDetails.Scope);
}
}
}
}
1 change: 1 addition & 0 deletions tracer/src/Datadog.Trace/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ internal static class User
internal const string Id = "usr.id";
internal const string SessionId = "usr.session_id";
internal const string Role = "usr.role";
internal const string Scope = "usr.scope";
}
}
}
6 changes: 6 additions & 0 deletions tracer/src/Datadog.Trace/UserDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public UserDetails(string id)
Name = null;
SessionId = null;
Role = null;
Scope = null;
}

/// <summary>
Expand All @@ -55,5 +56,10 @@ public UserDetails(string id)
/// Gets or sets the role associated with the user
/// </summary>
public string? Role { get; set; }

/// <summary>
/// Gets or sets the scopes or granted authorities the client currently possesses extracted from token or application security context
/// </summary>
public string? Scope { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ namespace Datadog.Trace
public string Id { get; set; }
public string? Name { get; set; }
public string? Role { get; set; }
public string? Scope { get; set; }
public string? SessionId { get; set; }
}
}
Expand Down
9 changes: 9 additions & 0 deletions tracer/test/Datadog.Trace.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public void SetUserOnRootSpanDirectly()
var id = Guid.NewGuid().ToString();
var sessionId = Guid.NewGuid().ToString();
var role = "admin";
var scope = "read:message, write:files";

var userDetails = new UserDetails()
{
Expand All @@ -554,6 +555,7 @@ public void SetUserOnRootSpanDirectly()
Id = id,
SessionId = sessionId,
Role = role,
Scope = scope,
};
tracer.ActiveScope?.Span.SetUser(userDetails);

Expand All @@ -562,6 +564,7 @@ public void SetUserOnRootSpanDirectly()
Assert.Equal(id, rootTestScope.Span.GetTag(Tags.User.Id));
Assert.Equal(sessionId, rootTestScope.Span.GetTag(Tags.User.SessionId));
Assert.Equal(role, rootTestScope.Span.GetTag(Tags.User.Role));
Assert.Equal(scope, rootTestScope.Span.GetTag(Tags.User.Scope));
}

[Fact]
Expand All @@ -583,6 +586,7 @@ public void SetUserOnChildChildSpan_ShouldAttachToRoot()
var id = Guid.NewGuid().ToString();
var sessionId = Guid.NewGuid().ToString();
var role = "admin";
var scope = "read:message, write:files";

var userDetails = new UserDetails()
{
Expand All @@ -591,6 +595,7 @@ public void SetUserOnChildChildSpan_ShouldAttachToRoot()
Id = id,
SessionId = sessionId,
Role = role,
Scope = scope,
};
tracer.ActiveScope?.Span.SetUser(userDetails);

Expand All @@ -601,6 +606,7 @@ public void SetUserOnChildChildSpan_ShouldAttachToRoot()
Assert.Equal(id, rootTestScope.Span.GetTag(Tags.User.Id));
Assert.Equal(sessionId, rootTestScope.Span.GetTag(Tags.User.SessionId));
Assert.Equal(role, rootTestScope.Span.GetTag(Tags.User.Role));
Assert.Equal(scope, rootTestScope.Span.GetTag(Tags.User.Scope));
}

[Fact]
Expand All @@ -613,6 +619,7 @@ public void SetUser_ShouldWorkOnAnythingImplementingISpan()
var id = Guid.NewGuid().ToString();
var sessionId = Guid.NewGuid().ToString();
var role = "admin";
var scope = "read:message, write:files";

var userDetails = new UserDetails()
{
Expand All @@ -621,6 +628,7 @@ public void SetUser_ShouldWorkOnAnythingImplementingISpan()
Id = id,
SessionId = sessionId,
Role = role,
Scope = scope,
};
testSpan.SetUser(userDetails);

Expand All @@ -629,6 +637,7 @@ public void SetUser_ShouldWorkOnAnythingImplementingISpan()
Assert.Equal(id, testSpan.GetTag(Tags.User.Id));
Assert.Equal(sessionId, testSpan.GetTag(Tags.User.SessionId));
Assert.Equal(role, testSpan.GetTag(Tags.User.Role));
Assert.Equal(scope, testSpan.GetTag(Tags.User.Scope));
}

[Fact]
Expand Down

0 comments on commit f74af6a

Please sign in to comment.