From 46368c10d124e258a28afd28b37e989767a0b5ef Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Mon, 13 Mar 2023 00:45:04 +0100 Subject: [PATCH 1/5] Add datadog connection to the UI connections list --- airflow/providers/datadog/hooks/datadog.py | 5 +++++ airflow/providers/datadog/provider.yaml | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/airflow/providers/datadog/hooks/datadog.py b/airflow/providers/datadog/hooks/datadog.py index 14f2b664d015a..8ace8798b7bb6 100644 --- a/airflow/providers/datadog/hooks/datadog.py +++ b/airflow/providers/datadog/hooks/datadog.py @@ -39,6 +39,11 @@ class DatadogHook(BaseHook, LoggingMixin): :param datadog_conn_id: The connection to datadog, containing metadata for api keys. """ + conn_name_attr = "datadog_conn_id" + default_conn_name = "datadog_default" + conn_type = "datadog" + hook_name = "DataDog" + def __init__(self, datadog_conn_id: str = "datadog_default") -> None: super().__init__() conn = self.get_connection(datadog_conn_id) diff --git a/airflow/providers/datadog/provider.yaml b/airflow/providers/datadog/provider.yaml index 3bd1734a53aae..f933708d035b6 100644 --- a/airflow/providers/datadog/provider.yaml +++ b/airflow/providers/datadog/provider.yaml @@ -51,3 +51,7 @@ hooks: - integration-name: Datadog python-modules: - airflow.providers.datadog.hooks.datadog + +connection-types: + - hook-class-name: airflow.providers.datadog.hooks.datadog.DatadogHook + connection-type: datadog From 2323ec7cefc7da4c41f4973b4bfda621d205f81a Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Mon, 13 Mar 2023 01:09:33 +0100 Subject: [PATCH 2/5] customize Datadog connection UI --- airflow/providers/datadog/hooks/datadog.py | 28 ++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/airflow/providers/datadog/hooks/datadog.py b/airflow/providers/datadog/hooks/datadog.py index 8ace8798b7bb6..f35964d13614c 100644 --- a/airflow/providers/datadog/hooks/datadog.py +++ b/airflow/providers/datadog/hooks/datadog.py @@ -42,7 +42,7 @@ class DatadogHook(BaseHook, LoggingMixin): conn_name_attr = "datadog_conn_id" default_conn_name = "datadog_default" conn_type = "datadog" - hook_name = "DataDog" + hook_name = "Datadog" def __init__(self, datadog_conn_id: str = "datadog_default") -> None: super().__init__() @@ -77,7 +77,7 @@ def send_metric( interval: int | None = None, ) -> dict[str, Any]: """ - Sends a single datapoint metric to DataDog + Sends a single datapoint metric to Datadog :param metric_name: The name of the metric :param datapoint: A single integer or float related to the metric @@ -157,3 +157,27 @@ def post_event( self.validate_response(response) return response + + @staticmethod + def get_connection_form_widgets() -> dict[str, Any]: + """Returns connection widgets to add to connection form""" + from flask_appbuilder.fieldwidgets import BS3TextFieldWidget + from flask_babel import lazy_gettext + from wtforms import StringField + + return { + "api_host": StringField(lazy_gettext("Datadog API endpoint"), widget=BS3TextFieldWidget()), + "api_key": StringField(lazy_gettext("Datadog API key"), widget=BS3TextFieldWidget()), + "app_key": StringField(lazy_gettext("Datadog application key"), widget=BS3TextFieldWidget()), + "source_type_name": StringField( + lazy_gettext("Datadog source type name"), widget=BS3TextFieldWidget() + ), + } + + @staticmethod + def get_ui_field_behaviour() -> dict[str, Any]: + """Returns custom field behaviour""" + return { + "hidden_fields": ["schema", "login", "password", "port", "extra"], + "relabeling": {"host": "Events host name"}, + } From f739c2add6a3d7c8bda2b7af628ef2a0713161e8 Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Mon, 13 Mar 2023 02:02:23 +0100 Subject: [PATCH 3/5] Add datadog connection documentation --- .../connections/datadog.rst | 46 +++++++++++++++++++ .../index.rst | 6 +++ 2 files changed, 52 insertions(+) create mode 100644 docs/apache-airflow-providers-datadog/connections/datadog.rst diff --git a/docs/apache-airflow-providers-datadog/connections/datadog.rst b/docs/apache-airflow-providers-datadog/connections/datadog.rst new file mode 100644 index 0000000000000..dad657bfa8a00 --- /dev/null +++ b/docs/apache-airflow-providers-datadog/connections/datadog.rst @@ -0,0 +1,46 @@ + .. 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. + +.. _howto/connection:kubernetes: + +Datadog Connection +============================= + +The Datadog Connection type enables integrations with the Datadog API client. + + +Default Connection IDs +---------------------- + +Hooks and sensors related to Datadog use ``datadog_default`` by default. + +Configuring the Connection +-------------------------- +Host + Host name to associate with the sent events. + +Extra + Specify the extra parameters (as json dictionary) that can be used in Datadog + connection. + + ``api_host``: Datadog `API endpoint `__ + + ``api_key``: Datadog `API key `__ + + ``app_key``: Datadog `application key `__ + + ``source_type_name``: Datadog `source type name `__ (defaults to my_apps). diff --git a/docs/apache-airflow-providers-datadog/index.rst b/docs/apache-airflow-providers-datadog/index.rst index 9581b5b587691..72aa448133a1d 100644 --- a/docs/apache-airflow-providers-datadog/index.rst +++ b/docs/apache-airflow-providers-datadog/index.rst @@ -22,6 +22,12 @@ Content ------- +.. toctree:: + :maxdepth: 1 + :caption: Guides + + Connection types + .. toctree:: :maxdepth: 1 :caption: References From bc9f7ca3929d6fe2ad972e0ffecb93857b8ec53e Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Tue, 14 Mar 2023 16:34:34 +0100 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Josh Fell <48934154+josh-fell@users.noreply.github.com> --- airflow/providers/datadog/hooks/datadog.py | 8 ++++---- .../connections/datadog.rst | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/airflow/providers/datadog/hooks/datadog.py b/airflow/providers/datadog/hooks/datadog.py index f35964d13614c..a8bf366dd487f 100644 --- a/airflow/providers/datadog/hooks/datadog.py +++ b/airflow/providers/datadog/hooks/datadog.py @@ -166,11 +166,11 @@ def get_connection_form_widgets() -> dict[str, Any]: from wtforms import StringField return { - "api_host": StringField(lazy_gettext("Datadog API endpoint"), widget=BS3TextFieldWidget()), - "api_key": StringField(lazy_gettext("Datadog API key"), widget=BS3TextFieldWidget()), - "app_key": StringField(lazy_gettext("Datadog application key"), widget=BS3TextFieldWidget()), + "api_host": StringField(lazy_gettext("API endpoint"), widget=BS3TextFieldWidget()), + "api_key": StringField(lazy_gettext("API key"), widget=BS3TextFieldWidget()), + "app_key": StringField(lazy_gettext("Application key"), widget=BS3TextFieldWidget()), "source_type_name": StringField( - lazy_gettext("Datadog source type name"), widget=BS3TextFieldWidget() + lazy_gettext("Source type name"), widget=BS3TextFieldWidget() ), } diff --git a/docs/apache-airflow-providers-datadog/connections/datadog.rst b/docs/apache-airflow-providers-datadog/connections/datadog.rst index dad657bfa8a00..dccc1caf68f03 100644 --- a/docs/apache-airflow-providers-datadog/connections/datadog.rst +++ b/docs/apache-airflow-providers-datadog/connections/datadog.rst @@ -34,7 +34,7 @@ Host Host name to associate with the sent events. Extra - Specify the extra parameters (as json dictionary) that can be used in Datadog + Specify the extra parameters (as a JSON dictionary) that can be used in Datadog connection. ``api_host``: Datadog `API endpoint `__ From 502d0384fd1afd7394e645ef3ad71d9052280f99 Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Wed, 15 Mar 2023 00:20:01 +0100 Subject: [PATCH 5/5] Fix static checks --- airflow/providers/datadog/hooks/datadog.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/airflow/providers/datadog/hooks/datadog.py b/airflow/providers/datadog/hooks/datadog.py index a8bf366dd487f..670a81594ec8e 100644 --- a/airflow/providers/datadog/hooks/datadog.py +++ b/airflow/providers/datadog/hooks/datadog.py @@ -169,9 +169,7 @@ def get_connection_form_widgets() -> dict[str, Any]: "api_host": StringField(lazy_gettext("API endpoint"), widget=BS3TextFieldWidget()), "api_key": StringField(lazy_gettext("API key"), widget=BS3TextFieldWidget()), "app_key": StringField(lazy_gettext("Application key"), widget=BS3TextFieldWidget()), - "source_type_name": StringField( - lazy_gettext("Source type name"), widget=BS3TextFieldWidget() - ), + "source_type_name": StringField(lazy_gettext("Source type name"), widget=BS3TextFieldWidget()), } @staticmethod