|
1 | | -=========== |
2 | | -Diagnostics |
3 | | -=========== |
| 1 | +======= |
| 2 | +Tracing |
| 3 | +======= |
4 | 4 |
|
5 | | -Autofac provides some diagnostics capabilities that allow you to monitor and trace the resolution of components within the container. This can help for troubleshooting issues with component registration and resolution, understanding the lifetime of components, and optimizing performance. |
| 5 | +Autofac provides diagnostics support that allows you to monitor and trace the resolution of components within the container. This can help with troubleshooting issues in component registration and resolution, understanding component lifetimes, and optimizing performance. |
6 | 6 |
|
7 | 7 | .. warning:: |
8 | 8 |
|
9 | | - Metrics and diagnostics aren't free. You will get better performance if you **don't** have a diagnostic listener attached to the container and if you **don't** have counters being collected. Further, tracers like ``DefaultDiagnosticTracer`` that generate a full trace of an operation will increase memory and resource usage as they have to hold onto data during the entire resolve operation in order to generate a complete trace. It is recommended you only use diagnostics in a non-production environment; or use diagnostics listeners that handle individual events without tracking full operations. |
10 | | - |
11 | | -.. contents:: Diagnostics Options |
12 | | - :local: |
13 | | - :depth: 1 |
14 | | - |
15 | | -Tracing |
16 | | -======= |
| 9 | + **Tracing isn't free.** You will get better performance if you **don't** have a diagnostic listener attached to the container. Further, tracers like ``DefaultDiagnosticTracer`` that generate a full trace of an operation will increase memory and resource usage as they have to hold onto data during the entire resolve operation in order to generate a complete trace. It is recommended you only use diagnostics in a non-production environment; or use diagnostics listeners that handle individual events without tracking full operations. |
17 | 10 |
|
18 | 11 | Autofac 6.0 introduced diagnostics support in the form of `System.Diagnostics.DiagnosticSource <https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.diagnosticsource?view=netcore-3.1>`_. This allows you to intercept diagnostic events from Autofac. |
19 | 12 |
|
| 13 | +If you're interested in metrics for performance measurement, :doc:`Autofac also provides metrics <metrics>`. |
| 14 | + |
20 | 15 | Quick Start |
21 | 16 | ----------- |
22 | 17 |
|
@@ -359,147 +354,3 @@ When you get this low, you can control the subscriptions for your events separat |
359 | 354 | // the tracer should get an event. |
360 | 355 | var tracer = new ConsoleOperationTracer(); |
361 | 356 | container.DiagnosticSource.Subscribe(tracer, e => e == "Autofac.Operation.Start"); |
362 | | - |
363 | | -Metrics |
364 | | -======= |
365 | | - |
366 | | -Autofac 9.1 introduced opt-in performance metrics using `System.Diagnostics.Metrics <https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics>`_, the standard .NET metrics API. These are separate from the ``DiagnosticSource``-based tracing above - they are lightweight, aggregatable counters and histograms rather than per-request event traces. |
367 | | - |
368 | | -.. warning:: |
369 | | - |
370 | | - Metrics aren't free. Collecting them will incur a performance hit, so this is **not** something you want to leave on in production. Enable metrics only when you need to measure and tune Autofac's behavior in your application. |
371 | | - |
372 | | -Enabling Metrics |
373 | | ----------------- |
374 | | - |
375 | | -Set the ``AUTOFAC_METRICS`` environment variable to ``true`` or ``1`` before your process starts. The variable is checked once at startup; changing it at runtime has no effect. |
376 | | - |
377 | | -.. code-block:: shell |
378 | | -
|
379 | | - # Unix/macOS |
380 | | - export AUTOFAC_METRICS=true |
381 | | -
|
382 | | - # Windows (PowerShell) |
383 | | - $env:AUTOFAC_METRICS = "true" |
384 | | -
|
385 | | - # Windows (cmd) |
386 | | - set AUTOFAC_METRICS=true |
387 | | -
|
388 | | -Available Counters |
389 | | ------------------- |
390 | | - |
391 | | -Metrics are published under the meter name ``autofac`` (version ``1.0.0``). The following instruments are available: |
392 | | - |
393 | | -.. list-table:: |
394 | | - :header-rows: 1 |
395 | | - :widths: 40 15 10 35 |
396 | | - |
397 | | - * - Name |
398 | | - - Type |
399 | | - - Unit |
400 | | - - Description |
401 | | - * - ``autofac.middleware.duration`` |
402 | | - - Histogram |
403 | | - - s |
404 | | - - Time spent executing resolve pipeline middleware. |
405 | | - * - ``autofac.middleware.count`` |
406 | | - - Counter |
407 | | - - |
408 | | - - Number of resolve pipeline middleware executions. |
409 | | - * - ``autofac.reflection.activation.duration`` |
410 | | - - Histogram |
411 | | - - s |
412 | | - - Time spent activating components via ``ReflectionActivator``. |
413 | | - * - ``autofac.collection.build.duration`` |
414 | | - - Histogram |
415 | | - - s |
416 | | - - Time spent materializing implicit collection services. |
417 | | - * - ``autofac.collection.build.count`` |
418 | | - - Counter |
419 | | - - |
420 | | - - Number of implicit collection builds performed. |
421 | | - * - ``autofac.collection.build.items`` |
422 | | - - Counter |
423 | | - - |
424 | | - - Total elements added to implicit collections. |
425 | | - * - ``autofac.property.injection.duration`` |
426 | | - - Histogram |
427 | | - - s |
428 | | - - Time spent performing property injection. |
429 | | - * - ``autofac.property.injection.count`` |
430 | | - - Counter |
431 | | - - |
432 | | - - Number of instances that had property injection applied. |
433 | | - * - ``autofac.property.injection.assignments`` |
434 | | - - Counter |
435 | | - - |
436 | | - - Number of individual property assignments performed. |
437 | | - * - ``autofac.lock.contention.duration`` |
438 | | - - Histogram |
439 | | - - s |
440 | | - - Time threads waited to acquire Autofac internal locks. |
441 | | - * - ``autofac.lock.contention.count`` |
442 | | - - Counter |
443 | | - - |
444 | | - - Number of lock contention events observed. |
445 | | - * - ``autofac.lock.contention.total_time`` |
446 | | - - Counter |
447 | | - - s |
448 | | - - Cumulative time spent waiting on Autofac locks. |
449 | | - |
450 | | -.. note:: |
451 | | - |
452 | | - The lock contention metrics include per-service and per-lifetime-scope detail in their tags. This can produce high-cardinality data, so be mindful of the storage implications if you forward these to an external metrics backend. |
453 | | - |
454 | | -Collecting Metrics |
455 | | ------------------- |
456 | | - |
457 | | -Because Autofac uses the standard ``System.Diagnostics.Metrics`` API, you can collect these metrics with any compatible tool. |
458 | | - |
459 | | -**Using dotnet-counters (CLI)** |
460 | | - |
461 | | -The quickest way to spot-check metrics is `dotnet-counters <https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-counters>`_: |
462 | | - |
463 | | -.. code-block:: shell |
464 | | -
|
465 | | - dotnet-counters monitor --counters autofac --process-id <pid> |
466 | | -
|
467 | | -**Using MeterListener in code** |
468 | | - |
469 | | -You can subscribe programmatically to receive measurement callbacks: |
470 | | - |
471 | | -.. sourcecode:: csharp |
472 | | - |
473 | | - var listener = new MeterListener(); |
474 | | - |
475 | | - listener.InstrumentPublished = (instrument, listener) => |
476 | | - { |
477 | | - if (instrument.Meter.Name == "autofac") |
478 | | - { |
479 | | - listener.EnableMeasurementEvents(instrument); |
480 | | - } |
481 | | - }; |
482 | | - |
483 | | - listener.SetMeasurementEventCallback<double>((instrument, value, tags, state) => |
484 | | - { |
485 | | - Console.WriteLine($"{instrument.Name}: {value:F6}s"); |
486 | | - }); |
487 | | - |
488 | | - listener.SetMeasurementEventCallback<long>((instrument, value, tags, state) => |
489 | | - { |
490 | | - Console.WriteLine($"{instrument.Name}: {value}"); |
491 | | - }); |
492 | | - |
493 | | - listener.Start(); |
494 | | - |
495 | | -**Using OpenTelemetry** |
496 | | - |
497 | | -If your application already uses `OpenTelemetry <https://opentelemetry.io/docs/languages/dotnet/>`_, add the ``autofac`` meter to your ``MeterProvider``: |
498 | | - |
499 | | -.. sourcecode:: csharp |
500 | | - |
501 | | - using var meterProvider = Sdk.CreateMeterProviderBuilder() |
502 | | - .AddMeter("autofac") |
503 | | - // Add your exporter of choice: |
504 | | - .AddConsoleExporter() |
505 | | - .Build(); |
0 commit comments