|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="OpenTelemetryDashboard.cs" company="Marimer LLC"> |
| 3 | +// Copyright (c) Marimer LLC. All rights reserved. |
| 4 | +// Website: https://cslanet.com |
| 5 | +// </copyright> |
| 6 | +// <summary>OpenTelemetry dashboard implementation for data portal</summary> |
| 7 | +//----------------------------------------------------------------------- |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Diagnostics; |
| 12 | +using System.Diagnostics.Metrics; |
| 13 | +using System.Threading; |
| 14 | + |
| 15 | +namespace Csla.Server.Dashboard |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Data portal server dashboard that reports metrics |
| 19 | + /// using OpenTelemetry instrumentation. |
| 20 | + /// </summary> |
| 21 | + public class OpenTelemetryDashboard : IDashboard |
| 22 | + { |
| 23 | + private readonly Meter _meter; |
| 24 | + private readonly Counter<long> _totalCallsCounter; |
| 25 | + private readonly Counter<long> _completedCallsCounter; |
| 26 | + private readonly Counter<long> _failedCallsCounter; |
| 27 | + private readonly Histogram<double> _callDurationHistogram; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets the OpenTelemetry meter name for CSLA data portal metrics. |
| 31 | + /// </summary> |
| 32 | + public const string MeterName = "Csla.DataPortal"; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the OpenTelemetry meter version. |
| 36 | + /// </summary> |
| 37 | + public const string MeterVersion = "1.0.0"; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Creates an instance of the type. |
| 41 | + /// </summary> |
| 42 | + public OpenTelemetryDashboard() |
| 43 | + { |
| 44 | + _meter = new Meter(MeterName, MeterVersion); |
| 45 | + |
| 46 | + _totalCallsCounter = _meter.CreateCounter<long>( |
| 47 | + name: "csla.dataportal.calls.total", |
| 48 | + unit: "{call}", |
| 49 | + description: "Total number of data portal calls"); |
| 50 | + |
| 51 | + _completedCallsCounter = _meter.CreateCounter<long>( |
| 52 | + name: "csla.dataportal.calls.completed", |
| 53 | + unit: "{call}", |
| 54 | + description: "Number of successfully completed data portal calls"); |
| 55 | + |
| 56 | + _failedCallsCounter = _meter.CreateCounter<long>( |
| 57 | + name: "csla.dataportal.calls.failed", |
| 58 | + unit: "{call}", |
| 59 | + description: "Number of failed data portal calls"); |
| 60 | + |
| 61 | + _callDurationHistogram = _meter.CreateHistogram<double>( |
| 62 | + name: "csla.dataportal.call.duration", |
| 63 | + unit: "ms", |
| 64 | + description: "Duration of data portal calls in milliseconds"); |
| 65 | + |
| 66 | + FirstCall = DateTimeOffset.Now; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the time the data portal was first invoked |
| 71 | + /// </summary> |
| 72 | + public DateTimeOffset FirstCall { get; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Gets the most recent time the data portal |
| 76 | + /// was invoked |
| 77 | + /// </summary> |
| 78 | + public DateTimeOffset LastCall { get; private set; } |
| 79 | + |
| 80 | + private long _totalCalls; |
| 81 | + /// <summary> |
| 82 | + /// Gets the total number of times the data portal |
| 83 | + /// has been invoked |
| 84 | + /// </summary> |
| 85 | + public long TotalCalls => Interlocked.Read(ref _totalCalls); |
| 86 | + |
| 87 | + private long _completedCalls; |
| 88 | + /// <summary> |
| 89 | + /// Gets the number of times data portal |
| 90 | + /// calls have successfully completed |
| 91 | + /// </summary> |
| 92 | + public long CompletedCalls => Interlocked.Read(ref _completedCalls); |
| 93 | + |
| 94 | + private long _failedCalls; |
| 95 | + /// <summary> |
| 96 | + /// Gets the number of times data portal |
| 97 | + /// calls have failed |
| 98 | + /// </summary> |
| 99 | + public long FailedCalls => Interlocked.Read(ref _failedCalls); |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets the items in the recent activity queue. |
| 103 | + /// </summary> |
| 104 | + public List<Activity> GetRecentActivity() |
| 105 | + { |
| 106 | + return new List<Activity>(); |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc /> |
| 110 | + void IDashboard.InitializeCall(InterceptArgs e) |
| 111 | + { |
| 112 | + if (e is null) |
| 113 | + throw new ArgumentNullException(nameof(e)); |
| 114 | + |
| 115 | + LastCall = DateTimeOffset.Now; |
| 116 | + Interlocked.Add(ref _totalCalls, 1); |
| 117 | + |
| 118 | + var tags = new TagList |
| 119 | + { |
| 120 | + { "object.type", e.ObjectType.Name }, |
| 121 | + { "operation", e.Operation.ToString() } |
| 122 | + }; |
| 123 | + |
| 124 | + _totalCallsCounter.Add(1, tags); |
| 125 | + } |
| 126 | + |
| 127 | + /// <inheritdoc /> |
| 128 | + void IDashboard.CompleteCall(InterceptArgs e) |
| 129 | + { |
| 130 | + if (e is null) |
| 131 | + throw new ArgumentNullException(nameof(e)); |
| 132 | + |
| 133 | + var tags = new TagList |
| 134 | + { |
| 135 | + { "object.type", e.ObjectType.Name }, |
| 136 | + { "operation", e.Operation.ToString() } |
| 137 | + }; |
| 138 | + |
| 139 | + if (e.Exception != null) |
| 140 | + { |
| 141 | + Interlocked.Add(ref _failedCalls, 1); |
| 142 | + tags.Add("exception.type", e.Exception.GetType().Name); |
| 143 | + _failedCallsCounter.Add(1, tags); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + Interlocked.Add(ref _completedCalls, 1); |
| 148 | + _completedCallsCounter.Add(1, tags); |
| 149 | + } |
| 150 | + |
| 151 | + _callDurationHistogram.Record(e.Runtime.TotalMilliseconds, tags); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Dispose resources used by this object. |
| 156 | + /// </summary> |
| 157 | + public void Dispose() |
| 158 | + { |
| 159 | + _meter?.Dispose(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments