Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TrackAvailability method #1099

Closed
DCampodonico opened this issue Oct 16, 2019 · 6 comments · Fixed by #2447
Closed

Add TrackAvailability method #1099

DCampodonico opened this issue Oct 16, 2019 · 6 comments · Fixed by #2447

Comments

@DCampodonico
Copy link

It would be nice to have the TrackAvailability method in Java. Currently it's only available in .NET.

@littleaj littleaj added this to the future milestone Oct 16, 2019
@trask
Copy link
Member

trask commented Jun 10, 2020

Can you let us know what your use case is for TrackAvailability?

@DCampodonico
Copy link
Author

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.

@ejbp
Copy link

ejbp commented Sep 20, 2020

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);

@mordeng
Copy link

mordeng commented Dec 10, 2020

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.

@Luisrod12
Copy link

Any Updates on getting this capability included in Java on its roadmap?

Thanks in advance.

@ejbp
Copy link

ejbp commented Jul 7, 2021

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.

@ghost ghost added the Status: Fixed label Aug 18, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants