Search in sources :

Example 1 with RequestData

use of com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData in project ApplicationInsights-Java by microsoft.

the class TelemetryUtil method getProperties.

// TODO (trask) Azure SDK: can we move getProperties up to MonitorDomain, or if not, a common
// interface?
public static Map<String, String> getProperties(MonitorDomain data) {
    if (data instanceof AvailabilityData) {
        AvailabilityData availabilityData = (AvailabilityData) data;
        Map<String, String> properties = availabilityData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            availabilityData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof MessageData) {
        MessageData messageData = (MessageData) data;
        Map<String, String> properties = messageData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            messageData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof MetricsData) {
        MetricsData metricsData = (MetricsData) data;
        Map<String, String> properties = metricsData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            metricsData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof PageViewData) {
        PageViewData pageViewData = (PageViewData) data;
        Map<String, String> properties = pageViewData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            pageViewData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof PageViewPerfData) {
        PageViewPerfData pageViewPerfData = (PageViewPerfData) data;
        Map<String, String> properties = pageViewPerfData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            pageViewPerfData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof RemoteDependencyData) {
        RemoteDependencyData remoteDependencyData = (RemoteDependencyData) data;
        Map<String, String> properties = remoteDependencyData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            remoteDependencyData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof RequestData) {
        RequestData requestData = (RequestData) data;
        Map<String, String> properties = requestData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            requestData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof TelemetryEventData) {
        TelemetryEventData eventData = (TelemetryEventData) data;
        Map<String, String> properties = eventData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            eventData.setProperties(properties);
        }
        return properties;
    } else if (data instanceof TelemetryExceptionData) {
        TelemetryExceptionData exceptionData = (TelemetryExceptionData) data;
        Map<String, String> properties = exceptionData.getProperties();
        if (properties == null) {
            properties = new HashMap<>();
            exceptionData.setProperties(properties);
        }
        return properties;
    } else {
        throw new IllegalArgumentException("Unexpected type: " + data.getClass().getName());
    }
}
Also used : AvailabilityData(com.microsoft.applicationinsights.agent.internal.exporter.models.AvailabilityData) PageViewPerfData(com.microsoft.applicationinsights.agent.internal.exporter.models.PageViewPerfData) HashMap(java.util.HashMap) MessageData(com.microsoft.applicationinsights.agent.internal.exporter.models.MessageData) RemoteDependencyData(com.microsoft.applicationinsights.agent.internal.exporter.models.RemoteDependencyData) PageViewData(com.microsoft.applicationinsights.agent.internal.exporter.models.PageViewData) MetricsData(com.microsoft.applicationinsights.agent.internal.exporter.models.MetricsData) RequestData(com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData) HashMap(java.util.HashMap) Map(java.util.Map) TelemetryExceptionData(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionData) TelemetryEventData(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)

Example 2 with RequestData

use of com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData in project ApplicationInsights-Java by microsoft.

the class Exporter method exportRequest.

private void exportRequest(SpanData span) {
    TelemetryItem telemetry = new TelemetryItem();
    RequestData data = new RequestData();
    telemetryClient.initRequestTelemetry(telemetry, data);
    Attributes attributes = span.getAttributes();
    long startEpochNanos = span.getStartEpochNanos();
    float samplingPercentage = getSamplingPercentage(span.getSpanContext().getTraceState());
    // set standard properties
    data.setId(span.getSpanId());
    setTime(telemetry, startEpochNanos);
    setSampleRate(telemetry, samplingPercentage);
    setExtraAttributes(telemetry, data, attributes);
    addLinks(data, span.getLinks());
    String operationName = getOperationName(span);
    telemetry.getTags().put(ContextTagKeys.AI_OPERATION_NAME.toString(), operationName);
    telemetry.getTags().put(ContextTagKeys.AI_OPERATION_ID.toString(), span.getTraceId());
    // see behavior specified at https://github.com/microsoft/ApplicationInsights-Java/issues/1174
    String aiLegacyParentId = span.getAttributes().get(AI_LEGACY_PARENT_ID_KEY);
    if (aiLegacyParentId != null) {
        // this was the real (legacy) parent id, but it didn't fit span id format
        telemetry.getTags().put(ContextTagKeys.AI_OPERATION_PARENT_ID.toString(), aiLegacyParentId);
    } else if (span.getParentSpanContext().isValid()) {
        telemetry.getTags().put(ContextTagKeys.AI_OPERATION_PARENT_ID.toString(), span.getParentSpanContext().getSpanId());
    }
    String aiLegacyRootId = span.getAttributes().get(AI_LEGACY_ROOT_ID_KEY);
    if (aiLegacyRootId != null) {
        telemetry.getTags().put("ai_legacyRootID", aiLegacyRootId);
    }
    // set request-specific properties
    data.setName(operationName);
    data.setDuration(FormattedDuration.fromNanos(span.getEndEpochNanos() - startEpochNanos));
    data.setSuccess(getSuccess(span));
    String httpUrl = getHttpUrlFromServerSpan(attributes);
    if (httpUrl != null) {
        data.setUrl(httpUrl);
    }
    Long httpStatusCode = attributes.get(SemanticAttributes.HTTP_STATUS_CODE);
    if (httpStatusCode == null) {
        httpStatusCode = attributes.get(SemanticAttributes.RPC_GRPC_STATUS_CODE);
    }
    if (httpStatusCode != null) {
        data.setResponseCode(Long.toString(httpStatusCode));
    } else {
        data.setResponseCode("0");
    }
    String locationIp = attributes.get(SemanticAttributes.HTTP_CLIENT_IP);
    if (locationIp == null) {
        // only use net.peer.ip if http.client_ip is not available
        locationIp = attributes.get(SemanticAttributes.NET_PEER_IP);
    }
    if (locationIp != null) {
        telemetry.getTags().put(ContextTagKeys.AI_LOCATION_IP.toString(), locationIp);
    }
    data.setSource(getSource(attributes, span.getSpanContext()));
    String sessionId = attributes.get(AI_SESSION_ID_KEY);
    if (sessionId != null) {
        // this is only used by the 2.x web interop bridge for
        // ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getSession().setId()
        telemetry.getTags().put(ContextTagKeys.AI_SESSION_ID.toString(), sessionId);
    }
    String deviceOs = attributes.get(AI_DEVICE_OS_KEY);
    if (deviceOs != null) {
        // this is only used by the 2.x web interop bridge for
        // ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getDevice().setOperatingSystem()
        telemetry.getTags().put(ContextTagKeys.AI_DEVICE_OS.toString(), deviceOs);
    }
    String deviceOsVersion = attributes.get(AI_DEVICE_OS_VERSION_KEY);
    if (deviceOsVersion != null) {
        // this is only used by the 2.x web interop bridge for
        // ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getDevice().setOperatingSystemVersion()
        telemetry.getTags().put(ContextTagKeys.AI_DEVICE_OS_VERSION.toString(), deviceOsVersion);
    }
    // TODO(trask)? for batch consumer, enqueuedTime should be the average of this attribute
    // across all links
    Long enqueuedTime = attributes.get(AZURE_SDK_ENQUEUED_TIME);
    if (enqueuedTime != null) {
        long timeSinceEnqueuedMillis = Math.max(0L, NANOSECONDS.toMillis(span.getStartEpochNanos()) - SECONDS.toMillis(enqueuedTime));
        if (data.getMeasurements() == null) {
            data.setMeasurements(new HashMap<>());
        }
        data.getMeasurements().put("timeSinceEnqueued", (double) timeSinceEnqueuedMillis);
    }
    Long timeSinceEnqueuedMillis = attributes.get(KAFKA_RECORD_QUEUE_TIME_MS);
    if (timeSinceEnqueuedMillis != null) {
        if (data.getMeasurements() == null) {
            data.setMeasurements(new HashMap<>());
        }
        data.getMeasurements().put("timeSinceEnqueued", (double) timeSinceEnqueuedMillis);
    }
    // export
    telemetryClient.trackAsync(telemetry);
    exportEvents(span, operationName, samplingPercentage);
}
Also used : TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) RequestData(com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes)

Example 3 with RequestData

use of com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData in project ApplicationInsights-Java by microsoft.

the class BytecodeUtilImpl method trackRequest.

@Override
public void trackRequest(String id, String name, URL url, Date timestamp, @Nullable Long duration, String responseCode, boolean success, String source, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics, String instrumentationKey) {
    if (Strings.isNullOrEmpty(name)) {
        return;
    }
    TelemetryItem telemetry = new TelemetryItem();
    RequestData data = new RequestData();
    TelemetryClient.getActive().initRequestTelemetry(telemetry, data);
    if (id == null) {
        data.setId(AiLegacyPropagator.generateSpanId());
    } else {
        data.setId(id);
    }
    data.setName(name);
    if (url != null) {
        data.setUrl(url.toString());
    }
    if (duration != null) {
        data.setDuration(FormattedDuration.fromMillis(duration));
    }
    data.setResponseCode(responseCode);
    data.setSuccess(success);
    data.setSource(source);
    data.setMeasurements(metrics);
    if (!properties.isEmpty()) {
        Map<String, String> existingProperties = data.getProperties();
        if (existingProperties == null) {
            data.setProperties(properties);
        } else {
            existingProperties.putAll(properties);
        }
    }
    if (timestamp != null) {
        telemetry.setTime(FormattedTime.offSetDateTimeFromDate(timestamp));
    } else {
        telemetry.setTime(FormattedTime.offSetDateTimeFromNow());
    }
    selectivelySetTags(telemetry, tags);
    if (instrumentationKey != null) {
        telemetry.setInstrumentationKey(instrumentationKey);
    }
    track(telemetry, true);
}
Also used : TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) RequestData(com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData)

Example 4 with RequestData

use of com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData in project ApplicationInsights-Java by microsoft.

the class QuickPulseTestBase method createRequestTelemetry.

public static TelemetryItem createRequestTelemetry(String name, Date timestamp, long durationMillis, String responseCode, boolean success) {
    TelemetryItem telemetry = new TelemetryItem();
    RequestData data = new RequestData();
    TelemetryClient.createForTest().initRequestTelemetry(telemetry, data);
    Map<String, String> properties = new HashMap<>();
    properties.put("customProperty", "customValue");
    data.setProperties(properties);
    data.setName(name);
    data.setDuration(FormattedDuration.fromMillis(durationMillis));
    data.setResponseCode(responseCode);
    data.setSuccess(success);
    data.setUrl("foo");
    telemetry.setTime(FormattedTime.offSetDateTimeFromDate(timestamp));
    return telemetry;
}
Also used : HashMap(java.util.HashMap) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) RequestData(com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData)

Aggregations

RequestData (com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData)4 TelemetryItem (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem)3 HashMap (java.util.HashMap)2 AvailabilityData (com.microsoft.applicationinsights.agent.internal.exporter.models.AvailabilityData)1 MessageData (com.microsoft.applicationinsights.agent.internal.exporter.models.MessageData)1 MetricsData (com.microsoft.applicationinsights.agent.internal.exporter.models.MetricsData)1 PageViewData (com.microsoft.applicationinsights.agent.internal.exporter.models.PageViewData)1 PageViewPerfData (com.microsoft.applicationinsights.agent.internal.exporter.models.PageViewPerfData)1 RemoteDependencyData (com.microsoft.applicationinsights.agent.internal.exporter.models.RemoteDependencyData)1 TelemetryEventData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)1 TelemetryExceptionData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionData)1 Attributes (io.opentelemetry.api.common.Attributes)1 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)1 Map (java.util.Map)1