Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding resource detectors and more #45

Merged
merged 13 commits into from
Aug 1, 2022
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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`

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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_RESOURCE_ATTRIBUTES` - Key-value pairs to be used as resource attributes -->
<!-- - `OTEL_SERVICE_NAME`: Sets the value of the `service.name` resource attribute -->
- `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)**
Expand Down
4 changes: 2 additions & 2 deletions library/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

#######################################
Expand All @@ -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"
Expand Down
21 changes: 18 additions & 3 deletions library/otel_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
48 changes: 24 additions & 24 deletions library/otel_metrics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions library/otel_metrics_schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

#######################################
Expand All @@ -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")
}

#######################################
Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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")
}
Loading