#!/bin/bash ########################################################## # Use this script to generate a list of resources # for each available metric to input in your yaml template ########################################################## GREEN='\033[0;32m' YELLOW='\033[0;33m' RED='\033[0;31m' NO_COLOR='\033[0m' function log { echo -e "$(date +'%Y-%m-%d %T') ${1}" } function check_installed { if ! command -v ${1} &> /dev/null; then log "${RED}'${1}' must be installed locally${NO_COLOR}. Please install it and rerun this script" exit 1 fi } if [ "${1}" = "" ] || [ "${2}" = "" ]; then log "${RED}Two arguments are required to run this script${NO_COLOR}: the name of the 'fd' to target and the environment" log "e.g. '${0} my-app production'" exit 1 fi # Checking if required cli tools are installed on local machine check_installed curl check_installed jq check_installed rg # Setting cli arguments to local variables service_name=${1} environment=${2} # Declaring array of allowed metric types declare -a metrics=("trace.netty.request.hits" "trace.http.request.hits" "trace.servlet.request.hits" "trace.kafka.consume" "trace.kafka.produce" "trace.grpc.server.hits") log "A Datadog 'Application Key' and an 'API Key' are required to run this script" log "If you do not already have these credentials, please exit the script and generate them from https://app.datadoghq.com/organization-settings/api-keys" log "Once your credentials are ready, you will be able to rerun this script" # Checking if datadog application and api env variables are set # If not the script will prompt entry if [[ -z "${DD_APP_KEY}" ]]; then log "${RED}Environment Variable DD_APP_KEY is not set" log "Please type your application key in the following prompt" echo 'Enter your Datadog Application Key: ' read -s DD_APP_KEY else log "${GREEN}Environment Variable DD_APP_KEY is present and set" fi if [[ -z "${DD_API_KEY}" ]]; then log "${RED}Environment Variable DD_API_KEY is not set" log "Please type your API key in the following prompt" echo 'Enter your Datadog API Key: ' read -s DD_API_KEY else log "${GREEN}Environment Variable DD_API_KEY is present and set" fi # Curl commands to grab metric resources from datadog console for each metric log "${GREEN}Resources for Metric netty.request.hits: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.netty.request.hits&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: log "${GREEN}Resources for Metric http.request.hits: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.http.request.hits&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: log "${GREEN}Resources for Metric servlet.request.hits: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.servlet.request.hits&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: # Note that when using the kafka metric, the resources will be found under '[service_name]-kafka' log "${GREEN}Resources for Metric kafka.consume: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.kafka.consume&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}-kafka" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: # Note that when using the kafka metric, the resources will be found under '[service_name]-kafka' log "${GREEN}Resources for Metric kafka.produce: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.kafka.produce&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}-kafka" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: log "${GREEN}Resources for Metric grpc.server.hits: " curl -X GET "https://app.datadoghq.com/metric/flat_tags_for_metric?metric=trace.grpc.server.hits&filter%5B%5D=env%3A${environment}&window=2419200&limit=1000&filter%5B%5D=service%3A${service_name}" -H "Content-Type: application/json" -H "DD-API-KEY: ${DD_API_KEY}" -H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq . | rg resource_name: