Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using HotChocolate.Language;
using HotChocolate.Language.Utilities;
using static HotChocolate.Diagnostics.SemanticConventions;
using static HotChocolate.WellKnownContextData;

namespace HotChocolate.Diagnostics;

Expand All @@ -19,6 +18,7 @@ internal sealed class ExecuteHttpRequestSpan(
ActivitySource source,
HttpContext httpContext,
HttpRequestKind kind,
string schemaName,
ActivityEnricherBase enricher,
InstrumentationOptionsBase options)
{
Expand Down Expand Up @@ -52,13 +52,6 @@ internal sealed class ExecuteHttpRequestSpan(
}

activity.SetTag(GraphQL.Http.Kind, kind.ToString());

if (!(httpContext.Items.TryGetValue(SchemaName, out var value)
&& value is string schemaName))
{
schemaName = ISchemaDefinition.DefaultName;
}

activity.SetTag(GraphQL.Schema.Name, schemaName);

return new ExecuteHttpRequestSpan(activity, httpContext, kind, enricher, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static IRequestExecutorBuilder AddInstrumentation(

builder.AddDiagnosticEventListener(
sp => new ActivityServerDiagnosticListener(
builder.Name,
sp.GetService<ActivityEnricher>() ??
sp.GetRequiredService<InternalActivityEnricher>(),
sp.GetRequiredService<InstrumentationOptions>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace HotChocolate.Diagnostics.Listeners;

internal sealed class ActivityServerDiagnosticListener(
string schemaName,
ActivityEnricher enricher,
InstrumentationOptions options)
: ServerDiagnosticEventListener
Expand All @@ -20,7 +21,13 @@ public override IDisposable ExecuteHttpRequest(HttpContext context, HttpRequestK
return EmptyScope;
}

var span = ExecuteHttpRequestSpan.Start(Source, context, kind, enricher, options);
var span = ExecuteHttpRequestSpan.Start(
Source,
context,
kind,
schemaName,
enricher,
options);

if (span is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,33 @@ query GetHero {
}
}

[Fact]
public async Task ExecuteHttpRequest_Should_TagSchemaName_When_SchemaIsNamed()
{
using (CaptureActivities(out var activities))
{
// arrange
using var server = CreateStarWarsServer(
configureServices: services => services
.AddGraphQLServer("StarWars")
.AddInstrumentation());
using var client = GraphQLHttpClient.Create(server.CreateClient());

// act
using var result = await client.PostAsync(
new OperationRequest("{ __typename }"),
new Uri("http://localhost:5000/starwars"),
TestContext.Current.CancellationToken);
await result.ReadAsResultAsync(TestContext.Current.CancellationToken);

// assert
var span = Assert.Single(
activities.Settled,
a => a.OperationName == "ExecuteHttpRequest");
Assert.Equal("StarWars", span.GetTagItem("graphql.schema.name"));
}
}

[Fact]
public async Task Http_Request_Should_Be_Unset_When_Client_Disconnects()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public Capture(
// Spans are exported as they stop, and server-side spans can finish on background
// continuations after the awaited call returns. Wait until no spans are in flight so
// those late spans are included, then read the snapshot.
private IReadOnlyList<Activity> Settled => _settled ??= CollectSettledActivities();
[JsonIgnore]
public IReadOnlyList<Activity> Settled => _settled ??= CollectSettledActivities();

private IReadOnlyList<Activity> CollectSettledActivities()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static IFusionGatewayBuilder AddInstrumentation(

builder.AddDiagnosticEventListener(
sp => new FusionActivityServerDiagnosticListener(
builder.Name,
sp.GetService<FusionActivityEnricher>() ??
sp.GetRequiredService<InternalActivityEnricher>(),
sp.GetRequiredService<InstrumentationOptions>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace HotChocolate.Fusion.Diagnostics.Listeners;

internal sealed class FusionActivityServerDiagnosticListener(
string schemaName,
FusionActivityEnricher enricher,
InstrumentationOptions options)
: ServerDiagnosticEventListener
Expand All @@ -21,7 +22,13 @@ public override IDisposable ExecuteHttpRequest(HttpContext context, HttpRequestK
return EmptyScope;
}

var span = ExecuteHttpRequestSpan.Start(Source, context, kind, enricher, options);
var span = ExecuteHttpRequestSpan.Start(
Source,
context,
kind,
schemaName,
enricher,
options);

if (span is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected async Task<Gateway> CreateCompositeSchemaAsync(
Action<IFusionGatewayBuilder>? configureGatewayBuilder = null,
[StringSyntax("json")] string? gatewaySettings = null,
string? environmentName = "Development",
string? gatewayName = null,
bool disableDefaultSecurity = false,
bool enableGlobalObjectIdentification = true,
int? defaultListSize = 1,
Expand All @@ -48,6 +49,7 @@ protected async Task<Gateway> CreateCompositeSchemaAsync(
var sourceSchemas = new List<SourceSchemaText>();
var gatewayServices = new ServiceCollection();
var gatewayBuilder = gatewayServices.AddGraphQLGatewayServer(
gatewayName,
disableDefaultSecurity: disableDefaultSecurity);
var interactions = new ConcurrentDictionary<string, ConcurrentDictionary<int, SourceSchemaInteraction>>();
// Interactions are keyed by an atomically-incremented int, but looked up
Expand Down Expand Up @@ -189,7 +191,7 @@ protected async Task<Gateway> CreateCompositeSchemaAsync(
{
app.UseWebSockets();
app.UseRouting();
app.UseEndpoints(endpoint => endpoint.MapGraphQL());
app.UseEndpoints(endpoint => endpoint.MapGraphQL(schemaName: gatewayName));
};

var gatewayTestServer = _testServerSession.CreateServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public Capture(
// Spans are exported as they stop, and server-side spans can finish on background
// continuations after the awaited call returns. Wait until no spans are in flight so
// those late spans are included, then read the snapshot.
private IReadOnlyList<Activity> Settled => _settled ??= CollectSettledActivities();
[JsonIgnore]
public IReadOnlyList<Activity> Settled => _settled ??= CollectSettledActivities();

private IReadOnlyList<Activity> CollectSettledActivities()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,38 @@ query GetGreeting {
}
}

[Fact]
public async Task ExecuteHttpRequest_Should_TagSchemaName_When_GatewayIsNamed()
{
using (CaptureActivities(out var activities))
{
// arrange
using var server = CreateSourceSchema(
"a",
b => b.AddQueryType<Query>());

using var gateway = await CreateCompositeSchemaAsync(
[("a", server)],
configureGatewayBuilder: b => b.AddInstrumentation(),
gatewayName: "b");

using var client = GraphQLHttpClient.Create(gateway.CreateClient());

// act
using var result = await client.PostAsync(
new OperationRequest("{ sayHello }"),
s_url,
TestContext.Current.CancellationToken);
await result.ReadAsResultAsync(TestContext.Current.CancellationToken);

// assert
var span = Assert.Single(
activities.Settled,
a => a.OperationName == "ExecuteHttpRequest");
Assert.Equal("b", span.GetTagItem("graphql.schema.name"));
}
}

[Fact]
public async Task Http_Request_Should_Be_Unset_When_Client_Disconnects()
{
Expand Down
Loading