Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Creating dynamic, data-driven, rich PDF documents is a typical demand in many sectors of modern software development. Businesses greatly depend on their capacity to generate high-quality PDFs quickly, whether it's for creating bills, reports, or documentation. However, tracking and comprehending the effectiveness of PDF production processes becomes essential for preserving the health of apps and ensuring user pleasure as they get more complicated and large-scale.
This article aims to discuss how developers can improve PDF-generating operations and provide important insights into application performance by utilizing the features of IronPDF and OpenTelemetry.NET. We will explore the characteristics and advantages of both technologies and show how they may work together harmoniously to optimize the creation and monitoring of PDFs in .NET applications.
OpenTelemetry is a vendor-neutral observability framework created especially for cloud-native apps. OpenTelemetry automatic instrumentation provides a single API for gathering telemetry information, including logs, tracing, and metrics. With the use of this extensive data, developers may effectively debug problems, pinpoint performance bottlenecks, and obtain a full understanding of program behavior. OpenTelemetry supports both automatic and manual instrumentation for comprehensive and flexible telemetry data collection.
Integrating OpenTelemetry into your .NET applications offers several advantages:
OpenTelemetry needs to be configured in your application; for ASP.NET Core applications, this is usually done in the ConfigureServices
method of your Startup.cs
file. This is an illustration of a Jaeger exporter configuration:
using OpenTelemetry.Trace;
using OpenTelemetry.Exporter.Jaeger;
using OpenTelemetry.Resources;
public void ConfigureServices(IServiceCollection services)
{
// Configure OpenTelemetry
builder.Services.AddOpenTelemetry().WithTracing(options =>
{
// Configure Jaeger exporter
// serviceName we can read from environment variables
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddAspNetCoreInstrumentation().AddJaegerExporter(opt =>
{
opt.AgentHost = "localhost"; // Jaeger agent host
opt.AgentPort = 14250; // Jaeger agent port
});
});
// Other service configurations...
}
using OpenTelemetry.Trace;
using OpenTelemetry.Exporter.Jaeger;
using OpenTelemetry.Resources;
public void ConfigureServices(IServiceCollection services)
{
// Configure OpenTelemetry
builder.Services.AddOpenTelemetry().WithTracing(options =>
{
// Configure Jaeger exporter
// serviceName we can read from environment variables
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddAspNetCoreInstrumentation().AddJaegerExporter(opt =>
{
opt.AgentHost = "localhost"; // Jaeger agent host
opt.AgentPort = 14250; // Jaeger agent port
});
});
// Other service configurations...
}
Imports OpenTelemetry.Trace
Imports OpenTelemetry.Exporter.Jaeger
Imports OpenTelemetry.Resources
Public Sub ConfigureServices(ByVal services As IServiceCollection)
' Configure OpenTelemetry
builder.Services.AddOpenTelemetry().WithTracing(Sub(options)
' Configure Jaeger exporter
' serviceName we can read from environment variables
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName)).AddAspNetCoreInstrumentation().AddJaegerExporter(Sub(opt)
opt.AgentHost = "localhost" ' Jaeger agent host
opt.AgentPort = 14250 ' Jaeger agent port
End Sub)
End Sub)
' Other service configurations...
End Sub
Put the name of your service in place of serviceName
. According to your Jaeger configuration options, change the Jaeger agent host and port.
You must include the OpenTelemetry middleware in ASP.NET Core apps to get automatic instrumentation of incoming HTTP requests. In your Startup.cs
file's Configure
method, add the middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add OpenTelemetry middleware if we are using Prometheus
app.UseHttpMetrics();
// Other middleware configurations...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add OpenTelemetry middleware if we are using Prometheus
app.UseHttpMetrics();
// Other middleware configurations...
}
Public Sub Configure(ByVal app As IApplicationBuilder, ByVal env As IWebHostEnvironment)
' Add OpenTelemetry middleware if we are using Prometheus
app.UseHttpMetrics()
' Other middleware configurations...
End Sub
After setting everything up, OpenTelemetry will begin tracking incoming HTTP requests and sending telemetry information to the configured Jaeger backend on its own.
You can check for traces in your Jaeger backend to confirm that OpenTelemetry is functioning properly. Open the Jaeger user interface (usually available at http://localhost:16686
for Jaeger UI) and look for traces associated with your service.
OpenTelemetry is not a must for basic instrumentation, but it integrates with other backend operating systems to store and process telemetry data. Popular options consist of the officially supported operating systems:
The popular .NET package IronPDF allows you to create, edit, and render PDF documents within .NET programs. The functions for working with PDFs are numerous and include: converting HTML pages to PDFs; extracting text and images from PDFs; adding text, images, and shapes to pre-existing PDF documents; and producing PDFs from HTML content, photos, or raw data.
Two of IronPDF's key advantages are its ease of use and simplicity. Developers may effortlessly start creating PDFs within their .NET projects because of its intuitive API and comprehensive documentation. IronPDF's efficiency and speed are additional characteristics that help developers create high-quality PDF documents quickly and efficiently.
Some of the benefits of IronPDF:
Multiple NuGet packages are available for different components from OpenTelemetry. Depending on your needs, you can install the required packages. To send telemetry data to an observability backend (such as Jaeger, Zipkin, or Prometheus), you will at the very least need the OpenTelemetry package, instrumentation packages for your application framework (such as ASP.NET Core), and an exporter package. Also, ensure that the IronPDF package is installed in your project. To install it, use the NuGet Package Manager Console:
Install-Package OpenTelemetry
Install-Package OpenTelemetry.Instrumentation.AspNetCore
Install-Package OpenTelemetry.Exporter.Jaeger
Install-Package IronPdf
Open your ASP.NET Core application's Startup.cs
file to get to the ConfigureServices
function. To set up IronPDF, include the subsequent code.
using IronPdf;
public void ConfigureServices(IServiceCollection services)
{
// Configure IronPDF
services.AddSingleton<HtmlToPdf>();
// Other service configurations...
}
using IronPdf;
public void ConfigureServices(IServiceCollection services)
{
// Configure IronPDF
services.AddSingleton<HtmlToPdf>();
// Other service configurations...
}
Imports IronPdf
Public Sub ConfigureServices(ByVal services As IServiceCollection)
' Configure IronPDF
services.AddSingleton(Of HtmlToPdf)()
' Other service configurations...
End Sub
This code ensures that an instance of HtmlToPdf
is created and used by the application only by configuring IronPDF's HtmlToPdf
service as a singleton.
You may track and observe PDF generation processes in your .NET applications by integrating OpenTelemetry.NET with IronPDF. Let's examine the code example in depth, going over each step step-by-step:
using Microsoft.AspNetCore.Mvc;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
namespace DemoWebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly HtmlToPdf _htmlToPdf;
private readonly ILogger<WeatherForecastController> _logger;
private readonly Tracer _tracer;
public WeatherForecastController(ILogger<WeatherForecastController> logger, HtmlToPdf htmlToPdf, TracerProvider tracerProvider)
{
_tracer = tracerProvider.GetTracer("Demo");
_htmlToPdf = htmlToPdf;
}
[HttpGet]
public FileContentResult Generate()
{
string fileName = "Sample.pdf";
var stream = GeneratePdf("Hello IronPDF");
return new FileContentResult(stream, "application/octet-stream")
{
FileDownloadName = fileName
};
}
private byte[] GeneratePdf(string htmlContent)
{
using (var activity = _tracer.StartActiveSpan("PDF Generation"))
{
var pdfDocument = _htmlToPdf.RenderHtmlAsPdf(htmlContent);
var pdfBytes = pdfDocument.BinaryData;
return pdfBytes;
}
}
}
}
using Microsoft.AspNetCore.Mvc;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
namespace DemoWebApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase
{
private readonly HtmlToPdf _htmlToPdf;
private readonly ILogger<WeatherForecastController> _logger;
private readonly Tracer _tracer;
public WeatherForecastController(ILogger<WeatherForecastController> logger, HtmlToPdf htmlToPdf, TracerProvider tracerProvider)
{
_tracer = tracerProvider.GetTracer("Demo");
_htmlToPdf = htmlToPdf;
}
[HttpGet]
public FileContentResult Generate()
{
string fileName = "Sample.pdf";
var stream = GeneratePdf("Hello IronPDF");
return new FileContentResult(stream, "application/octet-stream")
{
FileDownloadName = fileName
};
}
private byte[] GeneratePdf(string htmlContent)
{
using (var activity = _tracer.StartActiveSpan("PDF Generation"))
{
var pdfDocument = _htmlToPdf.RenderHtmlAsPdf(htmlContent);
var pdfBytes = pdfDocument.BinaryData;
return pdfBytes;
}
}
}
}
Imports Microsoft.AspNetCore.Mvc
Imports OpenTelemetry.Trace
Imports System.Diagnostics
Imports System.Net
Imports System.Net.Http.Headers
Namespace DemoWebApplication.Controllers
<ApiController>
<Route("[controller]")>
Public Class DemoController
Inherits ControllerBase
Private ReadOnly _htmlToPdf As HtmlToPdf
Private ReadOnly _logger As ILogger(Of WeatherForecastController)
Private ReadOnly _tracer As Tracer
Private Function WeatherForecastController(ByVal logger As ILogger(Of WeatherForecastController), ByVal htmlToPdf As HtmlToPdf, ByVal tracerProvider As TracerProvider) As Public
_tracer = tracerProvider.GetTracer("Demo")
_htmlToPdf = htmlToPdf
End Function
<HttpGet>
Public Function Generate() As FileContentResult
Dim fileName As String = "Sample.pdf"
Dim stream = GeneratePdf("Hello IronPDF")
Return New FileContentResult(stream, "application/octet-stream") With {.FileDownloadName = fileName}
End Function
Private Function GeneratePdf(ByVal htmlContent As String) As Byte()
Using activity = _tracer.StartActiveSpan("PDF Generation")
Dim pdfDocument = _htmlToPdf.RenderHtmlAsPdf(htmlContent)
Dim pdfBytes = pdfDocument.BinaryData
Return pdfBytes
End Using
End Function
End Class
End Namespace
To generate a tracer instance for our application, we inject the TracerProvider
services. An OpenTelemetry activity called "PDF Generation" is created by the Generate
method to symbolize the work that is now underway. We mimic some work within the activity (substitute your own logic). We establish a histogram metric called work_duration_ms
to monitor the work's millisecond duration. We add a data point to the metric, tagging it "Demo" with the value "PDF Generation" and documenting the duration (1000 ms in this case). Tags give analysis an extra layer of context.
Generated PDF output from the above source code:
Below is the screen of the generated trace from the Jaeger UI:
OpenTelemetry is a game-changer for .NET application performance optimization and health monitoring. It gives developers a thorough understanding of the inner workings of their applications by offering a vendor-neutral and standardized method for gathering, analyzing, and exporting telemetry data (metrics, traces, and logs).
By integrating OpenTelemetry.NET with IronPDF, developers can improve workflows for creating PDFs and obtain insights into the performance of their applications. By leveraging the OpenTelemetry.NET implementation to manually instrument the PDF-generating processes, developers may track execution pathways, monitor performance metrics, and pinpoint regions that require optimization. Furthermore, centralized monitoring metrics collection and analysis are made possible by exporting telemetry data to observability platforms, giving developers the ability to preserve the health of their applications and provide a flawless user experience.
The integration of OpenTelemetry.NET implementation and IronPDF can enhance developers' workflows for creating PDFs and guarantee scalability, performance, and dependability in .NET applications. The integration of these technologies helps developers fulfill the needs of modern software development and produce outstanding results, whether it's creating invoices, reports, or documentation.
When bought as a package, IronPDF is fairly priced and includes a lifetime license. The package offers great value at only $749, which is a one-time fee for many systems. It offers license holders online engineering support around the clock. Please visit the Iron Software website for more details about the charge. Explore Iron Software's products to learn more about the solutions they offer.
9 .NET API products for your office documents