|
| 1 | +:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/7.0 |
| 2 | + |
| 3 | +:github: https://github.com/elastic/elasticsearch-net |
| 4 | + |
| 5 | +:nuget: https://www.nuget.org/packages |
| 6 | + |
| 7 | +//// |
| 8 | +IMPORTANT NOTE |
| 9 | +============== |
| 10 | +This file has been generated from https://github.com/elastic/elasticsearch-net/tree/7.x/src/Tests/Tests/ClientConcepts/Troubleshooting/DiagnosticSource.doc.cs. |
| 11 | +If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file, |
| 12 | +please modify the original csharp file found at the link and submit the PR with that change. Thanks! |
| 13 | +//// |
| 14 | + |
| 15 | +[[diagnostic-source]] |
| 16 | +=== Diagnostic Source |
| 17 | + |
| 18 | +Elasticsearch.Net and by proxy NEST ship with support for DiagnosticSource and Activity out of the box. |
| 19 | + |
| 20 | +To aid with their discover the topics you can subscribe on and the event names they emit are exposed as |
| 21 | +strongly typed strings under `Elasticsearch.Net.Diagnostics.DiagnosticSources` |
| 22 | + |
| 23 | +Subscribing to DiagnosticSources means implementing `IObserver<DiagnosticListener>` |
| 24 | +or use `.Subscribe(observer, filter)` to opt in to the correct topic. |
| 25 | + |
| 26 | +Here we choose the more verbose `IObserver<>` implementation. |
| 27 | + |
| 28 | +[source,csharp] |
| 29 | +---- |
| 30 | +private class ListenerObserver : IObserver<DiagnosticListener>, IDisposable |
| 31 | +{ |
| 32 | + private long _messagesWrittenToConsole = 0; |
| 33 | + public long MessagesWrittenToConsole => _messagesWrittenToConsole; |
| 34 | +
|
| 35 | + public Exception SeenException { get; private set; } |
| 36 | + public void OnError(Exception error) => SeenException = error; |
| 37 | +
|
| 38 | + public bool Completed { get; private set; } |
| 39 | + public void OnCompleted() => Completed = true; |
| 40 | +
|
| 41 | + private void WriteToConsole<T>(string eventName, T data) |
| 42 | + { |
| 43 | + var a = Activity.Current; |
| 44 | + Interlocked.Increment(ref _messagesWrittenToConsole); |
| 45 | + } |
| 46 | +
|
| 47 | + private List<IDisposable> Disposables { get; } = new List<IDisposable>(); |
| 48 | +
|
| 49 | + /** |
| 50 | + * By inspecting the name we selectively subscribe only to topics `Elasticsearch.Net` emits. |
| 51 | + * |
| 52 | + * Thanks to `DiagnosticSources` you do not have to guess the topics we emit under. |
| 53 | + * |
| 54 | + * `DiagnosticListener.Subscribe` expects an `IObserver<KeyValuePair<string, object>>` which is useful to create |
| 55 | + * a decoupled messaging contract but as a subscriber you would like to know what `object` is. |
| 56 | + * |
| 57 | + * Therefor each topic we ship with has a dedicated `Observer` implementation that takes an `onNext` lambda |
| 58 | + * which is typed to the context object we actually emit. |
| 59 | + * |
| 60 | + */ |
| 61 | + public void OnNext(DiagnosticListener value) |
| 62 | + { |
| 63 | + void TrySubscribe(string sourceName, Func<IObserver<KeyValuePair<string, object>>> listener) |
| 64 | + { |
| 65 | + if (value.Name != sourceName) return; |
| 66 | +
|
| 67 | + var subscription = value.Subscribe(listener()); |
| 68 | + Disposables.Add(subscription); |
| 69 | + } |
| 70 | +
|
| 71 | + TrySubscribe(DiagnosticSources.AuditTrailEvents.SourceName, |
| 72 | + () => new AuditDiagnosticObserver(v => WriteToConsole(v.EventName, v.Audit))); |
| 73 | +
|
| 74 | + TrySubscribe(DiagnosticSources.Serializer.SourceName, |
| 75 | + () => new SerializerDiagnosticObserver(v => WriteToConsole(v.EventName, v.Registration))); |
| 76 | + /** |
| 77 | + * RequestPipeline emits a different context object for the start of the `Activity` then it does |
| 78 | + * for the end of the `Activity` therefor `RequestPipelineDiagnosticObserver` accepts two `onNext` lambda's. |
| 79 | + * One for the `.Start` events and one for the `.Stop` events. |
| 80 | + */ |
| 81 | + TrySubscribe(DiagnosticSources.RequestPipeline.SourceName, |
| 82 | + () => new RequestPipelineDiagnosticObserver( |
| 83 | + v => WriteToConsole(v.EventName, v.RequestData), |
| 84 | + v => WriteToConsole(v.EventName, v.Response) |
| 85 | + )); |
| 86 | +
|
| 87 | + TrySubscribe(DiagnosticSources.HttpConnection.SourceName, |
| 88 | + () => new HttpConnectionDiagnosticObserver( |
| 89 | + v => WriteToConsole(v.EventName, v.RequestData), |
| 90 | + v => WriteToConsole(v.EventName, v.StatusCode) |
| 91 | + )); |
| 92 | + } |
| 93 | +
|
| 94 | + public void Dispose() |
| 95 | + { |
| 96 | + foreach(var d in Disposables) d.Dispose(); |
| 97 | + } |
| 98 | +} |
| 99 | +---- |
| 100 | + |
| 101 | +Here we hook into all diagnostic sources and use `ListenerObserver` to only listen to the ones |
| 102 | +from `Elasticsearch.Net` |
| 103 | + |
| 104 | +[source,csharp] |
| 105 | +---- |
| 106 | +using(var listenerObserver = new ListenerObserver()) |
| 107 | +using (var subscription = DiagnosticListener.AllListeners.Subscribe(listenerObserver)) |
| 108 | +{ |
| 109 | +
|
| 110 | + /** |
| 111 | + * We'll use a Sniffing connection pool here since it sniffs on startup and pings before |
| 112 | + * first usage, so our diagnostics are involved enough to showcase most topics. |
| 113 | + */ |
| 114 | + var pool = new SniffingConnectionPool(new []{ TestConnectionSettings.CreateUri() }); |
| 115 | + var connectionSettings = new ConnectionSettings(pool) |
| 116 | + .DefaultMappingFor<Project>(i => i |
| 117 | + .IndexName("project") |
| 118 | + ); |
| 119 | +
|
| 120 | + var client = new ElasticClient(connectionSettings); |
| 121 | +
|
| 122 | + /** |
| 123 | + * After issuing the following request |
| 124 | + */ |
| 125 | + var response = client.Search<Project>(s => s |
| 126 | + .MatchAll() |
| 127 | + ); |
| 128 | +
|
| 129 | + listenerObserver.SeenException.Should().BeNull(); |
| 130 | + listenerObserver.Completed.Should().BeFalse(); |
| 131 | + listenerObserver.MessagesWrittenToConsole.Should().BeGreaterThan(0); |
| 132 | +} |
| 133 | +---- |
| 134 | + |
0 commit comments