-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
250 lines (208 loc) · 9.18 KB
/
Copy pathMainPage.xaml.cs
File metadata and controls
250 lines (208 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using Com.Datadog.Android.Log;
using Com.Datadog.Android.Rum;
// The hand-written convenience layers live beside the generated types they extend, so each
// namespace has to be in scope: Tracer for BuildSpan(string), Span for the span's own helpers,
// Propagation for Inject.
using Com.Datadog.Android.Trace;
using Com.Datadog.Android.Trace.Api.Propagation;
using Com.Datadog.Android.Trace.Api.Span;
using Com.Datadog.Android.Trace.Api.Tracer;
namespace DatadogNet.Android.Example;
/// <summary>
/// Each button drives one part of the Datadog API. The <see cref="ActivityLabel"/> echoes what was
/// reported, so the sample is useful even without a Datadog account to send it to.
/// </summary>
public partial class MainPage : ContentPage
{
private const string ViewKey = "main";
private readonly List<string> activity = [];
private IDisposable? viewScope;
private int count;
public MainPage()
{
InitializeComponent();
StatusLabel.Text = Datadog.IsConfigured
? "Reporting to Datadog."
: "No client token set, so events are collected but never delivered. "
+ "Put your own values in Datadog.cs to send them for real.";
}
protected override void OnAppearing()
{
base.OnAppearing();
// One RUM view per page. MAUI renders every page into a single Activity, so without this
// the whole app reports as a single view for its entire lifetime.
viewScope = Datadog.StartView(ViewKey, "Main Page");
}
protected override void OnDisappearing()
{
// Dispose and stop. An earlier version of this sample started a *new* view here, which is
// the mistake worth not shipping in a sample: it left a view open for the rest of the
// session, collecting every action and error that followed.
viewScope?.Dispose();
viewScope = null;
base.OnDisappearing();
}
private void OnCounterClicked(object? sender, EventArgs e)
{
count++;
CounterBtn.Text = count == 1 ? "Recorded 1 action" : $"Recorded {count} actions";
SemanticScreenReader.Announce(CounterBtn.Text);
// Attributes are plain C# values - the convenience overload converts them. The raw binding
// takes an IDictionary<string, Java.Lang.Object> and needs every value hand-wrapped.
Datadog.TrackTap("counter", new Dictionary<string, object?> { ["count"] = count });
Record($"RUM action recorded (count {count})");
}
private void OnReportErrorClicked(object? sender, EventArgs e)
{
try
{
throw new InvalidOperationException("Something the user did went wrong.");
}
catch (Exception exception)
{
// Reports the managed type, message and stack, so errors group by where they were
// thrown rather than by message text alone.
Datadog.TrackError(exception);
Record($"RUM error recorded: {exception.Message}");
}
}
private async void OnTrackWork(object? sender, EventArgs e)
{
// A view scope stops the view however the block is left, including on an exception.
using (Datadog.StartView("work", "Background Work"))
{
Record("started tracking a unit of work");
await Task.Delay(750);
}
Record("work finished, view stopped");
}
private async void OnShowSessionId(object? sender, EventArgs e)
{
// Worth attaching to a support ticket: it is what turns "the app was slow" into a session
// you can watch. The raw API takes a Kotlin Function1, which C# cannot express as a lambda.
var sessionId = await GlobalRumMonitor.Get().GetCurrentSessionIdAsync();
Record($"session {sessionId ?? "(none - RUM is off, or this session was sampled out)"}");
}
private async void OnTraceRequest(object? sender, EventArgs e)
{
var tracer = GlobalDatadogTracer.Get()!;
// The operation name is what APM groups by, so it has to be low cardinality - never a URL
// and never anything carrying an id.
var span = tracer.BuildSpan("http.request")!.Start()!;
try
{
span.SetTag("http.method", "GET");
using var request = new HttpRequestMessage(HttpMethod.Get, "https://example.com/api/items");
// This is the whole point of distributed tracing: the receiving service reads these
// headers and continues the same trace, so one flame graph spans both sides.
//
// The raw setter is a Kotlin (C, String, String) -> Unit, which binds as an interface -
// so without this overload every call site needs a Java.Lang.Object subclass, and
// getting it wrong sends the request untraced with nothing reported.
foreach (var header in tracer.Propagate()!.Inject(span.Context()!))
{
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
Record($"trace {span.GetTraceId()}");
Record($"propagating {request.Headers.Count()} headers");
using var client = new HttpClient();
using var response = await client.SendAsync(request);
span.SetTag("http.status_code", (long)(int)response.StatusCode);
Record($"request finished: {(int)response.StatusCode}");
}
catch (Exception exception)
{
span.SetError(exception);
Record($"request failed: {exception.GetType().Name}");
}
finally
{
span.Finish();
}
}
private void OnFailedSpan(object? sender, EventArgs e)
{
var span = GlobalDatadogTracer.Get()!.BuildSpan("checkout.submit")!.Start()!;
try
{
throw new InvalidOperationException("The cart expired before checkout completed.");
}
catch (Exception exception)
{
// Sets the error flag and message together. io.opentracing.Span had no setError at all
// in 2.x, so this was four log fields written by convention and easy to get wrong.
span.SetError(exception);
Record($"span {span.GetSpanId()} marked as failed");
}
finally
{
span.Finish();
}
}
private void OnWriteLogs(object? sender, EventArgs e)
{
Datadog.Logger.Log(DatadogLogLevel.Debug, "a debug message");
Datadog.Logger.Log(DatadogLogLevel.Info, "an info message");
Datadog.Logger.Log(DatadogLogLevel.Warn, "a warning");
Datadog.Logger.Log(DatadogLogLevel.Error, "an error");
// A logger-wide attribute goes on every subsequent entry from this logger.
Datadog.Logger.AddAttribute("screen", "main");
Record("wrote four log levels and a logger-wide attribute");
}
private void OnLogException(object? sender, EventArgs e)
{
try
{
_ = int.Parse("not a number");
}
catch (Exception exception)
{
// error.kind, error.message and error.stack are set from the exception, which is what
// makes it render as an error in the Logs UI rather than as a plain message.
Datadog.Logger.Log(
DatadogLogLevel.Error,
"parsing failed",
exception,
new Dictionary<string, object?> { ["input"] = "not a number" });
Record($"logged exception: {exception.GetType().Name}");
}
}
private async void OnOkHttpRequest(object? sender, EventArgs e)
{
Record("OkHttp GET through DatadogInterceptor...");
// No Datadog code at the call site - that is the point. The interceptor and event
// listener on Datadog.OkHttp report the request as a RUM resource and a span, and attach
// tracing headers because localhost is in the interceptor's first-party list. Execute()
// is synchronous, so it runs off the UI thread.
var outcome = await Task.Run(() =>
{
try
{
var request = new Square.OkHttp3.Request.Builder()!
.Url("http://localhost:9/items")!
.Build()!;
using var response = Datadog.OkHttp.NewCall(request)!.Execute()!;
return $"unexpected success: HTTP {response.Code()}";
}
catch (Exception exception)
{
// Nothing listens on port 9, so this is the expected path - and the failed call
// is still reported, which is exactly what you want from instrumentation.
return $"failed as expected ({exception.GetType().Name}) - still reported as a resource and span";
}
});
Record(outcome);
}
private async void OnOpenWebView(object? sender, EventArgs e) =>
await Navigation.PushAsync(new WebViewPage());
private void Record(string message)
{
activity.Insert(0, $"{DateTime.Now:HH:mm:ss} {message}");
// Keep the list short enough to stay on screen.
if (activity.Count > 12)
{
activity.RemoveAt(activity.Count - 1);
}
ActivityLabel.Text = string.Join(Environment.NewLine, activity);
}
}