ApplicationInsights-Java
ApplicationInsights-Java copied to clipboard
Add TrackAvailability method
It would be nice to have the TrackAvailability method in Java. Currently it's only available in .NET.
Can you let us know what your use case is for TrackAvailability?
TrackAvailability is needed for cases when you have to write Custom Track Availability Tests. I have a microservice architecture using gRPC and I want to log availability metrics for this services.
I've made a AvailabilityTelemetry class that apparently it's working.
Please note that I spent only some time on it and may need some care.
import com.microsoft.applicationinsights.internal.schemav2.AvailabilityData;
import com.microsoft.applicationinsights.internal.util.LocalStringsUtils;
import com.microsoft.applicationinsights.internal.util.Sanitizer;
import com.microsoft.applicationinsights.telemetry.BaseSampleSourceTelemetry;
import com.microsoft.applicationinsights.telemetry.Duration;
import java.util.Date;
import java.util.concurrent.ConcurrentMap;
public final class AvailabilityTelemetry extends BaseSampleSourceTelemetry<AvailabilityData> {
private Double samplingPercentage;
private final AvailabilityData data;
/**
* Envelope Name for this telemetry.
*/
public static final String ENVELOPE_NAME = "Availability";
/**
* Base Type for this telemetry.
*/
public static final String BASE_TYPE = "AvailabilityData";
/**
* Initializes a new instance of the HttpAvailabilityTelemetry class.
*/
public AvailabilityTelemetry() {
this.data = new AvailabilityData();
initialize(this.data.getProperties());
setId(LocalStringsUtils.generateRandomIntegerId());
// Setting mandatory fields.
setTimestamp(new Date());
setSuccess(true);
}
/**
* Initializes a new instance of the HttpAvailabilityTelemetry class with the given name,
* time stamp, duration, HTTP response code and success property values.
* @param name A user-friendly name for the request.
* @param timestamp The time of the request.
* @param duration The duration, in milliseconds, of the request processing.
* @param responseCode The HTTP response code.
* @param success 'true' if the request was a success, 'false' otherwise.
*/
public AvailabilityTelemetry(String name, long duration, String runLocation, String message,
boolean success, ConcurrentMap<String, Double> measurements,
ConcurrentMap<String, String> properties) {
this.data = new AvailabilityData();
this.data.setProperties(properties);
this.data.setMeasurements(measurements);
initialize(this.data.getProperties());
setId(LocalStringsUtils.generateRandomIntegerId());
setTimestamp(new Date());
setName(name);
setRunLocation(runLocation);
setDuration(new Duration(duration));
setSuccess(success);
}
@Override
public int getVer() {
return getData().getVer();
}
/**
* Gets a map of application-defined request metrics.
* @return The map of metrics
*/
public ConcurrentMap<String, Double> getMetrics() {
return data.getMeasurements();
}
/**
* Sets the StartTime. Uses the default behavior and sets the property on the 'data' start time
* @param timestamp he timestamp as Date.
*/
@Override
public void setTimestamp(Date timestamp) {
if (timestamp == null) {
timestamp = new Date();
}
super.setTimestamp(timestamp);
}
/**
* Gets or human-readable name of the requested page.
* @return A human-readable name
*/
public String getName() {
return data.getName();
}
/**
* Sets or human-readable name of the requested page.
* @param name A human-readable name
*/
public void setName(String name) {
data.setName(name);
}
/**
* Gets or human-readable name of the run location.
* @return A human-readable name
*/
public String getRunLocation() {
return data.getRunLocation();
}
/**
* Sets or human-readable name of the run location.
* @param name A human-readable name
*/
public void setRunLocation(String runLocation) {
data.setRunLocation(runLocation);
}
/**
* Gets the unique identifier of the request.
* @return Unique identifier
*/
public String getId() {
return data.getId();
}
/**
* Sets the unique identifier of the request.
* @param id Unique identifier
*/
public void setId(String id) {
data.setId(id);
}
/**
* Gets a value indicating whether application handled the request successfully.
* @return Success indication
*/
public boolean isSuccess() {
return data.getSuccess();
}
/**
* Sets a value indicating whether application handled the request successfully.
* @param success Success indication
*/
public void setSuccess(boolean success) {
data.setSuccess(success);
}
/**
* Gets the amount of time it took the application to handle the request.
* @return Amount of time in milliseconds
*/
public Duration getDuration() {
return data.getDuration();
}
/**
* Sets the amount of time it took the application to handle the request.
* @param duration Amount of time in captured in a {@link com.microsoft.applicationinsights.telemetry.Duration}.
*/
public void setDuration(Duration duration) {
data.setDuration(duration);
}
@Override
public Double getSamplingPercentage() {
return samplingPercentage;
}
@Override
public void setSamplingPercentage(Double samplingPercentage) {
this.samplingPercentage = samplingPercentage;
}
@Override
@Deprecated
protected void additionalSanitize() {
data.setName(Sanitizer.sanitizeName(data.getName()));
data.setId(Sanitizer.sanitizeName(data.getId()));
Sanitizer.sanitizeMeasurements(getMetrics());
}
@Override
protected AvailabilityData getData() {
return data;
}
@Override
public String getEnvelopName() {
return ENVELOPE_NAME;
}
@Override
public String getBaseTypeName() {
return BASE_TYPE;
}
}
How you should use it:
AvailabilityTelemetry availabilityTelemetry =
new AvailabilityTelemetry("some-api-name", longDurationMs, "West Europe",
"some message describing this availability check", booleanSuccess, measurements, props);
telemetryClient.track(availabilityTelemetry);
Hi, any updates on that? @ejbp Could you confirm this is still working? I tried to implement it but no luck so far.
The supported trackEvent() is working though.
Any Updates on getting this capability included in Java on its roadmap?
Thanks in advance.
Hi, any updates on that? @ejbp Could you confirm this is still working? I tried to implement it but no luck so far.
The supported trackEvent() is working though.
I missed your message. We have it up and running on production, so Yes, I'm still using this workaround.