Skip to content

Commit

Permalink
feat: updated examples on using instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhifenglee-aelf committed Jul 16, 2024
1 parent aef4a20 commit c668ea3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ An OpenTelemetry module for use in ABP and Orleans framework.
- [Orleans](#orleans)
- [Examples](#examples)
- [AggregateExecutionTime](#aggregateexecutiontime)
- [Instrumentation](#instrumentation)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -109,6 +110,38 @@ This will automatically measure the execution time of the method of the class an

**Do note that for Controllers and Application Services in ABP, please make sure that the method you would like metrics for is a virtual method for the attribute to work.**

### Instrumentation

For custom instrumentation, you can use the `IInstrumentationProvider` interface to create custom spans and metrics. Get the instrumentation provider from the DI container and use it to create spans and metrics.

```csharp
public class MessageValidatorGrain : Grain, IMessageValidator
{
private readonly ILogger _logger;
private readonly ActivitySource _activitySource;
private static readonly string[] OffensiveWords = new string[] {"offensive", "bad", "rude"};

public MessageValidatorGrain(ILogger<MessageValidatorGrain> logger, IInstrumentationProvider instrumentationProvider)
{
_logger = logger;
_activitySource = instrumentationProvider.ActivitySource;
}

public Task<bool> IsOffensive(string message)
{
// This will start a custom trace for the IsOffensive method
using var myActivity = _activitySource.StartActivity($"{nameof(MessageValidatorGrain)}.IsOffensive");

_logger.LogInformation("""
IsOffensive message received: message = "{Message}"
""",
message);

return Task.FromResult(OffensiveWords.Any(message.Contains));
}
}
```

## Contributing

If you encounter a bug or have a feature request, please use the [Issue Tracker](https://github.com/AElfProject/aelf.opentelemetry/issues/new). The project is also open to contributions, so feel free to fork the project and open pull requests.
Expand Down

0 comments on commit c668ea3

Please sign in to comment.