azureazure-active-directoryazure-monitoringazure-monitorazure-log-analytics-workspace

Azure Monitor Java SDK -MismatchedInputException: Missing required creator property 'timespan' when using Azure Monitor Metrics SDK


I'm using the Azure Monitor Metrics SDK in my Java application to query metrics for a specific resource. However, I'm encountering a com.fasterxml.jackson.databind.exc.MismatchedInputException with the error message "Missing required creator property 'timespan' (index 0)". I would appreciate any help in understanding and resolving this issue

**

MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
    .credential(new DefaultAzureCredentialBuilder().build())
    .buildClient();
MetricsQueryResult metricsQueryResult = metricsQueryClient.queryResource(
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    Arrays.asList("requests/count"));

** The exception occurs when attempting to query metrics using the metricsQueryClient.queryResource() method. It seems that the required 'timespan' property is missing in the JSON response returned by the Azure Monitor API.

I have verified that the API call is correct and the resource ID is valid. Could you please help me understand why the 'timespan' property is missing in the response and how I can resolve this issue?

Additionally, I have already tried the following:

Thank you for your assistance in resolving this issue.


Solution

  • "Missing required creator property 'timespan' (index 0)".

    The above error suggests that the timespan parameter, which is a necessary attribute for the MetricsResponse class in the Azure Monitor Metrics SDK, is missing from the JSON response supplied by the Azure Monitor API.

    You can use the below code to get the metric values using Java.

    Code:

    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.monitor.query.MetricsQueryClient;
    import com.azure.monitor.query.MetricsQueryClientBuilder;
    import com.azure.monitor.query.models.MetricResult;
    import com.azure.monitor.query.models.MetricValue;
    import com.azure.monitor.query.models.MetricsQueryResult;
    import com.azure.monitor.query.models.TimeSeriesElement;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class App {
        public static void main(String[] args) {
            // Replace with your own resource ID and metric names
            String resourceId = "/subscriptions/sub-id/resourceGroups/v-resource-grp/providers/Microsoft.Storage/storageAccounts/venkat123";
            List<String> metricNames = Arrays.asList("UsedCapacity");
    
            MetricsQueryClient metricsQueryClient = new MetricsQueryClientBuilder()
            .credential(new DefaultAzureCredentialBuilder().build())
            .buildClient();
            MetricsQueryResult metricsQueryResult = metricsQueryClient.queryResource(resourceId,
            metricNames);
            for (MetricResult metric : metricsQueryResult.getMetrics()) {
                System.out.println("Metric name " + metric.getMetricName());
                for (TimeSeriesElement timeSeriesElement : metric.getTimeSeries()) {
                    for (MetricValue metricValue : timeSeriesElement.getValues()) {
                       System.out.println(metricValue.getTimeStamp() + " " + metricValue.getTotal());
                    }
                       for (MetricValue metricValue : timeSeriesElement.getValues()) {
                       System.out.println(metricValue.getAverage());
                    }
                }
            }
        }
    }
    

    Dependency:

     <dependency>
           <groupId>com.azure</groupId>
           <artifactId>azure-monitor-query</artifactId>
           <version>1.2.0</version>
     </dependency>
     <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-identity</artifactId>
        <version>1.7.2</version>
        <scope>compile</scope>
     </dependency>  
    

    Output:

    Metric name UsedCapacity
    2023-06-20T05:44Z null
    8849309.0
    

    enter image description here

    Reference:

    Azure Monitor Query client library for Java | Microsoft Learn