diff --git a/README.md b/README.md index 8df343e..9272874 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,16 @@ # opentelemetry-shell +```sh + _____ _____ _____ _____ _____ _____ __ _____ _____ _____ _____ _____ __ __ +| | _ | __| | |_ _| __| | | __| | __|_ _| __ | | | +| | | __| __| | | | | | | __| |__| __| | | | __| | | | -|_ _| +|_____|__| |_____|_|___| |_| |_____|_____|_____|_|_|_|_____| |_| |__|__| |_| + _____ _____ _____ __ __ +| __| | | __| | | | +|__ | | __| |__| |__ +|_____|__|__|_____|_____|_____| +``` + **Logs**, **metrics**, and **traces** are often known as the three pillars of observability. We take great care to ensure we cover these pillars in our services. But, underpinning these services is usually a script. `Bash` has been around for many decades now and other shells for even longer. This is usually the glue that ensures we can manage, deploy and perform many tasks around the services that we develop. @@ -12,7 +23,7 @@ The functions utilise the [OpenTelemetry Protocol Specification (OTLP)](https:// These functions have been tested on several `bash` versions, the list is as follows: -- `3.x` +- `3.x`, although looking to deprecate support for this soon (tm) - `4.x` - `5.x` @@ -55,7 +66,23 @@ Tracing is supported with a rudimentary, parent/child relationship. The followin - Script name - Line number - Ability to add user specified resource attributes -- Detectors, supports Github Actions workflows + +#### Resource Detectors + +The resource detectors can be used to detect resource information from the host, in a format that conforms to the OpenTelemetry resource semantic conventions, and append or override the resource value in telemetry data with this information. Currently the following detectors are supported: + +- [X] Azure Pipelines +- [X] Bitbucket Pipelines +- [X] Buildkite +- [X] Circle CI +- [ ] Codefresh +- [X] Github Actions +- [X] Gitlab CI +- [ ] Google Cloud Build +- [ ] Harness +- [X] Jenkins +- [ ] Jenkins X +- [X] Travis CI ## Examples @@ -142,7 +169,7 @@ The following environment variables will be currently used: **[General SDK Configuration](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#general-sdk-configuration)** - +- `OTEL_SERVICE_NAME`: Sets the value of the `service.name` resource attribute - `OTEL_LOG_LEVEL`: Log level used by the logger, `debug`. Unset variable to disable verbose logging **[Exporter Selection](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#exporter-selection)** diff --git a/library/log.sh b/library/log.sh index 9547b5c..4564077 100644 --- a/library/log.sh +++ b/library/log.sh @@ -42,7 +42,7 @@ function log { local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") local -r script_name="${0##*/}#L${BASH_LINENO[1]}" local -r function="${FUNCNAME[2]}" - echo_stderr -e "${timestamp} [${level}] [$script_name] ["${function}"()] ${message}" + echo_stderr -e "${timestamp} [${level}] [$script_name] [${function}()] ${message}" } ####################################### @@ -65,7 +65,7 @@ function log_trace { # Write to stderr ####################################### function log_debug { - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then local -r message="$1" else local -r message="$1" diff --git a/library/otel_init.sh b/library/otel_init.sh index 4ba0cb2..ec2f15f 100644 --- a/library/otel_init.sh +++ b/library/otel_init.sh @@ -16,7 +16,7 @@ set -euo pipefail -if [ -z ${OTEL_SH_LIB_PATH-} ]; then +if [ -z "${OTEL_SH_LIB_PATH-}" ]; then export OTEL_SH_LIB_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" fi @@ -29,17 +29,32 @@ export hostname=$(hostname) export telemetry_sdk_name="opentelemetry.sh" export telemetry_sdk_lang="bash" +printf "\n" +printf " _____ _____ _____ _____ _____ _____ __ _____ _____ _____ _____ _____ __ __ \n" +printf "| | _ | __| | |_ _| __| | | __| | __|_ _| __ | | |\n" +printf "| | | __| __| | | | | | | __| |__| __| | | | __| | | | -|_ _|\n" +printf "|_____|__| |_____|_|___| |_| |_____|_____|_____|_|_|_|_____| |_| |__|__| |_| \n" +printf " _____ _____ _____ __ __ \n" +printf "| __| | | __| | | | \n" +printf "|__ | | __| |__| |__ \n" +printf "|_____|__|__|_____|_____|_____| \n" +printf "\n" + log_info "Initialising OpenTelemetry Shell v${telemetry_sdk_ver}" # OTEL_EXPORTER_OTLP_TRACES_ENDPOINT # OTEL_EXPORTER_OTLP_METRICS_ENDPOINT # OTEL_EXPORTER_OTLP_LOGS_ENDPOINT -if [ -z ${OTEL_EXPORTER_OTEL_ENDPOINT-} ]; then +if [ -z "${OTEL_EXPORTER_OTEL_ENDPOINT-}" ]; then log_error "OTEL_EXPORTER_OTEL_ENDPOINT not exported" exit 1 fi -if [ -z ${service_version-} ]; then +if [ -z "${OTEL_SERVICE_NAME-}" ]; then + export OTEL_SERVICE_NAME="${0##*/}" +fi + +if [ -z "${service_version-}" ]; then export service_version="undefined" fi diff --git a/library/otel_metrics.sh b/library/otel_metrics.sh index 5ae5268..9db16e2 100644 --- a/library/otel_metrics.sh +++ b/library/otel_metrics.sh @@ -44,31 +44,31 @@ function otel_metrics_push_gauge { local time_unix_namo=$(get_epoch_now) - if [ ! -z ${custom_resource_attributes-} ]; then + if [ -n "${custom_resource_attributes-}" ]; then log_debug "Appending custom resource attributes" for attr in "${custom_resource_attributes[@]}"; do otel_metrics_add_resourcemetrics_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi -otel_metrics_add_gauge $name \ - $description \ - $unit +otel_metrics_add_gauge "$name" \ + "$description" \ + "$unit" if [[ ${type} == "double" ]]; then - otel_metrics_add_gauge_datapoint_double $key \ - $value \ - $as_value + otel_metrics_add_gauge_datapoint_double "$key" \ + "$value" \ + "$as_value" elif [[ ${type} == "int" ]]; then - otel_metrics_add_gauge_datapoint_int $key \ - $value \ - $as_value + otel_metrics_add_gauge_datapoint_int "$key" \ + "$value" \ + "$as_value" else log_error "'as_value' arg needs to be double|int" exit 1 fi - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z $"{OTEL_LOG_LEVEL-}" ]; then net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" else log_debug "[$( caller )] $*" >&2 @@ -106,32 +106,32 @@ function otel_metrics_push_sum { local time_unix_namo=$(get_epoch_now) - if [ $custom_resource_attributes ]; then + if [ "$custom_resource_attributes" ]; then for attr in "${custom_resource_attributes[@]}"; do otel_metrics_add_resourcemetrics_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi -otel_metrics_add_gauge $name \ - $description \ - $unit +otel_metrics_add_gauge "$name" \ + "$description" \ + "$unit" if [[ ${type} == "double" ]]; then - otel_metrics_add_sum_datapoint_double $key \ - $value \ - $as_value \ - $start_time_unix_nano + otel_metrics_add_sum_datapoint_double "$key" \ + "$value" \ + "$as_value" \ + "$start_time_unix_nano" elif [[ ${type} == "int" ]]; then - otel_metrics_add_sum_datapoint_int $key \ - $value \ - $as_value \ - $start_time_unix_nano + otel_metrics_add_sum_datapoint_int "$key" \ + "$value" \ + "$as_value" \ + "$start_time_unix_nano" else log_error "'as_value' arg needs to be double|int" exit 1 fi - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" else log_debug "[$( caller )] $*" >&2 diff --git a/library/otel_metrics_schema.sh b/library/otel_metrics_schema.sh index 5619b7a..a0db359 100644 --- a/library/otel_metrics_schema.sh +++ b/library/otel_metrics_schema.sh @@ -62,7 +62,7 @@ otel_metrics_add_resourcemetrics_resource_attrib_string() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].resource.attributes += [$attribute]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].resource.attributes += [$attribute]" <<< "$otel_metrics_resource_metrics") } ####################################### @@ -85,7 +85,7 @@ otel_metrics_add_resourcemetrics_resource_attrib_int() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].resource.attributes += [$attribute]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].resource.attributes += [$attribute]" <<< "$otel_metrics_resource_metrics") } ####################################### @@ -115,7 +115,7 @@ otel_metrics_add_gauge() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[-1].metrics += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[-1].metrics += [$obj]" <<< "$otel_metrics_resource_metrics") } @@ -151,7 +151,7 @@ otel_metrics_add_gauge_datapoint_int() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].gauge.dataPoints += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].gauge.dataPoints += [$obj]" <<< "$otel_metrics_resource_metrics") } @@ -187,7 +187,7 @@ otel_metrics_add_gauge_datapoint_double() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].gauge.dataPoints += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].gauge.dataPoints += [$obj]" <<< "$otel_metrics_resource_metrics") } @@ -220,7 +220,7 @@ otel_metrics_add_sum() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[-1].metrics += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[-1].metrics += [$obj]" <<< "$otel_metrics_resource_metrics") } @@ -250,11 +250,11 @@ otel_metrics_add_sum_datapoint_double() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.dataPoints += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.dataPoints += [$obj]" <<< "$otel_metrics_resource_metrics") - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.aggregationTemporality = \"AGGREGATION_TEMPORALITY_CUMULATIVE\"" <<< $otel_metrics_resource_metrics) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.isMonotonic = true" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.aggregationTemporality = \"AGGREGATION_TEMPORALITY_CUMULATIVE\"" <<< "$otel_metrics_resource_metrics") + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.isMonotonic = true" <<< "$otel_metrics_resource_metrics") } @@ -284,9 +284,9 @@ otel_metrics_add_sum_datapoint_int() { EOF ) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.dataPoints += [$obj]" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.dataPoints += [$obj]" <<< "$otel_metrics_resource_metrics") - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.aggregationTemporality = \"AGGREGATION_TEMPORALITY_CUMULATIVE\"" <<< $otel_metrics_resource_metrics) - otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.isMonotonic = true" <<< $otel_metrics_resource_metrics) + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.aggregationTemporality = \"AGGREGATION_TEMPORALITY_CUMULATIVE\"" <<< "$otel_metrics_resource_metrics") + otel_metrics_resource_metrics=$(jq -r ".resourceMetrics[].instrumentationLibraryMetrics[].metrics[-1].sum.isMonotonic = true" <<< "$otel_metrics_resource_metrics") } diff --git a/library/otel_traces.sh b/library/otel_traces.sh index db84850..4c42ccd 100644 --- a/library/otel_traces.sh +++ b/library/otel_traces.sh @@ -20,10 +20,11 @@ . "${OTEL_SH_LIB_PATH}/uuid.sh" . "${OTEL_SH_LIB_PATH}/otel_init.sh" -. "${OTEL_SH_LIB_PATH}/otel_traces_schema.sh" . "${OTEL_SH_LIB_PATH}/otel_traces_detector.sh" +. "${OTEL_SH_LIB_PATH}/otel_traces_schema.sh" + -if [ -z ${OTEL_TRACE_ID-} ]; then +if [ -z "${OTEL_TRACE_ID-}" ]; then export OTEL_TRACE_ID=$(generate_uuid 16) fi @@ -53,27 +54,27 @@ otel_trace_start_parent_span() { log_debug "Command: ${*} Errorlevel: ${exit_code}" local end_time_unix_nano=$(get_epoch_now) - if [ ! -z ${custom_resource_attributes-} ]; then + if [ -n "${custom_resource_attributes-}" ]; then log_debug "Appending custom resource attributes" for attr in "${custom_resource_attributes[@]}"; do otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi - if [ ! -z ${detector_resource_attributes-} ]; then + if [ -n "${detector_resource_attributes-}" ]; then log_debug "Appending detector resource attributes" for attr in "${detector_resource_attributes[@]}"; do otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi - otel_trace_add_resource_scopespans_span $name \ - $OTEL_TRACE_ID \ - ${span_id} \ + otel_trace_add_resource_scopespans_span "$name" \ + "$OTEL_TRACE_ID" \ + "$span_id" \ "" \ - $start_time_unix_nano \ - $end_time_unix_nano \ - $exit_code + "$start_time_unix_nano" \ + "$end_time_unix_nano" \ + "$exit_code" otel_trace_add_resourcespan_scopespans_spans_attrib_string "command" "$*" otel_trace_add_resourcespan_scopespans_spans_attrib_string "errorlevel" "${exit_code}" @@ -81,7 +82,7 @@ otel_trace_start_parent_span() { otel_trace_add_resourcespan_scopespans_spans_attrib_string "code.url" "${PWD}/${0##*/}#L${BASH_LINENO[0]}" - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then log_debug "curling ${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" else @@ -128,27 +129,27 @@ otel_trace_start_child_span() { log_debug "Command: ${*} Errorlevel: ${exit_code}" local end_time_unix_nano=$(get_epoch_now) - if [ ! -z ${custom_resource_attributes-} ]; then + if [ -n "${custom_resource_attributes-}" ]; then log_debug "Appending custom resource attributes" for attr in "${custom_resource_attributes[@]}"; do otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi - if [ ! -z ${detector_resource_attributes-} ]; then + if [ -n "${detector_resource_attributes-}" ]; then log_debug "Appending detector resource attributes" for attr in "${detector_resource_attributes[@]}"; do otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" done fi - otel_trace_add_resource_scopespans_span $name \ - $OTEL_TRACE_ID \ - ${span_id} \ - $PARENT_SPAN_ID \ - $start_time_unix_nano \ - $end_time_unix_nano \ - $exit_code + otel_trace_add_resource_scopespans_span" $name" \ + "$OTEL_TRACE_ID" \ + "$span_id" \ + "$PARENT_SPAN_ID" \ + "$start_time_unix_nano" \ + "$end_time_unix_nano" \ + "$exit_code" otel_trace_add_resourcespan_scopespans_spans_attrib_string "command" "$*" otel_trace_add_resourcespan_scopespans_spans_attrib_string "errorlevel" "${exit_code}" @@ -156,7 +157,7 @@ otel_trace_start_child_span() { otel_trace_add_resourcespan_scopespans_spans_attrib_string "code.url" "${PWD}/${0##*/}#L${BASH_LINENO[0]}" - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" else log_debug "[$( caller )] $*" >&2 @@ -171,7 +172,7 @@ otel_trace_start_child_span() { net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" fi - if [ $exit_code -ne 0 ]; then + if [ "$exit_code" -ne 0 ]; then log_fatal "Exiting with Errorlevel: ${exit_code}" exit $exit_code fi diff --git a/library/otel_traces_detector.sh b/library/otel_traces_detector.sh index 80dde6d..96aca44 100644 --- a/library/otel_traces_detector.sh +++ b/library/otel_traces_detector.sh @@ -16,17 +16,57 @@ . "${OTEL_SH_LIB_PATH}/log.sh" +# Azure Pipelines +if [ "${TF_BUILD-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_azure_pipelines.sh" +fi + +# Bitbucket Pipelines +if [ "${BITBUCKET_BUILD_NUMBER-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_bitbucket_pipelines.sh" +fi + +# Buildkite +if [ "${BUILDKITE-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_buildkite.sh" +fi + +# Circle CI +if [ "${CIRCLECI-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_circle_ci.sh" +fi + # Github Actions -if [ ${GITHUB_ACTIONS-} ]; then - . "${OTEL_SH_LIB_PATH}/otel_traces_detector_gha.sh" +if [ "${GITHUB_ACTIONS-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_github_actions.sh" fi -# Harness -if [ ${DEPLOYMENT_ID-} ]; then - . "${OTEL_SH_LIB_PATH}/otel_traces_detector_harness.sh" +# Gitlab CI +if [ "${GITLAB_CI-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_gitlab_ci.sh" fi +# Google Cloud Build +# if [ "${GOOGLE-CLOUD-BUILD-}" ]; then +# . "${OTEL_SH_LIB_PATH}/otel_traces_detector_google_cloud_build.sh" +# fi + +# TODO: Harness +# if [ "${DEPLOYMENT_ID-}" ]; then +# . "${OTEL_SH_LIB_PATH}/otel_traces_detector_harness.sh" +# fi + # Jenkins -if [ ${CI-} ] && [ ${JENKINS_URL-} ]; then +if [ "${CI-}" ] && [ "${JENKINS_URL-}" ]; then . "${OTEL_SH_LIB_PATH}/otel_traces_detector_jenkins.sh" fi + +# Jenkins X +# if [ "${JENKINS-X-}" ]; then +# . "${OTEL_SH_LIB_PATH}/otel_traces_detector_jenkins_x.sh" +# fi + +# Travis CI +if [ "${TRAVIS-}" ]; then + . "${OTEL_SH_LIB_PATH}/otel_traces_detector_travis_ci.sh" +fi diff --git a/library/otel_traces_detector_azure_pipelines.sh b/library/otel_traces_detector_azure_pipelines.sh new file mode 100644 index 0000000..b4918b5 --- /dev/null +++ b/library/otel_traces_detector_azure_pipelines.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Azure Pipelines..." + +return_spaces_to_dashes "${BUILD_REPOSITORY_URI}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "azure.branch:${BUILD_SOURCEBRANCHNAME}" + "azure.build.id:${BUILD_BUILDID}" + "azure.build.number:${BUILD_BUILDNUMBER}" + "azure.job.name:${SYSTEM_JOBDISPLAYNAME}" + "azure.stage.name:${SYSTEM_STAGEDISPLAYNAME}" + "azure.pull.request.id:${SYSTEM_PULLREQUEST_PULLREQUESTID}" + "azure.pull.request.number:${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}" + "azure.build.user:${BUILD_REQUESTEDFOR}" + "azure.repo:${BUILD_REPOSITORY_URI}" +) diff --git a/library/otel_traces_detector_gha.sh b/library/otel_traces_detector_bitbucket_pipelines.sh similarity index 56% rename from library/otel_traces_detector_gha.sh rename to library/otel_traces_detector_bitbucket_pipelines.sh index 92bc7db..fb1eff0 100644 --- a/library/otel_traces_detector_gha.sh +++ b/library/otel_traces_detector_bitbucket_pipelines.sh @@ -15,21 +15,17 @@ # limitations under the License. . "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" -log_info "Detected, GitHub Actions workflow" +log_info "Detected, Bitbucket Pipelines..." + +return_spaces_to_dashes "${BITBUCKET_REPO_FULL_NAME}-pipelines" "OTEL_SERVICE_NAME" detector_resource_attributes=( - "gha.action:${GITHUB_ACTION}" - "gha.action_repository:${GITHUB_ACTION_REPOSITORY}" - "gha.actor:${GITHUB_ACTOR}" - "gha.job:${GITHUB_JOB}" - "gha.ref:${GITHUB_REF}" - "gha.ref_name:${GITHUB_REF_NAME}" - "gha.github.repository:${GITHUB_REPOSITORY}" - "gha.github.sha:${GITHUB_SHA}" - "gha.workflow:${GITHUB_WORKFLOW}" - "gha.workspace:${GITHUB_WORKSPACE}" - "gha.runner.arch:${RUNNER_ARCH}" - "gha.runner.name:${RUNNER_NAME}" - "gha.runner.os:${RUNNER_OS}" + "buildkite.branch:${BITBUCKET_BRANCH}" + "buildkite.build.number:${BITBUCKET_BUILD_NUMBER}" + "buildkite.build.user:${BITBUCKET_STEP_TRIGGERER_UUID}" + "buildkite.pipeline.id:${BITBUCKET_PIPELINE_UUID}" + "buildkite.pull.request.id:${BITBUCKET_PR_ID}" + "buildkite.repo:${BITBUCKET_REPO_FULL_NAME}" ) diff --git a/library/otel_traces_detector_buildkite.sh b/library/otel_traces_detector_buildkite.sh new file mode 100644 index 0000000..f552b21 --- /dev/null +++ b/library/otel_traces_detector_buildkite.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Buildkite..." + +return_spaces_to_dashes "${BUILDKITE_REPO}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "buildkite.branch:${BUILDKITE_BRANCH}" + "buildkite.build.number:${BUILDKITE_BUILD_NUMBER}" + "buildkite.build.url:${BUILDKITE_BUILD_URL}" + "buildkite.pull.request.number:${BUILDKITE_PULL_REQUEST}" + "buildkite.pull.request.repo:${BUILDKITE_PULL_REQUEST_REPO}" + "buildkite.repo:${BUILDKITE_REPO}" +) diff --git a/library/otel_traces_detector_circle_ci.sh b/library/otel_traces_detector_circle_ci.sh new file mode 100644 index 0000000..b8846b1 --- /dev/null +++ b/library/otel_traces_detector_circle_ci.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Circle CI..." + +return_spaces_to_dashes "${CIRCLE_REPOSITORY_URL}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "circle.ci.branch:${CIRCLE_BRANCH}" + "circle.ci.build.number:${CIRCLE_BUILD_NUM}" + "circle.ci.build.url:${CIRCLE_BUILD_URL}" + "circle.ci.job.name:${CIRCLE_JOB}" + "circle.ci.pull.request.number:${CIRCLE_PR_NUMBER}" + "circle.ci.pull.request.user:${CIRCLE_PR_USER}" + "circle.ci.pull.request.repo:${CIRCLE_PR_REPONAME}" + "circle.ci.repo:${CIRCLE_REPOSITORY_URL}" +) diff --git a/library/otel_traces_detector_github_actions.sh b/library/otel_traces_detector_github_actions.sh new file mode 100644 index 0000000..757af10 --- /dev/null +++ b/library/otel_traces_detector_github_actions.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, GitHub Actions..." + +return_spaces_to_dashes "${GITHUB_REPOSITORY}-workflows" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "github.action:${GITHUB_ACTION}" + "github.action.repository:${GITHUB_ACTION_REPOSITORY}" + "github.actor:${GITHUB_ACTOR}" + "github.event.name:${GITHUB_EVENT_NAME}" + "github.job:${GITHUB_JOB}" + "github.ref:${GITHUB_REF}" + "github.ref.name:${GITHUB_REF_NAME}" + "github.repository:${GITHUB_REPOSITORY}" + "github.repository.owner:${GITHUB_REPOSITORY_OWNER}" + "github.run.number:${GITHUB_RUN_NUMBER}" + "github.sha:${GITHUB_SHA}" + "github.workflow:${GITHUB_WORKFLOW}" + "github.workspace:${GITHUB_WORKSPACE}" + "github.runner.arch:${RUNNER_ARCH}" + "github.runner.name:${RUNNER_NAME}" + "github.runner.os:${RUNNER_OS}" +) diff --git a/library/otel_traces_detector_gitlab_ci.sh b/library/otel_traces_detector_gitlab_ci.sh new file mode 100644 index 0000000..8bc6f25 --- /dev/null +++ b/library/otel_traces_detector_gitlab_ci.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Gitlab CI..." + +return_spaces_to_dashes "${CI_PROJECT_URL}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "gitlab.ci.branch:${CI_COMMIT_REF_NAME}" + "gitlab.ci.build.number:${CI_PIPELINE_ID}" + "gitlab.ci.build.url:${CI_PIPELINE_URL}" + "gitlab.ci.pull.request.branch:${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}" + "gitlab.ci.pull.request.number:${CI_MERGE_REQUEST_ID}" + "gitlab.ci.pull.request.repo:${CI_MERGE_REQUEST_SOURCE_PROJECT_PATH}" + "gitlab.ci.repo:${CI_PROJECT_URL}" +) diff --git a/library/otel_traces_detector_google_cloud_build.sh b/library/otel_traces_detector_google_cloud_build.sh new file mode 100644 index 0000000..4592a0b --- /dev/null +++ b/library/otel_traces_detector_google_cloud_build.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Google Cloud Build..." + +return_spaces_to_dashes "${REPO_NAME}-workflows" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "cloud.build.branch:${BRANCH_NAME}" + "cloud.build.build.number:${BUILD_ID}" + "cloud.build.pull.request.branch:${HEAD_BRANCH}" + "cloud.build.repo:${REPO_NAME}" + "cloud.build.repo.owner:${REPO_OWNER}" +) diff --git a/library/otel_traces_detector_harness.sh b/library/otel_traces_detector_harness.sh index 20d5caf..05064f0 100644 --- a/library/otel_traces_detector_harness.sh +++ b/library/otel_traces_detector_harness.sh @@ -15,8 +15,11 @@ # limitations under the License. . "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" -log_info "Detected, Harness" +log_info "Detected, Harness..." + +return_spaces_to_dashes "${REPO_NAME}-pipelines" "OTEL_SERVICE_NAME" declare -t detector_resource_attributes diff --git a/library/otel_traces_detector_jenkins.sh b/library/otel_traces_detector_jenkins.sh index ec32ed1..c2f6d46 100644 --- a/library/otel_traces_detector_jenkins.sh +++ b/library/otel_traces_detector_jenkins.sh @@ -15,8 +15,11 @@ # limitations under the License. . "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" -log_info "Detected, Jenkins pipeline" +log_info "Detected, Jenkins..." + +return_spaces_to_dashes "${REPO_NAME}-pipelines" "OTEL_SERVICE_NAME" detector_resource_attributes=( "jenkins.branch.name:${BRANCH_NAME}" diff --git a/library/otel_traces_detector_jenkins_x.sh b/library/otel_traces_detector_jenkins_x.sh new file mode 100644 index 0000000..cf467da --- /dev/null +++ b/library/otel_traces_detector_jenkins_x.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Jenkins X..." + +return_spaces_to_dashes "${REPO_NAME}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "jenkins.branch:${BRANCH_NAME}" + "jenkins.build.number:${BUILD_NUMBER}" + "jenkins.pull.request.number:${PULL_NUMBER}" + "jenkins.repo:${REPO_NAME}" +) diff --git a/library/otel_traces_detector_travis_ci.sh b/library/otel_traces_detector_travis_ci.sh new file mode 100644 index 0000000..b577f10 --- /dev/null +++ b/library/otel_traces_detector_travis_ci.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +# Copyright 2022 Krzysztof Kowalski + +# Licensed 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 + +# https://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. + +. "${OTEL_SH_LIB_PATH}/log.sh" +. "${OTEL_SH_LIB_PATH}/strings.sh" + +log_info "Detected, Travis CI..." + +return_spaces_to_dashes "${TRAVIS_REPO_SLUG}-pipelines" "OTEL_SERVICE_NAME" + +detector_resource_attributes=( + "travis.branch:${TRAVIS_BRANCH}" + "travis.build.number:${BUILD_NUMBER}" + "travis.build.url:${TRAVIS_BUILD_WEB_URL}" + "travis.pull.request:${TRAVIS_PULL_REQUEST}" + "travis.pull.request.branch:${TRAVIS_PULL_REQUEST_BRANCH}" + "travis.pull.request.repo:${TRAVIS_PULL_REQUEST_SLUG}" + "travis.repo:${TRAVIS_REPO_SLUG}" +) diff --git a/library/otel_traces_schema.sh b/library/otel_traces_schema.sh index f95d0ca..9859d07 100644 --- a/library/otel_traces_schema.sh +++ b/library/otel_traces_schema.sh @@ -22,7 +22,7 @@ otel_trace_resource_spans=$(cat </dev/null; then - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then log_debug "Using gdate..." fi epoch="$(gdate +%s.%N)" - elif [ ${EPOCHREALTIME} ]; then - if [ -z ${OTEL_LOG_LEVEL-} ]; then - log_debug 'Using $EPOCHREALTIME...' + elif [ "${EPOCHREALTIME}" ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then + log_debug "Using ${EPOCHREALTIME}..." fi epoch=$EPOCHREALTIME else - if [ -z ${OTEL_LOG_LEVEL-} ]; then + if [ -z "${OTEL_LOG_LEVEL-}" ]; then log_debug "Using date..." fi epoch=$(date +%s%N) diff --git a/library/uuid.sh b/library/uuid.sh index 96c5097..9c4ccaf 100644 --- a/library/uuid.sh +++ b/library/uuid.sh @@ -25,7 +25,7 @@ ####################################### generate_uuid() { - local length=$1 + # local length=$1 local N B C='89ab'