Exemplo n.º 1
0
    def query(
        self, log_query: str, *, timespan: str = "PT24H", raw: bool = False
    ) -> Any:
        """
        Perform an Application Insights query

        Queries should be well formed Kusto Queries.
        Ref https://docs.microsoft.com/en-us/azure/data-explorer/kql-quick-reference

        :param str log_query: Query to send to Application Insights
        :param str timespan: ISO 8601 duration format
        :param bool raw: Do not simplify the data result
        """
        creds, _ = get_azure_cli_credentials(
            resource="https://api.applicationinsights.io"
        )
        client = ApplicationInsightsDataClient(creds)

        app_id = self.onefuzz.info.get().insights_appid
        if app_id is None:
            raise Exception("instance does not have an insights_appid")
        raw_data = client.query.execute(
            app_id, body=QueryBody(query=log_query, timespan=timespan)
        )
        if "error" in raw_data.additional_properties:
            raise Exception(
                "Error performing query: %s" % raw_data.additional_properties["error"]
            )
        if raw:
            return raw_data
        return self._convert(raw_data)
Exemplo n.º 2
0
    def test_query(self):
        query = 'requests | take 10'
        application = 'DEMO_APP'
        result = self.client.query.execute(application, QueryBody(query=query))
        # All queries should return at least a table.
        self.assertGreaterEqual(len(result.tables), 1)

        # Request table schema has 37 columns.
        self.assertEqual(len(result.tables[0].columns), 37)

        # The application should contain enough data to retrieve 10 rows, as asked
        self.assertEqual(len(result.tables[0].rows), 10)
        self.assertIs(type(result.tables[0].rows[0][7]), float)
Exemplo n.º 3
0
    def query(
        self,
        log_query: str,
        *,
        timespan: Optional[str] = DAY_TIMESPAN,
        raw: bool = False,
    ) -> Any:
        """
        Perform an Application Insights query

        Queries should be well formed Kusto Queries.
        Ref https://docs.microsoft.com/en-us/azure/data-explorer/kql-quick-reference

        :param str log_query: Query to send to Application Insights
        :param str timespan: ISO 8601 duration format
        :param bool raw: Do not simplify the data result
        """
        if self._app_id is None:
            self._app_id = self.onefuzz.info.get().insights_appid
        if self._app_id is None:
            raise Exception("instance does not have an insights_appid")
        if self._client is None:

            creds = AzureIdentityCredentialAdapter(
                AzureCliCredential(), resource_id="https://api.applicationinsights.io"
            )
            self._client = ApplicationInsightsDataClient(creds)

        self.logger.debug("query: %s", log_query)
        raw_data = self._client.query.execute(
            self._app_id, body=QueryBody(query=log_query, timespan=timespan)
        )
        if "error" in raw_data.additional_properties:
            raise Exception(
                "Error performing query: %s" % raw_data.additional_properties["error"]
            )
        if raw:
            return raw_data
        return self._convert(raw_data)