Skip to content

Commit

Permalink
Add options for HotChocolate (#13)
Browse files Browse the repository at this point in the history
This is a braking change if you used Enrich delegate.
  • Loading branch information
glucaci authored Sep 24, 2021
1 parent 11d66a1 commit eb299fb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ new MassTransitDiagnosticsSubscriber(o =>
```
The same can be used also for `SendLabel`
## HotChocolate
### Usage
HotChocolate by default is not emitting diagnostic events, but has the infrastructure to instrument each request.
```csharp
public class Startup
Expand All @@ -57,6 +58,19 @@ public class Startup
}
}
```
### Options
Elastic APM Transaction can be enriched by registering a delegate on configure parameter. In this way you can add custom data to the transaction.
```csharp
services
.AddGraphQLServer()
.AddObservability(o =>
o.Enrich = (transaction, operationDetails) =>
{
transaction.SetLabel("GraphQLResult", operationDetails.HasFailed);
transaction.SetLabel("Department", Environment.GetEnvironmentVariable("DEPARTMENT"));
});
```

## Community

This project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ internal class HotChocolateDiagnosticListener : DiagnosticEventListener

private readonly IApmAgent _apmAgent;
private readonly IConfigurationReader _configuration;
private readonly EnrichTransaction? _enrich;
private readonly HotChocolateDiagnosticOptions _options;
private readonly IApmLogger _apmLogger;

internal HotChocolateDiagnosticListener(
IApmAgent apmAgent,
IConfigurationReader configuration,
EnrichTransaction? enrich)
HotChocolateDiagnosticOptions options)
{
_apmAgent = apmAgent;
_configuration = configuration;
_enrich = enrich;
_options = options;
_apmLogger = _apmAgent.Logger;
}

public override IActivityScope ExecuteRequest(IRequestContext context)
{
ITransaction? transaction = _apmAgent.Tracer.CurrentTransaction;
return transaction != null
? new RequestActivityScope(context, transaction, _apmAgent, _enrich)
? new RequestActivityScope(context, transaction, _apmAgent, _options)
: EmptyScope;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Elastic.Apm.Api;

namespace Elastic.Apm.GraphQL.HotChocolate
{
public class HotChocolateDiagnosticOptions
{
/// <summary>
/// Gives the possibility to enrich transaction data just before <see cref="RequestActivityScope"/> gets disposed.
/// </summary>
public Action<ITransaction, OperationDetails>? Enrich { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
using System;
using Elastic.Apm.Api;
using Elastic.Apm.Config;
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Elastic.Apm.GraphQL.HotChocolate
{
/// <summary>
/// Gives the possibility to enrich transaction data just before <see cref="RequestActivityScope"/> gets disposed.
/// </summary>
/// <param name="transaction"></param>
/// <param name="details"></param>
public delegate void EnrichTransaction(ITransaction transaction, OperationDetails details);

/// <summary>
/// Report diagnostic events to Elastic <see cref="IApmAgent"/>
/// </summary>
public static class RequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddObservability(
this IRequestExecutorBuilder builder,
EnrichTransaction? enrich = default)
Action<HotChocolateDiagnosticOptions>? configure = default)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

var options = new HotChocolateDiagnosticOptions();
configure?.Invoke(options);

return builder.AddDiagnosticEventListener(sp =>
{
IApmAgent apmAgent = sp.GetApplicationService<IApmAgent>();
IConfigurationReader configuration = sp.GetApplicationService<IConfigurationReader>();
return new HotChocolateDiagnosticListener(apmAgent, configuration, enrich);
return new HotChocolateDiagnosticListener(apmAgent, configuration, options);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ internal class RequestActivityScope : IActivityScope
private readonly IRequestContext _context;
private readonly ITransaction _transaction;
private readonly IApmAgent _apmAgent;
private readonly EnrichTransaction? _enrich;
private readonly HotChocolateDiagnosticOptions _options;

internal RequestActivityScope(IRequestContext context,
internal RequestActivityScope(
IRequestContext context,
ITransaction transaction,
IApmAgent apmAgent,
EnrichTransaction? enrich)
HotChocolateDiagnosticOptions options)
{
_context = context;
_transaction = transaction;
_apmAgent = apmAgent;
_enrich = enrich;
_options = options;
}

public void Dispose()
Expand All @@ -51,7 +52,7 @@ public void Dispose()
_apmAgent.CaptureException(exception);
}

_enrich?.Invoke(_transaction, operationDetails);
_options.Enrich?.Invoke(_transaction, operationDetails);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elastic.Apm.Messaging.MassTransit
/// </summary>
public class MassTransitDiagnosticsSubscriber : IDiagnosticsSubscriber
{
private MassTransitDiagnosticOptions _options;
private readonly MassTransitDiagnosticOptions _options;

public MassTransitDiagnosticsSubscriber(Action<MassTransitDiagnosticOptions>? configure = default)
{
Expand Down

0 comments on commit eb299fb

Please sign in to comment.