From 9910420e2b21fed63a10d3479e4e4506a91949ca Mon Sep 17 00:00:00 2001 From: Ace Eldeib Date: Mon, 1 Oct 2018 16:34:51 -0700 Subject: [PATCH] App Insights readme and sample (#2442) * add App insights java info back * Migrate example and remove JDK 8 features. * fix bugs and pom * change package, add disclaimer. --- applicationinsights/data-plane/README.md | 50 ++++++++++++ .../data-plane/samples/pom.xml | 76 +++++++++++++++++ .../ApplicationInsightsClientExample.java | 81 +++++++++++++++++++ .../ApplicationInsightsDataClientTests.java | 7 +- 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 applicationinsights/data-plane/README.md create mode 100644 applicationinsights/data-plane/samples/pom.xml create mode 100644 applicationinsights/data-plane/samples/src/main/java/com/microsoft/azure/applicationinsights/query/samples/ApplicationInsightsClientExample.java diff --git a/applicationinsights/data-plane/README.md b/applicationinsights/data-plane/README.md new file mode 100644 index 0000000000000..15885629d7289 --- /dev/null +++ b/applicationinsights/data-plane/README.md @@ -0,0 +1,50 @@ +# Azure Application Insights + +This project provides client tools or utilities in Java that make it easy to query data in [Azure Application Insights](https://azure.microsoft.com/en-us/services/application-insights/). For reference documentation on classes and models, please see the [Azure SDK for Java reference](https://docs.microsoft.com/en-us/java/api/overview/azure/?view=azure-java-stable). + +Azure Application Insights provides SDKs for telemtry collection and enables deep analytics via a [rich query language](https://docs.loganalytics.io/index). This SDK provides query access to data already stored in Application Insights. To start monitoring a Java application, take a look at the [quickstart](https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-quick-start). + +## Examples + +Please see [here](src/main/java/com/microsoft/azure/applicationinsights/query/samples) for code examples using this SDK. + +## Download + +### Latest release + +To get the binaries of the official Microsoft Azure Log Analytics SDK as distributed by Microsoft, reade for use within your project, you can use Maven. + +```xml + + com.microsoft.azure + azure-applicationinsights-query + LATEST + +``` + +## Prerequisites + +- A Java Developer Kit (JDK), v 1.7 or later +- Maven + +## Help and Issues + +If you encounter any bugs with these SDKs, please file issues via [Issues](https://github.com/Azure/azure-sdk-for-java/issues) or checkout [StackOverflow for Azure Java SDK](http://stackoverflow.com/questions/tagged/azure-java-sdk). + +## Contribute Code + +If you would like to become an active contributor to this project please follow the instructions provided in [Microsoft Azure Projects Contribution Guidelines](http://azure.github.io/guidelines.html). + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request + +## More information +- [Azure Java SDKs](https://docs.microsoft.com/java/azure/) +- If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212) + +--- + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. \ No newline at end of file diff --git a/applicationinsights/data-plane/samples/pom.xml b/applicationinsights/data-plane/samples/pom.xml new file mode 100644 index 0000000000000..9336ce554fa19 --- /dev/null +++ b/applicationinsights/data-plane/samples/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + com.microsoft.azure + azure-applicationinsights-query-sample + 1.0.0-Preview-1 + jar + Microsoft Azure SDK for Application Insights Query Sample + This package contains Microsoft Application Insights Query SDK Sample. + https://github.com/Azure/azure-sdk-for-java + + + The MIT License (MIT) + http://opensource.org/licenses/MIT + repo + + + + scm:git:https://github.com/Azure/azure-sdk-for-java + scm:git:git@github.com:Azure/azure-sdk-for-java.git + HEAD + + + UTF-8 + + 1.6.3 + + + + microsoft + Microsoft + + + + + com.microsoft.azure + azure-applicationinsights-query + ${version} + + + com.microsoft.azure + azure-client-authentication + ${runtime.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + com.microsoft.azure.management.apigeneration.LangDefinitionProcessor + + + true + true + + true + true + + + + + + \ No newline at end of file diff --git a/applicationinsights/data-plane/samples/src/main/java/com/microsoft/azure/applicationinsights/query/samples/ApplicationInsightsClientExample.java b/applicationinsights/data-plane/samples/src/main/java/com/microsoft/azure/applicationinsights/query/samples/ApplicationInsightsClientExample.java new file mode 100644 index 0000000000000..4beac5489e35c --- /dev/null +++ b/applicationinsights/data-plane/samples/src/main/java/com/microsoft/azure/applicationinsights/query/samples/ApplicationInsightsClientExample.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + */ +package com.microsoft.azure.applicationinsights.query.samples; + +import java.util.ArrayList; +import java.util.List; +import com.microsoft.azure.AzureEnvironment; +import com.microsoft.azure.applicationinsights.query.implementation.ApplicationInsightsDataClientImpl; +import com.microsoft.azure.applicationinsights.query.models.QueryBody; +import com.microsoft.azure.applicationinsights.query.models.MetricId; +import com.microsoft.azure.applicationinsights.query.models.MetricsResult; +import com.microsoft.azure.applicationinsights.query.models.MetricsResultsItem; +import com.microsoft.azure.applicationinsights.query.models.MetricsPostBodySchema; +import com.microsoft.azure.applicationinsights.query.models.MetricsPostBodySchemaParameters; +import com.microsoft.azure.applicationinsights.query.models.EventType; +import com.microsoft.azure.applicationinsights.query.models.EventsResult; +import com.microsoft.azure.applicationinsights.query.models.EventsResults; +import com.microsoft.azure.applicationinsights.query.models.QueryResults; +import com.microsoft.azure.applicationinsights.query.models.Column; +import com.microsoft.azure.credentials.ApplicationTokenCredentials; + +/** + * Basic query example + * + */ +public class ApplicationInsightsClientExample +{ + public static void main( String[] args ) + { + // ApplicationTokenCredentials work well for service principal authentication + ApplicationTokenCredentials credentials = new ApplicationTokenCredentials( + "", + "", + "", + AzureEnvironment.AZURE + ); + + // New up client. Accepts credentials, or a pre-authenticated restClient + ApplicationInsightsDataClientImpl client = new ApplicationInsightsDataClientImpl(credentials); + + // Prepare information for query + String query = "availabilityResults | take 1"; + String appId = ""; + String eventId = ""; + + // POST parameters for multiple metrics + List parameters = new ArrayList(); + parameters.add(new MetricsPostBodySchema().withId("1").withParameters(new MetricsPostBodySchemaParameters().withMetricId(MetricId.AVAILABILITY_RESULTSAVAILABILITY_PERCENTAGE))); + parameters.add(new MetricsPostBodySchema().withId("2").withParameters(new MetricsPostBodySchemaParameters().withMetricId(MetricId.AVAILABILITY_RESULTSDURATION))); + + // Execute log query + QueryResults queryResults = client.querys().execute(appId, new QueryBody().withQuery(query)); + + // Metrics + MetricsResult metricResultSingle = client.metrics().get(appId, MetricId.AVAILABILITY_RESULTSAVAILABILITY_PERCENTAGE); + List metricResultMultiple = client.metrics().getMultiple(appId, parameters); + Object metadata = client.metrics().getMetadata(appId); + + // Events + EventsResults eventsResultByType = client.events().getByType(appId, EventType.AVAILABILITY_RESULTS); + EventsResults eventsResult = client.events().get(appId, EventType.AVAILABILITY_RESULTS, eventId); + + // Process and print results + List row = queryResults.tables().get(0).rows().get(0); + List columnNames = queryResults + .tables() + .get(0) + .columns(); + + for (int i = 0; i < row.size(); i++){ + System.out.println("The value of " + columnNames.get(i).name() + " is " + row.get(i)); + } + + System.out.println(metricResultMultiple.get(0).body().value().additionalProperties()); + + } +} \ No newline at end of file diff --git a/applicationinsights/data-plane/src/test/java/com/microsoft/azure/applicationinsights/query/ApplicationInsightsDataClientTests.java b/applicationinsights/data-plane/src/test/java/com/microsoft/azure/applicationinsights/query/ApplicationInsightsDataClientTests.java index 037e31c90b2cc..a2bcf9928d39b 100644 --- a/applicationinsights/data-plane/src/test/java/com/microsoft/azure/applicationinsights/query/ApplicationInsightsDataClientTests.java +++ b/applicationinsights/data-plane/src/test/java/com/microsoft/azure/applicationinsights/query/ApplicationInsightsDataClientTests.java @@ -1,3 +1,9 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + */ package com.microsoft.azure.applicationinsights.query; import java.util.ArrayList; @@ -59,7 +65,6 @@ public void canGetMetric() { MetricsResult metricResult = applicationInsightsClient.metrics().get(appId, MetricId.AVAILABILITY_RESULTSAVAILABILITY_PERCENTAGE); // Validate properties Assert.assertNotNull(metricResult.value().additionalProperties()); - System.out.println(metricResult.value().additionalProperties().keySet()); Assert.assertNotNull(metricResult.value().start()); Assert.assertTrue(metricResult.value().start() instanceof DateTime); }