You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/10/28 08:37:30 UTC

[GitHub] [airflow] feluelle commented on a change in pull request #10273: Azure Log Analytics Workspace Task Logs Ingestion

feluelle commented on a change in pull request #10273:
URL: https://github.com/apache/airflow/pull/10273#discussion_r513263047



##########
File path: airflow/providers/microsoft/azure/hooks/laws.py
##########
@@ -0,0 +1,118 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+"""
+This module contains integration with Azure LAWS.
+
+"""
+from datetime import datetime
+
+
+import hashlib
+import hmac
+import base64
+import requests
+
+from airflow.hooks.base_hook import BaseHook
+
+
+class LawsHook(BaseHook):
+    """
+    Interacts with Azure LAWS through api.
+
+    :param remote_conn_id: Not Used
+    :type remote_conn_id: str
+    :param account_id: Laws Account Id
+    :type account_id: str
+    :param access_key: Access Key
+    :type access_key: str
+    :param table_name: <Table Name>_CL
+    :type table_name: str
+    """
+
+    def __init__(self, remote_conn_id, account_id, access_key, table_name):
+        super().__init__()
+        self.conn_id = remote_conn_id
+        self.account_id = account_id
+        self.access_key = access_key
+        self.table_name = table_name
+
+    def build_signature(self, date, content_length, method, content_type, resource):
+        """Builds signature for Azure HTTP Data Collector API"""
+        x_headers = 'x-ms-date:' + date
+        string_to_hash = (
+            method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
+        )  # pylint: disable=line-too-long
+        bytes_to_hash = bytes(string_to_hash, encoding="utf-8")
+        decoded_key = base64.b64decode(self.access_key)
+        encoded_hash = base64.b64encode(
+            hmac.new(
+                decoded_key, bytes_to_hash, digestmod=hashlib.sha256
+            ).digest()  # pylint: disable=line-too-long
+        ).decode()
+        authorization = "SharedKey {}:{}".format(self.account_id, encoded_hash)
+        return authorization
+
+    @staticmethod
+    def _clean_execution_date(execution_date: datetime) -> str:
+        """
+        Clean up an execution date so that it is safe to query in log analytics
+        by formatting it correctly
+
+        :param execution_date: execution date of the dag run.
+        """
+        return execution_date.strftime("%Y-%m-%d %H:%M:%S")
+
+    def post_log(self, log, ti, ssl_verify=True):
+        """
+        Post data to Azure Log Analytics
+        """
+        execution_date = self._clean_execution_date(ti.execution_date)
+        body = {
+            "dag_id": ti.dag_id,
+            "task_id": ti.task_id,
+            "execution_date": execution_date,
+            "try_number": ti.try_number,
+            "part": 1,
+            "raw_data": log,
+        }
+        # TODO: Break content into said size like 20M.
+        custom_table_name = self.table_name
+        method = 'POST'
+        content_type = 'application/json'
+        resource = '/api/logs'
+        rfc1123date = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
+        content_length = len(body)
+        signature = self.build_signature(rfc1123date, content_length, method, content_type, resource)
+        uri = (
+            'https://' + self.account_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'
+        )
+
+        headers = {
+            'content-type': content_type,
+            'Authorization': signature,
+            'Log-Type': custom_table_name,
+            'x-ms-date': rfc1123date,
+        }
+        # TODO: Option to print to console instead API.
+        try:
+            response = requests.post(uri, data=body, headers=headers, verify=ssl_verify)
+            response.raise_for_status()

Review comment:
       Did that work? Or haven't you tried that yet?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org