-
Notifications
You must be signed in to change notification settings - Fork 240
OpenTelemetry follow up PR #613
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a39b8fd
support db.query.text tag
joao-r-reis 610c50b
add opentelemetry distributed tracing example
joao-r-reis a640962
add batch statement opentelemetry test
joao-r-reis 195f7d9
add OtelExporter to Otel distributed tracing example
joao-r-reis a0fe879
update tests after opentelemetry semantics upgrade
joao-r-reis 91d0a8c
rename builder extension to WithOpenTelemetryInstrumentation
joao-r-reis 7ab6233
Fix docs and fix csproj file
joao-r-reis 7b338bb
fix OpenTelemetry integration with speculative executions
joao-r-reis 099346c
fix activity name
joao-r-reis ae28871
adjust names
joao-r-reis 42b48fb
fix unit test
joao-r-reis b4ca9a5
add RequestTracker.OnNodeAborted
joao-r-reis fc3a6d0
PR suggestions
joao-r-reis 19f88c5
performance
joao-r-reis 6e3156e
performance
joao-r-reis a54fa62
remove unneeded volatile
joao-r-reis 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
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\Cassandra\Cassandra.csproj" /> | ||
<ProjectReference Include="..\..\..\..\src\Extensions\Cassandra.OpenTelemetry\Cassandra.OpenTelemetry.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,6 @@ | ||
@Api_HostAddress = http://localhost:5284 | ||
|
||
GET {{Api_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
26 changes: 26 additions & 0 deletions
26
examples/OpenTelemetry/DistributedTracing/Api/Controllers/WeatherForecastController.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,26 @@ | ||
using Cassandra.Mapping; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Api.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private readonly ILogger<WeatherForecastController> _logger; | ||
private readonly IMapper _mapper; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger, IMapper mapper) | ||
{ | ||
_logger = logger; | ||
_mapper = mapper; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public async Task<IEnumerable<WeatherForecast>> GetAsync() | ||
{ | ||
var results = await _mapper.FetchAsync<WeatherForecast>().ConfigureAwait(false); | ||
return results.ToArray(); | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
examples/OpenTelemetry/DistributedTracing/Api/Program.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,149 @@ | ||
|
||
using Cassandra; | ||
using Cassandra.Mapping; | ||
using Cassandra.OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using ISession = Cassandra.ISession; | ||
|
||
namespace Api | ||
{ | ||
// This API creates a keyspace "weather" and table "weather_forecast" on startup (if they don't exist). | ||
public class Program | ||
{ | ||
private const string CassandraContactPoint = "127.0.0.1"; | ||
private const int CassandraPort = 9042; | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.ConfigureResource(resource => resource.AddService("Weather Forecast API")) | ||
.WithTracing(tracing => tracing | ||
.AddAspNetCoreInstrumentation() | ||
.AddSource(CassandraActivitySourceHelper.ActivitySourceName) | ||
//.AddOtlpExporter(opt => opt.Endpoint = new Uri("http://localhost:4317")) // uncomment if you want to use an OTPL exporter like Jaeger | ||
.AddConsoleExporter()); | ||
builder.Services.AddSingleton<ICluster>(_ => | ||
{ | ||
var cassandraBuilder = Cluster.Builder() | ||
.AddContactPoint(CassandraContactPoint) | ||
.WithPort(CassandraPort) | ||
.WithOpenTelemetryInstrumentation(opts => opts.IncludeDatabaseStatement = true); | ||
return cassandraBuilder.Build(); | ||
}); | ||
builder.Services.AddSingleton<ISession>(provider => | ||
{ | ||
var cluster = provider.GetService<ICluster>(); | ||
if (cluster == null) | ||
{ | ||
throw new ArgumentNullException(nameof(cluster)); | ||
} | ||
return cluster.Connect(); | ||
}); | ||
builder.Services.AddSingleton<IMapper>(provider => | ||
{ | ||
var session = provider.GetService<ISession>(); | ||
if (session == null) | ||
{ | ||
throw new ArgumentNullException(nameof(session)); | ||
} | ||
|
||
return new Mapper(session); | ||
}); | ||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// force initialization of C* Session because if it fails then it can not be reused and the app should restart | ||
// (or the Session should be created before registering it on the service collection with some kind of retries if needed) | ||
var session = app.Services.GetService<ISession>(); | ||
if (session == null) | ||
{ | ||
throw new ArgumentNullException(nameof(session)); | ||
} | ||
var mapper = app.Services.GetService<IMapper>(); | ||
if (mapper == null) | ||
{ | ||
throw new ArgumentNullException(nameof(mapper)); | ||
} | ||
|
||
await SetupWeatherForecastDb(session, mapper).ConfigureAwait(false); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseAuthorization(); | ||
|
||
|
||
app.MapControllers(); | ||
|
||
await app.RunAsync().ConfigureAwait(false); | ||
} | ||
|
||
private static async Task SetupWeatherForecastDb(ISession session, IMapper mapper) | ||
{ | ||
await session.ExecuteAsync( | ||
new SimpleStatement( | ||
"CREATE KEYSPACE IF NOT EXISTS weather WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 1 }")) | ||
.ConfigureAwait(false); | ||
|
||
await session.ExecuteAsync( | ||
new SimpleStatement( | ||
"CREATE TABLE IF NOT EXISTS weather.weather_forecast ( id uuid PRIMARY KEY, date timestamp, summary text, temp_c int )")) | ||
.ConfigureAwait(false); | ||
|
||
var weatherForecasts = new WeatherForecast[] | ||
{ | ||
new() | ||
{ | ||
Date = new DateTime(2024, 9, 18), | ||
Id = Guid.Parse("9c9fdc2c-cf59-4ebe-93ac-c26e2fd1a56a"), | ||
Summary = "Generally clear. Areas of smoke and haze are possible, reducing visibility at times. High 30\u00b0C. Winds NE at 10 to 15 km/h.", | ||
TemperatureC = 30 | ||
}, | ||
new() | ||
{ | ||
Date = new DateTime(2024, 9, 19), | ||
Id = Guid.Parse("b38b338f-56a8-4f56-a8f1-640d037ed8f6"), | ||
Summary = "Generally clear. Areas of smoke and haze are possible, reducing visibility at times. High 28\u00b0C. Winds SSW at 10 to 15 km/h.", | ||
TemperatureC = 28 | ||
}, | ||
new() | ||
{ | ||
Date = new DateTime(2024, 9, 20), | ||
Id = Guid.Parse("04b8e06a-7f59-4921-888f-1a71a52ff7bb"), | ||
Summary = "Partly cloudy. High 24\u00b0C. Winds SW at 10 to 15 km/h.", | ||
TemperatureC = 24 | ||
}, | ||
new() | ||
{ | ||
Date = new DateTime(2024, 9, 21), | ||
Id = Guid.Parse("036c25a6-e354-4613-8c27-1822ffb9e184"), | ||
Summary = "Rain. High 23\u00b0C. Winds SSW and variable. Chance of rain 70%.", | ||
TemperatureC = 23 | ||
}, | ||
new() | ||
{ | ||
Date = new DateTime(2024, 9, 22), | ||
Id = Guid.Parse("ebd16ca8-ee00-42c1-9763-bb19dbf9a8e9"), | ||
Summary = "Morning showers. High 22\u00b0C. Winds SW and variable. Chance of rain 50%.", | ||
TemperatureC = 22 | ||
}, | ||
}; | ||
|
||
var tasks = weatherForecasts.Select(w => mapper.InsertAsync(w)); | ||
await Task.WhenAll(tasks).ConfigureAwait(false); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/OpenTelemetry/DistributedTracing/Api/Properties/launchSettings.json
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,15 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Api": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5284", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/OpenTelemetry/DistributedTracing/Api/WeatherForecast.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,24 @@ | ||
using Cassandra.Mapping.Attributes; | ||
|
||
namespace Api | ||
{ | ||
[Table(Keyspace = "weather", Name = "weather_forecast")] | ||
public class WeatherForecast | ||
{ | ||
[PartitionKey] | ||
[Column("id")] | ||
public Guid Id { get; set; } | ||
|
||
[Column("date")] | ||
public DateTime? Date { get; set; } | ||
|
||
[Column("temp_c")] | ||
public int TemperatureC { get; set; } | ||
|
||
[Ignore] | ||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
[Column("summary")] | ||
public string? Summary { get; set; } | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/OpenTelemetry/DistributedTracing/Api/appsettings.Development.json
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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/OpenTelemetry/DistributedTracing/Api/appsettings.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
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
60 changes: 60 additions & 0 deletions
60
examples/OpenTelemetry/DistributedTracing/Client/Program.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,60 @@ | ||
using System.Diagnostics; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Client | ||
{ | ||
internal class Program | ||
{ | ||
private const string WeatherApiUri = "http://localhost:5284"; | ||
|
||
private const string WeatherForecastEndpointUri = WeatherApiUri + "/" + "WeatherForecast"; | ||
|
||
private static readonly ActivitySource ClientActivity = new ActivitySource("Weather Forecast Client Request"); | ||
|
||
static async Task Main(string[] args) | ||
{ | ||
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService( | ||
serviceName: "Weather Forecast Client", | ||
serviceVersion: "1.0.0")) | ||
.AddSource(ClientActivity.Name) | ||
//.AddOtlpExporter(opt => opt.Endpoint = new Uri("http://localhost:4317")) // uncomment if you want to use an OTPL exporter like Jaeger | ||
.Build(); | ||
var cts = new CancellationTokenSource(); | ||
var task = Task.Run(async () => | ||
{ | ||
await Task.Delay(1000, cts.Token).ConfigureAwait(false); | ||
using var httpClient = new HttpClient(); | ||
while (!cts.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
using (var _ = ClientActivity.StartActivity(ActivityKind.Client)) | ||
{ | ||
await Console.Out.WriteLineAsync("TraceId: " + Activity.Current?.TraceId + Environment.NewLine + "Sending request.").ConfigureAwait(false); | ||
var forecastResponse = await httpClient.GetAsync(WeatherForecastEndpointUri, cts.Token).ConfigureAwait(false); | ||
|
||
if (forecastResponse.IsSuccessStatusCode) | ||
{ | ||
var content = await forecastResponse.Content.ReadAsStringAsync(cts.Token).ConfigureAwait(false); | ||
await Console.Out.WriteLineAsync("TraceId: " + Activity.Current?.TraceId + Environment.NewLine + content + Environment.NewLine).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
await Task.Delay(5000, cts.Token).ConfigureAwait(false); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
} | ||
} | ||
}); | ||
|
||
await Console.Out.WriteLineAsync("Press enter to shut down.").ConfigureAwait(false); | ||
await Console.In.ReadLineAsync().ConfigureAwait(false); | ||
await cts.CancelAsync().ConfigureAwait(false); | ||
await task.ConfigureAwait(false); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/OpenTelemetry/DistributedTracing/Client/WeatherForecast.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,13 @@ | ||
namespace Client | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateOnly Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF { get; set; } | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.