Created
May 30, 2022 11:51
-
-
Save atalv/f98b928c870630c677694b5edac072dc to your computer and use it in GitHub Desktop.
Sample scripts that demonstrate how to submit logging data to the Azure Monitor HTTP Data Collector API from R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(caTools) | |
require(digest) | |
require(httr) | |
require(jsonlite) | |
#' Build API signature for logging to Azure log analytics | |
#' | |
#' Azure log analytics HTTP REST API documentation for Python is followed to | |
#' create the R version of it. Python version of this function is described | |
#' at \url{https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api#python-3-sample} | |
#' | |
#' @param customer_id \code{customer_id} of the Azure log analytics workspace | |
#' @param shared_key \code{shared_key} of the Azure log analytics workspace | |
#' @param date datetime of logging event | |
#' @param content_length Content length of the body | |
#' @param method Only one value is expected - \code{POST} | |
#' @param content_type Only one value is expected - \code{application/json} | |
#' @param resource Only one value is expected - \code{/api/logs} | |
#' @return Returns part of the header of HTTP POST request to be sent to Azure | |
#' log analytics workspace | |
.build_signature <- function(customer_id, shared_key, date, content_length, | |
method, content_type, resource) { | |
x_headers <- paste0("x-ms-date:", date) | |
string_to_hash <- paste0(method, "\n", content_length, "\n", | |
content_type, "\n", x_headers, "\n", resource) | |
bytes_to_hash <- charToRaw(enc2utf8(string_to_hash)) | |
decoded_key <- caTools::base64decode(shared_key, "raw") | |
encoded_hash <- caTools::base64encode(digest::hmac(decoded_key, bytes_to_hash, | |
algo = "sha256", | |
serialize = FALSE, | |
raw = TRUE)) | |
authorization <- paste0("SharedKey ", customer_id, ":", encoded_hash) | |
return(authorization) | |
} | |
#' Build and send a request to the POST API of Azure log analytics | |
#' | |
#' @inherit .build_signature details | |
#' | |
#' @param customer_id \code{customer_id} of the Azure log analytics workspace | |
#' @param shared_key \code{shared_key} of the Azure log analytics workspace | |
#' @param body Content or message to be logged in json format | |
#' @param log_type Log-Type as defined in Azure document, for custom logging | |
#' @return Returns the HTTP response object | |
#' @examples \dontrun{ | |
#' .post_data( | |
#' "xxxx", "yyyy", | |
#' as.character(jsonlite::toJSON(list(level="info", message="test"), | |
#' auto_unbox = TRUE)), | |
#' "test" | |
#' ) | |
.post_data <- function(customer_id, shared_key, body, log_type) { | |
method <- "POST" | |
content_type <- "application/json" | |
resource <- "/api/logs" | |
rfc1123date <- as.character(strftime(as.POSIXlt(Sys.time(), tz = "UTC"), | |
"%a, %d %b %Y %H:%M:%S GMT")) | |
content_length <- nchar(body) | |
signature <- .build_signature(customer_id, shared_key, rfc1123date, | |
content_length, method, content_type, resource) | |
uri <- paste0("https://", customer_id, ".ods.opinsights.azure.com", | |
resource, "?api-version=2016-04-01") | |
headers <- httr::add_headers(.headers = c("Content-Type" = content_type, | |
"Authorization" = signature, | |
"Log-Type" = log_type, | |
"x-ms-date" = rfc1123date)) | |
response <- httr::POST(uri, config = headers, body = body) | |
return(response) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment