Add Tracking to HTTP Requests
Semi-automatic HTTP Tracking
The HttpMessageHandler handles all the tracking and error handling. It can also include other inner handlers if you are already using a custom HttpMessageHandler for other purposes, such as for logging.
To add semi-automatic tracking, instantiate a HttpClient and pass the HttpRequestTrackerHandler:
var client = new HttpClient(new HttpRequestTrackerHandler());
Then, all the requests sent using the client will be already instrumented:
response = await client.GetAsync(uri);
HttpMessageHandler passed to the HttpClient (for example, adding a logging handler), you must instantiate HttpRequestTrackerHandler and pass the existing handler to the constructor:
var loggingHandler = new MyLoggingHandler();
var client = new HttpClient(new HttpRequestTrackerHandler(loggingHandler));
Manual HTTP Tracking
You can manually report a network request using the AppDynamics.Agent.HttpRequestTracker class.
The example below uses HttpRequestTracker with the System.Net.Http.HttpClient class. The tracker object synchronously captures and reports the network request as well as any network errors.
using AppDynamics.Agent;
...
public async Task<string> Fetch(Uri uri) {
var client = new HttpClient();
// Create AppDynamics Tracker
var tracker = HttpRequestTracker.Create(uri);
// Add AppDynamics Server Correlation Headers
foreach (var header in ServerCorrelationHeaders.Generate) {
// Each header could have multiple values
foreach (var value in header.Value) {
client.DefaultRequestHeaders.Add(header.Key, value);
}
}
HttpResponseMessage response = null;
try {
response = await client.GetAsync(uri);
} catch (Exception ex) {
// Capture any network errors.
tracker.Exception = ex;
tracker.ReportDone();
throw ex; //you decide to throw it or not
}
if (!response.Equals(null)) {
// Capture request information such as the
// status code, status message, and headers.
tracker.ResponseCode = (int)response.StatusCode;
tracker.StatusLine = response.ReasonPhrase;
tracker.ResponseHeaderFields = response.Headers;
tracker.ReportDone();
return await response.Content.ReadAsStringAsync();
}
return null;
}
Attach a Custom User Data Property to a Network Request
You can attach a HttpRequestTracker custom user data property to a specific network request by adding user data to HttpRequestTracker.
public HttpRequestTracker withUserData(String key, String value)
public HttpRequestTracker withUserLong(String key, Long value)
public HttpRequestTracker withUserBoolean(String key, Boolean value)
public HttpRequestTracker withUserDouble(String key, Double value)
public HttpRequestTracker withUserDate(String key, Date value)
Example
public byte[] sendRequest(URL url) throws HttpException {
HttpRequestTracker tracker = Instrumentation.beginHttpRequest(url);
try {
// implementation omitted
tracker.withResponseCode(theResponseCode)
.withResponseHeaderFields(theResponseHeaderFields)
.withUserData("key", "value")
.withUserLong("number", 100)
.withUserBoolean("boolean", true)
.withUserDouble("double", 1.1234)
.withUserDate("date", 1609488000)
.reportDone();
return responseBody;
} catch (UnderlyingException e) {
tracker.withException(e)
.reportDone();
throw new HttpException(e);
}
}