diff --git a/oci/README.md b/oci/README.md deleted file mode 100644 index 81fe672b..00000000 --- a/oci/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# Overview - -Repository of OCI functions that process OCI metrics and send data to Datadog as metrics events for further processing. - -# Deploy to OCI - -[![Deploy to Oracle Cloud](https://oci-resourcemanager-plugin.plugins.oci.oraclecloud.com/latest/deploy-to-oracle-cloud.svg)](https://cloud.oracle.com/resourcemanager/stacks/create?zipUrl=https://github.com/kanishktripathi/datadog-serverless-functions/releases/latest/download/datadog-oci-orm.zip) - diff --git a/oci/datadog-oci-orm/metrics-setup/data.tf b/oci/datadog-oci-orm/metrics-setup/data.tf deleted file mode 100644 index 625f71b4..00000000 --- a/oci/datadog-oci-orm/metrics-setup/data.tf +++ /dev/null @@ -1,22 +0,0 @@ -# Source from https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/identity_region_subscriptions - -data "oci_identity_region_subscriptions" "subscriptions" { - # Required - provider = oci.home - tenancy_id = var.tenancy_ocid -} - -data "oci_objectstorage_namespace" "namespace" { - provider = oci.home - compartment_id = var.tenancy_ocid -} - -data "oci_identity_tenancy" "tenancy_metadata" { - tenancy_id = var.tenancy_ocid -} - -data "oci_core_subnet" "input_subnet" { - depends_on = [module.vcn] - #Required - subnet_id = var.create_vcn ? module.vcn[0].subnet_id[local.subnet] : var.function_subnet_id -} diff --git a/oci/datadog-oci-orm/metrics-setup/function_setup.tf b/oci/datadog-oci-orm/metrics-setup/function_setup.tf deleted file mode 100644 index 603039e3..00000000 --- a/oci/datadog-oci-orm/metrics-setup/function_setup.tf +++ /dev/null @@ -1,53 +0,0 @@ -resource "null_resource" "Login2OCIR" { - count = local.user_image_provided ? 0 : 1 - provisioner "local-exec" { - command = "echo '${var.oci_docker_password}' | docker login ${local.oci_docker_repository} --username ${local.ocir_namespace}/${var.oci_docker_username} --password-stdin" - } -} - -### Repository in the Container Image Registry for the container images underpinning the function -resource "oci_artifacts_container_repository" "function_repo" { - # note: repository = store for all images versions of a specific container image - so it included the function name - depends_on = [null_resource.Login2OCIR] - count = local.user_image_provided ? 0 : 1 - compartment_id = var.compartment_ocid - display_name = local.ocir_repo_name - is_public = false - defined_tags = {} - freeform_tags = local.freeform_tags -} - -# ### build the function into a container image and push that image to the repository in the OCI Container Image Registry -resource "null_resource" "FnImagePushToOCIR" { - count = local.user_image_provided ? 0 : 1 - depends_on = [oci_functions_application.metrics_function_app, null_resource.Login2OCIR] - - provisioner "local-exec" { - command = "echo '${var.oci_docker_password}' | docker login ${local.oci_docker_repository} --username ${local.ocir_namespace}/${var.oci_docker_username} --password-stdin" - } - - # remove function image (if it exists) from local container registry - provisioner "local-exec" { - command = "image=$(docker images | grep ${local.function_name} | awk -F ' ' '{print $3}') ; docker rmi -f $image &> /dev/null ; echo $image" - working_dir = "metrics-function" - } - - # build and tag the image from the docker file - provisioner "local-exec" { - command = "docker build -t ${local.docker_image_path} . --no-cache" - working_dir = "metrics-function" - } - - # Push the docker image to the OCI registry - provisioner "local-exec" { - command = "docker push ${local.docker_image_path}" - working_dir = "metrics-function" - } - - # remove function image (if it exists) from local container registry - provisioner "local-exec" { - command = "docker rmi -f `docker images | grep ${local.function_name} | awk -F ' ' '{print $3}'`> /dev/null" - working_dir = "metrics-function" - } - -} diff --git a/oci/datadog-oci-orm/metrics-setup/functions.tf b/oci/datadog-oci-orm/metrics-setup/functions.tf deleted file mode 100644 index 5f84371f..00000000 --- a/oci/datadog-oci-orm/metrics-setup/functions.tf +++ /dev/null @@ -1,34 +0,0 @@ -resource "oci_functions_application" "metrics_function_app" { - depends_on = [data.oci_core_subnet.input_subnet] - compartment_id = var.compartment_ocid - config = { - "DD_API_KEY" = var.datadog_api_key - "DD_COMPRESS" = "true" - "DD_INTAKE_HOST" = var.datadog_environment - "DD_INTAKE_LOGS" = "false" - "DD_MAX_POOL" = "20" - "TENANCY_OCID" = var.tenancy_ocid - } - defined_tags = {} - display_name = "${var.resource_name_prefix}-function-app" - freeform_tags = local.freeform_tags - network_security_group_ids = [ - ] - shape = var.function_app_shape - subnet_ids = [ - data.oci_core_subnet.input_subnet.id, - ] -} - -resource "oci_functions_function" "metrics_function" { - depends_on = [null_resource.FnImagePushToOCIR, oci_functions_application.metrics_function_app] - #Required - application_id = oci_functions_application.metrics_function_app.id - display_name = "${oci_functions_application.metrics_function_app.display_name}-metrics-function" - memory_in_mbs = "256" - - #Optional - defined_tags = {} - freeform_tags = local.freeform_tags - image = local.user_image_provided ? local.custom_image_path : local.docker_image_path -} diff --git a/oci/datadog-oci-orm/metrics-setup/locals.tf b/oci/datadog-oci-orm/metrics-setup/locals.tf deleted file mode 100644 index dec1b39e..00000000 --- a/oci/datadog-oci-orm/metrics-setup/locals.tf +++ /dev/null @@ -1,42 +0,0 @@ -locals { - # Names for the network infra - vcn_name = "${var.resource_name_prefix}-vcn" - nat_gateway = "${local.vcn_name}-natgateway" - service_gateway = "${local.vcn_name}-servicegateway" - subnet = "${local.vcn_name}-private-subnet" -} - -locals { - # Names for the service connector - connector_name = "${var.resource_name_prefix}-connector" -} - -locals { - # Tags for the provisioned resource - freeform_tags = { - datadog-terraform = "true" - } -} - -locals { - # Name for tenancy namespace, metadata and regions - ocir_namespace = data.oci_objectstorage_namespace.namespace.namespace - oci_regions = tomap({ - for reg in data.oci_identity_region_subscriptions.subscriptions.region_subscriptions : - reg.region_name => reg - }) - oci_region_key = lower(local.oci_regions[var.region].region_key) - tenancy_home_region = data.oci_identity_tenancy.tenancy_metadata.home_region_key -} - -locals { - # OCI docker repository - oci_docker_repository = "${local.oci_region_key}.ocir.io/${local.ocir_namespace}" - oci_docker_host = "${local.oci_region_key}.ocir.io" - ocir_repo_name = "${var.resource_name_prefix}-functions" - function_name = "datadog-function-metrics" - docker_image_path = "${local.oci_docker_repository}/${local.ocir_repo_name}/${local.function_name}:latest" - # custom_image_path = "${local.oci_region_key}.ocir.io/${var.function_image_path}" - custom_image_path = var.function_image_path - user_image_provided = length(var.function_image_path) > 0 ? true : false -} diff --git a/oci/datadog-oci-orm/metrics-setup/metrics-function/Dockerfile b/oci/datadog-oci-orm/metrics-setup/metrics-function/Dockerfile deleted file mode 100644 index 5039cc41..00000000 --- a/oci/datadog-oci-orm/metrics-setup/metrics-function/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -FROM fnproject/python:3.11-dev AS build-stage -WORKDIR /function -ADD requirements.txt /function/ - -RUN pip3 install --target /python/ --no-cache --no-cache-dir -r requirements.txt && \ - rm -fr ~/.cache/pip /tmp* requirements.txt func.yaml Dockerfile .venv && \ - chmod -R o+r /python && \ - groupadd --gid 1000 fn && \ - adduser --uid 1000 --gid fn fn -ADD . /function/ -RUN rm -fr /function/.pip_cache -FROM fnproject/python:3.11 -WORKDIR /function -COPY --from=build-stage /python /python -COPY --from=build-stage /function /function -RUN chmod -R o+r /function -ENV PYTHONPATH=/function:/python -ENTRYPOINT ["/python/bin/fdk", "/function/func.py", "handler"] diff --git a/oci/datadog-oci-orm/metrics-setup/metrics-function/func.py b/oci/datadog-oci-orm/metrics-setup/metrics-function/func.py deleted file mode 100644 index 2c4b3516..00000000 --- a/oci/datadog-oci-orm/metrics-setup/metrics-function/func.py +++ /dev/null @@ -1,150 +0,0 @@ -import io -import json -import logging -import os -import gzip - -from fdk import context, response -import requests -from requests.adapters import HTTPAdapter -from requests.exceptions import HTTPError - - -logger = logging.getLogger(__name__) - - -OUTPUT_MESSAGE_VERSION = "v1.0" - -_max_pool = int(os.environ.get("DD_MAX_POOL", 10)) -_session = requests.Session() -_session.mount("https://", HTTPAdapter(pool_connections=_max_pool)) - - -def _get_serialized_metric_data(raw_metrics: io.BytesIO) -> str: - return raw_metrics.getvalue().decode("utf-8") - - -def _generate_metrics_msg( - ctx: context.InvokeContext, - serialized_metric_data: str, -) -> str: - tenancy_ocid = os.environ.get("TENANCY_OCID") - - if not tenancy_ocid: - raise ValueError("Missing environment variable: TENANCY_OCID") - - # Bump OUTPUT_MESSAGE_VERSION any time this - # structure gets updated - message_dict = { - "version": OUTPUT_MESSAGE_VERSION, - "payload": { - "headers": { - "tenancy_ocid": tenancy_ocid, - "source_fn_app_ocid": ctx.AppID(), - "source_fn_app_name": ctx.AppName(), - "source_fn_ocid": ctx.FnID(), - "source_fn_name": ctx.FnName(), - "source_fn_call_id": ctx.CallID(), - }, - "body": serialized_metric_data, - }, - } - - return json.dumps(message_dict) - - -def _should_compress_payload() -> bool: - return os.environ.get("DD_COMPRESS", "false").lower() == "true" - - -def _send_metrics_msg_to_datadog(metrics_message: str) -> str: - endpoint = os.environ.get("DD_INTAKE_HOST") - api_key = os.environ.get("DD_API_KEY") - - if not endpoint or not api_key: - raise ValueError( - "Missing one of the following environment variables: DD_INTAKE_HOST, DD_API_KEY" - ) - - url = f"https://{endpoint}/api/v2/ocimetrics" - api_headers = {"content-type": "application/json", "dd-api-key": api_key} - - if _should_compress_payload(): - serialized = gzip.compress(metrics_message.encode()) - api_headers["content-encoding"] = "gzip" - else: - serialized = metrics_message - - http_response = _session.post(url, data=serialized, headers=api_headers) - http_response.raise_for_status() - - logger.info( - f"Sent payload size={len(metrics_message)} encoding={api_headers.get('content-encoding', None)}" - ) - return http_response.text - - -def handler(ctx: context.InvokeContext, data: io.BytesIO = None) -> response.Response: - """ - Submits incoming metrics data to Datadog. - - Wraps incoming metrics data in a message payload and forwards this - payload to a Datadog endpoint. - - Args: - ctx: - An fdk InvokeContext. - data: - A BytesIO stream containing a JSON representation of metrics. - Each metric has the form: - - { - "namespace": "", - "resourceGroup": "", - "compartmentId": "", - "name": "", - "dimensions": { - "": "", - }, - "metadata": { - "": "", - }, - "datapoints": [ - { - "timestamp": "", - "value": "", - "count": "", - }, - ] - } - - Returns: - An fdk Response in which the body contains any error - messages encountered during processing. At present, HTTP 200 - responses will always be returned. - """ - - try: - serialized_metric_data = _get_serialized_metric_data( - data, - ) - - metrics_message = _generate_metrics_msg( - ctx, - serialized_metric_data, - ) - - result = _send_metrics_msg_to_datadog(metrics_message) - except HTTPError as e: - logger.exception(f"Error sending metrics to Datadog") - result = e.response.text - except Exception as e: - logger.exception("Unexpected error while processing input data") - result = str(e) - - return response.Response( - ctx, - response_data=json.dumps({"result": result}), - headers={"Content-Type": "application/json"}, - ) - diff --git a/oci/datadog-oci-orm/metrics-setup/metrics-function/func.yaml b/oci/datadog-oci-orm/metrics-setup/metrics-function/func.yaml deleted file mode 100644 index f79168af..00000000 --- a/oci/datadog-oci-orm/metrics-setup/metrics-function/func.yaml +++ /dev/null @@ -1,8 +0,0 @@ -schema_version: 20180708 -name: datadog-function-metrics -version: 0.0.1 -runtime: python -build_image: fnproject/python:3.11-dev -run_image: fnproject/python:3.11 -entrypoint: /python/bin/fdk /function/func.py handler -memory: 256 diff --git a/oci/datadog-oci-orm/metrics-setup/metrics-function/requirements.txt b/oci/datadog-oci-orm/metrics-setup/metrics-function/requirements.txt deleted file mode 100644 index 32458ad7..00000000 --- a/oci/datadog-oci-orm/metrics-setup/metrics-function/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -fdk -requests diff --git a/oci/datadog-oci-orm/metrics-setup/outputs.tf b/oci/datadog-oci-orm/metrics-setup/outputs.tf deleted file mode 100644 index db7aeb3d..00000000 --- a/oci/datadog-oci-orm/metrics-setup/outputs.tf +++ /dev/null @@ -1,44 +0,0 @@ -# Output the "list" of all subscribed regions. - -output "all_availability_domains_in_your_tenancy" { - value = data.oci_identity_region_subscriptions.subscriptions.region_subscriptions -} - -output "tenancy_object_storage_namespace" { - value = local.ocir_namespace -} - -output "vcn_network_details" { - depends_on = [module.vcn] - description = "Output of the created network infra" - value = var.create_vcn ? { - vcn_id = module.vcn[0].vcn_id - nat_gateway_id = module.vcn[0].nat_gateway_id - nat_route_id = module.vcn[0].nat_route_id - service_gateway_id = module.vcn[0].service_gateway_id - sgw_route_id = module.vcn[0].sgw_route_id - subnet_id = module.vcn[0].subnet_id[local.subnet] - } : { - vcn_id = "" - nat_gateway_id = "" - nat_route_id = "" - service_gateway_id = "" - sgw_route_id = "" - subnet_id = var.function_subnet_id - } -} - -output "function_application" { - description = "OCID of the Function app" - value = oci_functions_application.metrics_function_app.id -} - -output "function_application_function" { - description = "OCID of the Function" - value = oci_functions_function.metrics_function.id -} - -output "connector_hub" { - description = "Connector hub created for forwarding the data to the function" - value = oci_sch_service_connector.metrics_service_connector.id -} diff --git a/oci/datadog-oci-orm/metrics-setup/provider.tf b/oci/datadog-oci-orm/metrics-setup/provider.tf deleted file mode 100644 index 793fed1c..00000000 --- a/oci/datadog-oci-orm/metrics-setup/provider.tf +++ /dev/null @@ -1,20 +0,0 @@ -terraform { - required_version = ">= 1.2.0" - required_providers { - oci = { - source = "oracle/oci" - version = "5.46.0" - } - } -} - -provider "oci" { - tenancy_ocid = var.tenancy_ocid - region = var.region -} - -provider "oci" { - alias = "home" - tenancy_ocid = var.tenancy_ocid - region = local.tenancy_home_region -} \ No newline at end of file diff --git a/oci/datadog-oci-orm/metrics-setup/schema.yaml b/oci/datadog-oci-orm/metrics-setup/schema.yaml deleted file mode 100644 index e757d280..00000000 --- a/oci/datadog-oci-orm/metrics-setup/schema.yaml +++ /dev/null @@ -1,117 +0,0 @@ -# Title shown in Application Information tab. -title: Datadog Metrics infra -# Sub Title shown in Application Information tab. -description: Setting up infra for sending OCI metrics to Datadog -schemaVersion: 1.1.0 -version: 1.0 -locale: en -variableGroups: - - title: "Tenancy" - variables: - - ${tenancy_ocid} - - ${region} - - ${compartment_ocid} - - title: "Datadog Environment" - variables: - - ${datadog_api_key} - - ${datadog_environment} - - title: "Network options" - variables: - - ${create_vcn} - - ${vcnCompartment} - - ${existingVcn} - - ${function_subnet_id} - - title: "Function settings" - variables: - - ${function_app_shape} - - ${function_image_path} - - ${oci_docker_username} - - ${oci_docker_password} - -variables: - resource_name_prefix: - type: string - title: Resource prefix - description: The prefix for the name of all of the resources. - required: true - default: datadog-metrics - create_vcn: - title: Create VCN - description: Optional variable to create virtual network for the setup. Otherwise, choose an existing subnet from VCN - type: boolean - default: true - -# VCN - vcnCompartment: - # prepopulates available values for compartment - type: oci:identity:compartment:id - existingVcn: - type: oci:core:vcn:id - dependsOn: - compartmentId: ${vcnCompartment} - function_subnet_id: - title: Function Subnet OCID - type: oci:core:subnet:id - description: The OCID of the subnet to be used for the function app. Required if not creating the VCN. - required: false - dependsOn: - compartmentId: ${vcnCompartment} - vcnId: ${existingVcn} - -# Datadog Environment - datadog_api_key: - title: Datadog API Key - type: string - description: The API key for sending message to datadog endpoints. - required: true - sensitive: true - password: true - confirmation: true - datadog_environment: - title: Datadog Environment Endpoint - type: enum - description: The endpoint to hit for sending the metrics. Varies by different datacenter. - required: true - enum: - - ocimetrics-intake.datadoghq.com - - ocimetrics-intake.us5.datadoghq.com - - ocimetrics-intake.us3.datadoghq.com - - ocimetrics-intake.datadoghq.eu - - ocimetrics-intake.ap1.datadoghq.com - - ocimetrics-intake.ddog-gov.com - allowMultiple: false - - # Function setup - function_app_shape: - title: Function Application shape - type: enum - description: The shape of the function application. The docker image should be built accordingly. Use GENERIC_ARM if using Oracle Resource managaer stack. - required: true - enum: - - GENERIC_ARM - - GENERIC_X86 - - GENERIC_X86_ARM - default: GENERIC_ARM - function_image_path: - title: Function Image Path - type: string - description: The full path of the function image. The image should be present in the container registry for the region and be compatible with the function application shape. - required: false - oci_docker_username: - title: OCI Docker registry user name - type: string - description: The user login for the OCI docker container registry to push function image. Not required if using an existing image path - required: false - sensitive: true - oci_docker_password: - title: OCI Docker registry password - type: password - description: The user password for the OCI docker container registry. - required: false - sensitive: true - service_connector_target_batch_size_in_kbs: - title: Service Connector hub batch size - type: nonNegativeInteger - description: The batch size (in Kb) in which to send payload to target. - required: false - default: 5000 diff --git a/oci/datadog-oci-orm/metrics-setup/serviceconnector.tf b/oci/datadog-oci-orm/metrics-setup/serviceconnector.tf deleted file mode 100644 index d92172a1..00000000 --- a/oci/datadog-oci-orm/metrics-setup/serviceconnector.tf +++ /dev/null @@ -1,44 +0,0 @@ -resource "oci_sch_service_connector" "metrics_service_connector" { - depends_on = [oci_functions_function.metrics_function] - #Required - compartment_id = var.compartment_ocid - display_name = local.connector_name - source { - #Required - kind = "monitoring" - - #Optional - monitoring_sources { - - #Optional - compartment_id = var.tenancy_ocid - namespace_details { - #Required - kind = "selected" - namespaces { - #Required - metrics { - #Required - kind = "all" - } - namespace = "oci_computeagent" - } - } - } - } - target { - #Required - kind = "functions" - - #Optional - batch_size_in_kbs = var.service_connector_target_batch_size_in_kbs - batch_time_in_sec = 60 - compartment_id = var.tenancy_ocid - function_id = oci_functions_function.metrics_function.id - } - - #Optional - defined_tags = {} - description = "Terraform created connector hub to distribute metrics" - freeform_tags = local.freeform_tags -} diff --git a/oci/datadog-oci-orm/metrics-setup/variables.tf b/oci/datadog-oci-orm/metrics-setup/variables.tf deleted file mode 100644 index 76ecf81b..00000000 --- a/oci/datadog-oci-orm/metrics-setup/variables.tf +++ /dev/null @@ -1,100 +0,0 @@ -variable "resource_name_prefix" { - type = string - description = "The prefix for the name of all of the resources" - default = "datadog-metrics" -} - -variable "create_vcn" { - type = bool - default = true - description = "Optional variable to create virtual network for the setup. True by default" -} - -variable "datadog_api_key" { - type = string - description = "The API key for sending message to datadog endpoints" -} - -variable "function_subnet_id" { - type = string - default = "" - description = "The OCID of the subnet to be used for the function app. Required if not creating the VCN" -} - -variable "function_image_path" { - type = string - default = "" - description = "The full path of the function image. The image should be present in the container registry for the region" -} -variable "function_app_shape" { - type = string - default = "GENERIC_ARM" - description = "The shape of the function application. The docker image should be built accordingly. Use ARM if using Oracle Resource managaer stack" - validation { - condition = contains(["GENERIC_ARM", "GENERIC_X86", "GENERIC_X86_ARM"], var.function_app_shape) - error_message = "Valid values are: GENERIC_ARM, GENERIC_X86, GENERIC_X86_ARM." - } -} - -variable "datadog_environment" { - type = string - description = "The endpoint to hit for sending the metrics. Varies by different datacenter" - validation { - condition = contains(["ocimetrics-intake.datadoghq.com", "ocimetrics-intake.us5.datadoghq.com", "ocimetrics-intake.us3.datadoghq.com", - "ocimetrics-intake.datadoghq.eu", "ocimetrics-intake.ap1.datadoghq.com", "ocimetrics-intake.ddog-gov.com"], var.datadog_environment) - error_message = "Valid values for var: datadog_environment are (ocimetrics-intake.datadoghq.com, ocimetrics-intake.us5.datadoghq.com, ocimetrics-intake.us3.datadoghq.com, ocimetrics-intake.datadoghq.eu, ocimetrics-intake.ap1.datadoghq.com, ocimetrics-intake.ddog-gov.com)." - } -} - -variable "oci_docker_username" { - type = string - default = "" - sensitive = true - description = "The docker login username for the OCI container registry. Used in creating function image. Not required if the image is already exists." -} - -variable "oci_docker_password" { - type = string - default = "" - sensitive = true - description = "The auth password for docker login to OCI container registry. Used in creating function image. Not required if the image already exists." -} - -variable "service_connector_target_batch_size_in_kbs" { - type = string - description = "The batch size (in Kb) in which to send payload to target" - default = "5000" -} - -#************************************* -# TF auth Requirements -#************************************* -variable "tenancy_ocid" { - type = string - description = "OCI tenant OCID, more details can be found at https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#five" -} -variable "region" { - type = string - description = "OCI Region as documented at https://docs.cloud.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm" -} -variable "compartment_ocid" { - type = string - description = "The compartment OCID to deploy resources to" -} - -#************************************* -# TF auth Requirements for CLI -#************************************* - -# variable "user_ocid" { -# type = string -# description = "The OCID of the user used in authentication and provisioning the resources" -# } -# variable "fingerprint" { -# type = string -# description = "The user public key fingerprint" -# } -# variable "private_key_path" { -# type = string -# description = "The path to the user private key" -# } diff --git a/oci/datadog-oci-orm/metrics-setup/vcn.tf b/oci/datadog-oci-orm/metrics-setup/vcn.tf deleted file mode 100644 index 387a6bb9..00000000 --- a/oci/datadog-oci-orm/metrics-setup/vcn.tf +++ /dev/null @@ -1,24 +0,0 @@ -module "vcn" { - depends_on = [data.oci_objectstorage_namespace.namespace] - source = "oracle-terraform-modules/vcn/oci" - version = "3.6.0" - count = var.create_vcn ? 1 : 0 - compartment_id = var.compartment_ocid - defined_tags = {} - freeform_tags = local.freeform_tags - vcn_cidrs = ["10.0.0.0/16"] - vcn_dns_label = "datadogfunction" - vcn_name = local.vcn_name - lockdown_default_seclist = false - subnets = { - private = { - cidr_block = "10.0.0.0/16" - type = "private" - name = local.subnet - } - } - create_nat_gateway = true - nat_gateway_display_name = local.nat_gateway - create_service_gateway = true - service_gateway_display_name = local.service_gateway -} diff --git a/oci/datadog-oci-orm/policy-setup/policy.tf b/oci/datadog-oci-orm/policy-setup/policy.tf deleted file mode 100644 index 63c5d573..00000000 --- a/oci/datadog-oci-orm/policy-setup/policy.tf +++ /dev/null @@ -1,49 +0,0 @@ -terraform { - required_version = ">= 1.2.0" - required_providers { - oci = { - source = "oracle/oci" - version = "5.46.0" - } - } -} - -data "oci_identity_tenancy" "tenancy_metadata" { - tenancy_id = var.tenancy_ocid -} - -provider "oci" { - tenancy_ocid = var.tenancy_ocid -} - -locals { - tenancy_home_region = data.oci_identity_tenancy.tenancy_metadata.home_region_key - freeform_tags = { - datadog-terraform = "true" - } -} - -resource "oci_identity_dynamic_group" "serviceconnector_group" { - #Required - compartment_id = var.tenancy_ocid - description = "[DO NOT REMOVE] Dynamic group for service connector" - matching_rule = "All {resource.type = 'serviceconnector'}" - name = var.dynamic_group_name - - #Optional - defined_tags = {} - freeform_tags = local.freeform_tags -} - -resource "oci_identity_policy" "metrics_policy" { - depends_on = [oci_identity_dynamic_group.serviceconnector_group] - compartment_id = var.tenancy_ocid - description = "[DO NOT REMOVE] Policy to have any connector hub read from monitoring source and write to a target function" - name = var.datadog_metrics_policy - statements = ["Allow dynamic-group ${var.dynamic_group_name} to read metrics in tenancy", - "Allow dynamic-group ${var.dynamic_group_name} to use fn-function in tenancy", - "Allow dynamic-group ${var.dynamic_group_name} to use fn-invocation in tenancy" - ] - defined_tags = {} - freeform_tags = local.freeform_tags -} diff --git a/oci/datadog-oci-orm/policy-setup/variables.tf b/oci/datadog-oci-orm/policy-setup/variables.tf deleted file mode 100644 index 7a2ba7ef..00000000 --- a/oci/datadog-oci-orm/policy-setup/variables.tf +++ /dev/null @@ -1,24 +0,0 @@ -#************************************* -# TF auth Requirements -#************************************* -variable "tenancy_ocid" { - type = string - description = "OCI tenant OCID, more details can be found at https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#five" -} -variable "region" { - type = string - description = "OCI Region as documented at https://docs.cloud.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm" -} - -variable "dynamic_group_name" { - type = string - description = "The name of the dynamic group for giving access to service connector" - default = "datadog-metrics-dynamic-group" -} - -variable "datadog_metrics_policy" { - type = string - description = "The name of the policy for metrics" - default = "datadog-metrics-policy" -} - diff --git a/oci/dd-metrics-function.py b/oci/dd-metrics-function.py deleted file mode 100644 index e58a105a..00000000 --- a/oci/dd-metrics-function.py +++ /dev/null @@ -1,149 +0,0 @@ -import io -import json -import logging -import os -import gzip - -from fdk import context, response -import requests -from requests.adapters import HTTPAdapter -from requests.exceptions import HTTPError - - -logger = logging.getLogger(__name__) - - -OUTPUT_MESSAGE_VERSION = "v1.0" - -_max_pool = int(os.environ.get("DD_MAX_POOL", 10)) -_session = requests.Session() -_session.mount("https://", HTTPAdapter(pool_connections=_max_pool)) - - -def _get_serialized_metric_data(raw_metrics: io.BytesIO) -> str: - return raw_metrics.getvalue().decode("utf-8") - - -def _generate_metrics_msg( - ctx: context.InvokeContext, - serialized_metric_data: str, -) -> str: - tenancy_ocid = os.environ.get("TENANCY_OCID") - - if not tenancy_ocid: - raise ValueError("Missing environment variable: TENANCY_OCID") - - # Bump OUTPUT_MESSAGE_VERSION any time this - # structure gets updated - message_dict = { - "version": OUTPUT_MESSAGE_VERSION, - "payload": { - "headers": { - "tenancy_ocid": tenancy_ocid, - "source_fn_app_ocid": ctx.AppID(), - "source_fn_app_name": ctx.AppName(), - "source_fn_ocid": ctx.FnID(), - "source_fn_name": ctx.FnName(), - "source_fn_call_id": ctx.CallID(), - }, - "body": serialized_metric_data, - }, - } - - return json.dumps(message_dict) - - -def _should_compress_payload() -> bool: - return os.environ.get("DD_COMPRESS", "false").lower() == "true" - - -def _send_metrics_msg_to_datadog(metrics_message: str) -> str: - endpoint = os.environ.get("DD_INTAKE_HOST") - api_key = os.environ.get("DD_API_KEY") - - if not endpoint or not api_key: - raise ValueError( - "Missing one of the following environment variables: DD_INTAKE_HOST, DD_API_KEY" - ) - - url = f"https://{endpoint}/api/v2/ocimetrics" - api_headers = {"content-type": "application/json", "dd-api-key": api_key} - - if _should_compress_payload(): - serialized = gzip.compress(metrics_message.encode()) - api_headers["content-encoding"] = "gzip" - else: - serialized = metrics_message - - http_response = _session.post(url, data=serialized, headers=api_headers) - http_response.raise_for_status() - - logger.info( - f"Sent payload size={len(metrics_message)} encoding={api_headers.get('content-encoding', None)}" - ) - return http_response.text - - -def handler(ctx: context.InvokeContext, data: io.BytesIO = None) -> response.Response: - """ - Submits incoming metrics data to Datadog. - - Wraps incoming metrics data in a message payload and forwards this - payload to a Datadog endpoint. - - Args: - ctx: - An fdk InvokeContext. - data: - A BytesIO stream containing a JSON representation of metrics. - Each metric has the form: - - { - "namespace": "", - "resourceGroup": "", - "compartmentId": "", - "name": "", - "dimensions": { - "": "", - }, - "metadata": { - "": "", - }, - "datapoints": [ - { - "timestamp": "", - "value": "", - "count": "", - }, - ] - } - - Returns: - An fdk Response in which the body contains any error - messages encountered during processing. At present, HTTP 200 - responses will always be returned. - """ - - try: - serialized_metric_data = _get_serialized_metric_data( - data, - ) - - metrics_message = _generate_metrics_msg( - ctx, - serialized_metric_data, - ) - - result = _send_metrics_msg_to_datadog(metrics_message) - except HTTPError as e: - logger.exception(f"Error sending metrics to Datadog") - result = e.response.text - except Exception as e: - logger.exception("Unexpected error while processing input data") - result = str(e) - - return response.Response( - ctx, - response_data=json.dumps({"result": result}), - headers={"Content-Type": "application/json"}, - )