diff --git a/Inspectron.Epson/Queue/PrintServer.cs b/Inspectron.Epson/Queue/PrintServer.cs index 19f5409..f5f8d31 100644 --- a/Inspectron.Epson/Queue/PrintServer.cs +++ b/Inspectron.Epson/Queue/PrintServer.cs @@ -8,18 +8,19 @@ public class PrintServer private readonly IPrintService _printService; private readonly ILogger _logger; private readonly IJobStatusReporter _statusReporter; + private readonly Inspectron.Epson.PrintServer.Telemetry.IInsinTelemetry _telemetry; private readonly ConcurrentDictionary _printerQueues; private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1); private int _readerCount = 0; private readonly object _readerCountLock = new(); - public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter) + public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter, Inspectron.Epson.PrintServer.Telemetry.IInsinTelemetry telemetry) { _printService = printService; _logger = logger; _statusReporter = statusReporter; + _telemetry = telemetry; _printerQueues = new ConcurrentDictionary(); - } /// @@ -80,7 +81,7 @@ public class PrintServer public void RegisterPrinter(string printerIp) { - var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter); + var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter, _telemetry); if (_printerQueues.TryAdd(printerIp, queue)) { queue.Start(); diff --git a/Inspectron.Epson/Queue/PrinterQueue.cs b/Inspectron.Epson/Queue/PrinterQueue.cs index d823d9b..6f1e3e3 100644 --- a/Inspectron.Epson/Queue/PrinterQueue.cs +++ b/Inspectron.Epson/Queue/PrinterQueue.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using Inspectron.Epson.PrintServer.Telemetry; using Microsoft.Extensions.Logging; namespace Inspectron.Epson.Queue; @@ -15,11 +16,12 @@ public class PrinterQueue private readonly ILogger _logger; private readonly PrintServer _printServer; private readonly IJobStatusReporter _statusReporter; + private readonly IInsinTelemetry _telemetry; public bool IsProcessing { get; private set; } public int QueueLength => _queue.Count + _priorityQueue.Count; - public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter) + public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter, IInsinTelemetry telemetry) { PrinterIp = printerIp; _queue = new ConcurrentQueue(); @@ -30,6 +32,7 @@ public class PrinterQueue _logger = logger; _printServer = printServer; _statusReporter = statusReporter; + _telemetry = telemetry; } public void Enqueue(PrintJob job) @@ -74,13 +77,22 @@ public class PrinterQueue _logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp); _printServer.EnterPrintLock(); + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); try { // Call the actual print function var result = await _printService.PrintAsync(PrinterIp, job); + stopwatch.Stop(); if (!result.Success) { + var failKind = InsinEventKinds.MapErrorTypeToKind(result.ErrorType); + SafeEmit(failKind, InsinMessageFormatter.Format( + ("printer", PrinterIp), + ("receiptType", job.Document?.ReceiptType.ToString()), + ("retry", job.RetryCount.ToString()), + ("error", result.ErrorType.ToString()))); + if (result.ErrorType == PrintErrorType.ConversionError) { _logger.LogWarning("Job failed due to conversion error, not re-queuing"); @@ -108,6 +120,10 @@ public class PrinterQueue { _logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp); await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed); + SafeEmit(InsinEventKinds.JobPrinted, InsinMessageFormatter.Format( + ("printer", PrinterIp), + ("receiptType", job.Document?.ReceiptType.ToString()), + ("durationMs", stopwatch.ElapsedMilliseconds.ToString()))); } } catch (Exception ex) @@ -146,4 +162,10 @@ public class PrinterQueue { return new List(_queue); } + + private void SafeEmit(string kind, string message) + { + try { _telemetry.Emit(kind, message); } + catch (Exception ex) { _logger.LogWarning(ex, "insin telemetry emit failed"); } + } } \ No newline at end of file