istio.io/istio@v0.0.0-20240520182934-d79c90f27776/common/scripts/tracing.sh (about)

     1  #!/bin/bash
     2  
     3  # WARNING: DO NOT EDIT, THIS FILE IS PROBABLY A COPY
     4  #
     5  # The original version of this file is located in the https://github.com/istio/common-files repo.
     6  # If you're looking at this file in a different repo and want to make a change, please go to the
     7  # common-files repo, make the change there and check it in. Then come back to this repo and run
     8  # "make update-common".
     9  
    10  # Copyright Istio Authors
    11  #
    12  # Licensed under the Apache License, Version 2.0 (the "License");
    13  # you may not use this file except in compliance with the License.
    14  # You may obtain a copy of the License at
    15  #
    16  #    http://www.apache.org/licenses/LICENSE-2.0
    17  #
    18  # Unless required by applicable law or agreed to in writing, software
    19  # distributed under the License is distributed on an "AS IS" BASIS,
    20  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21  # See the License for the specific language governing permissions and
    22  # limitations under the License.
    23  
    24  # Usage: tracing::extract_prow_trace.
    25  # If running in a prow job, this sets the parent trace to the same value Prow tracing will use, as defined in https://github.com/kubernetes/test-infra/issues/30010
    26  function tracing::extract_prow_trace() {
    27    if [[ "${PROW_JOB_ID:-}" != "" ]]; then
    28      local trace
    29      trace="$(<<< "$PROW_JOB_ID" tr -d '\-')"
    30      local span
    31      span="${trace:0:16}"
    32      export TRACEPARENT="01-${trace}-${span}-00"
    33    fi
    34  }
    35  
    36  function _genattrs() {
    37    # No upstream standard, so copy from https://github.com/jenkinsci/opentelemetry-plugin/blob/master/docs/job-traces.md
    38    if [[ -n "${PULL_NUMBER:=}" ]]
    39    then
    40      # Presubmit
    41      url="https://prow.istio.io/view/gs/istio-prow/pr-logs/pull/${REPO_OWNER}_${REPO_NAME}/${PULL_NUMBER}/${JOB_NAME}/${BUILD_ID},"
    42    else
    43      # Postsubmit or periodic
    44      url="https://prow.istio.io/view/gs/istio-prow/pr-logs/${JOB_NAME}/${BUILD_ID},"
    45    fi
    46    # Use printf instead of echo to avoid spaces between args
    47    printf '%s' "ci.pipeline.id=${JOB_NAME},"\
    48      "ci.pipeline.type=${JOB_TYPE},"\
    49      "ci.pipeline.run.url=${url}"\
    50      "ci.pipeline.run.number=${BUILD_ID},"\
    51      "ci.pipeline.run.id=${PROW_JOB_ID},"\
    52      "ci.pipeline.run.repo=${REPO_OWNER:-unknown}/${REPO_NAME:-unknown},"\
    53      "ci.pipeline.run.base=${PULL_BASE_REF:-none},"\
    54      "ci.pipeline.run.pull_number=${PULL_NUMBER:-none},"\
    55      "ci.pipeline.run.pull_sha=${PULL_PULL_SHA:-${PULL_BASE_SHA:-none}}"
    56  }
    57  
    58  # Usage: tracing::run <span name> [command ...]
    59  function tracing::run() {
    60    # If not running in a prow job or otel-cli is not available (e.g. build system without otel-cli) just run the command
    61    if [ -z "${JOB_NAME:-}" ] || ! command -v otel-cli &> /dev/null
    62    then
    63      "${@:2}"
    64      return "$?"
    65    fi
    66  
    67    # Disable execution tracing to avoid noise
    68    { [[ $- = *x* ]] && was_execution_trace=1 || was_execution_trace=0; } 2>/dev/null
    69    { set +x; } 2>/dev/null
    70    # Throughout, "local" usage is critical to avoid nested calls overwriting things
    71    local start
    72    start="$(date -u +%s.%N)"
    73    # First, get a trace and span ID. We need to get one now so we can propagate it to the child
    74    # Get trace ID from TRACEPARENT, if present
    75    local tid
    76    tid="$(<<<"${TRACEPARENT:-}" cut -d- -f2)"
    77    tid="${tid:-"$(tr -dc 'a-f0-9' < /dev/urandom | head -c 32)"}"
    78    # Always generate a new span ID
    79    local sid
    80    sid="$(tr -dc 'a-f0-9' < /dev/urandom | head -c 16)"
    81  
    82    # Execute the command they wanted with the propagation through TRACEPARENT
    83    if [[ $was_execution_trace == 1 ]]; then
    84      { set -x; } 2>/dev/null
    85    fi
    86  
    87    TRACEPARENT="00-${tid}-${sid}-01" "${@:2}"
    88    local result="$?"
    89    { set +x; } 2>/dev/null
    90  
    91    local end
    92    end="$(date -u +%s.%N)"
    93  
    94    # Now report this span. We override the IDs to the ones we set before.
    95    otel-cli span \
    96      --service "${BASH_SOURCE[-1]}" \
    97      --name "$1" \
    98      --start "$start" \
    99      --end "$end" \
   100      --force-trace-id "$tid" \
   101      --force-span-id "$sid" \
   102      --attrs "$(_genattrs)"
   103    if [[ $was_execution_trace == 1 ]]; then
   104      { set -x; } 2>/dev/null
   105    fi
   106    return "$result"
   107  }
   108  
   109  # Usage: tracing::decorate <function>
   110  # Automatically makes a function traced.
   111  function tracing::decorate() {
   112  eval "\
   113  function $1() {
   114  _$(typeset -f "$1")
   115  tracing::run '$1' _$1
   116  }
   117  "
   118  }