Skip to content

Commit

Permalink
feat: add open telemetry tracing, prometheus metric
Browse files Browse the repository at this point in the history
  • Loading branch information
sixwaaaay committed Jan 13, 2024
1 parent 51c4525 commit 3ae4adb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/chore/compose.content.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ services:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
timeout: 20s
retries: 10
jaeger:
image: jaegertracing/all-in-one:1.52
ports:
- 4317:4317
- 16686:16686

volumes:
mysql-data:
Expand Down
37 changes: 36 additions & 1 deletion sharp/content/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/

using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -20,6 +21,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using MySqlConnector;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateSlimBuilder(args);

Expand All @@ -41,6 +46,35 @@
}
);

const string serviceName = "sharing.content";
var otelEndpoint = builder.Configuration.GetConnectionString("Otel_GrpcEndpoint");

Check warning on line 50 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L50

Added line #L50 was not covered by tests
builder.Services.AddOpenTelemetry().WithTracing(tcb =>
{

Check warning on line 52 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L52

Added line #L52 was not covered by tests
tcb
.AddSource(serviceName)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName))
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(o =>
{

Check warning on line 58 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L54-L58

Added lines #L54 - L58 were not covered by tests
if (!string.IsNullOrEmpty(otelEndpoint))
{
o.Endpoint = new Uri(otelEndpoint);
}
});
}).WithMetrics(mtb =>
{
mtb
.AddMeter("Microsoft.AspNetCore.Hosting", "Microsoft.AspNetCore.Server.Kestrel")
.AddView("http.server.request.duration",
new ExplicitBucketHistogramConfiguration
{
Boundaries = [0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]
})
.AddPrometheusExporter();
});

Check warning on line 74 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L60-L74

Added lines #L60 - L74 were not covered by tests

builder.Services.AddSingleton(TracerProvider.Default.GetTracer(serviceName));

Check warning on line 76 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L76

Added line #L76 was not covered by tests

builder.Services.AddAuthorization().AddProbe();
builder.Services.AddProblemDetails().AddResponseCompression();

Expand All @@ -63,6 +97,7 @@

app.UseToken();

app.MapPrometheusScrapingEndpoint();

Check warning on line 100 in sharp/content/Program.cs

View check run for this annotation

Codecov / codecov/patch

sharp/content/Program.cs#L100

Added line #L100 was not covered by tests
app.MapEndpoints();

app.Run();
Expand Down
4 changes: 4 additions & 0 deletions sharp/content/content.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="MySqlConnector" Version="2.3.3" />
<PackageReference Include="MySqlConnector.DependencyInjection" Version="2.3.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.7.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="Riok.Mapperly" Version="3.3.0" />
</ItemGroup>

Expand Down

0 comments on commit 3ae4adb

Please sign in to comment.