Skip to content

Commit f036c56

Browse files
Automator: update common-files@master in istio/api@master (#2870)
1 parent 358b906 commit f036c56

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

common/.commonfiles.sha

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6bf196b12841981a692b6951a96c31c78c987624
1+
fcd31eb2297bc18a96ee4bafefcb3e1e298dc330

common/scripts/tracing.sh

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
url="https://prow.istio.io/view/gs/istio-prow/pr-logs/pull/${REPO_OWNER}_${REPO_NAME}/${PULL_NUMBER}/${JOB_NAME}/${BUILD_ID},"
41+
else
42+
url="https://prow.istio.io/view/gs/istio-prow/pr-logs/${JOB_NAME}/${BUILD_ID},"
43+
fi
44+
# Use printf instead of echo to avoid spaces between args
45+
printf '%s' "ci.pipeline.id=${JOB_NAME},"\
46+
"ci.pipeline.type=${JOB_TYPE},"\
47+
"ci.pipeline.run.url=${url}"\
48+
"ci.pipeline.run.number=${BUILD_ID},"\
49+
"ci.pipeline.run.id=${PROW_JOB_ID},"\
50+
"ci.pipeline.run.repo=${REPO_OWNER}/${REPO_NAME},"\
51+
"ci.pipeline.run.base=${PULL_BASE_REF},"\
52+
"ci.pipeline.run.pull_number=${PULL_NUMBER},"\
53+
"ci.pipeline.run.pull_sha=${PULL_PULL_SHA:-${PULL_BASE_SHA:-none}}"
54+
}
55+
56+
# Usage: tracing::run <span name> [command ...]
57+
function tracing::run() {
58+
# If not running in a prow job or otel-cli is not available (e.g. build system without otel-cli) just run the command
59+
if [ -z "${JOB_NAME:-}" ] || ! command -v otel-cli &> /dev/null
60+
then
61+
"${@:2}"
62+
return "$?"
63+
fi
64+
65+
# Disable execution tracing to avoid noise
66+
{ [[ $- = *x* ]] && was_execution_trace=1 || was_execution_trace=0; } 2>/dev/null
67+
{ set +x; } 2>/dev/null
68+
# Throughout, "local" usage is critical to avoid nested calls overwriting things
69+
local start
70+
start="$(date -u +%s.%N)"
71+
# First, get a trace and span ID. We need to get one now so we can propagate it to the child
72+
# Get trace ID from TRACEPARENT, if present
73+
local tid
74+
tid="$(<<<"${TRACEPARENT:-}" cut -d- -f2)"
75+
tid="${tid:-"$(tr -dc 'a-f0-9' < /dev/urandom | head -c 32)"}"
76+
# Always generate a new span ID
77+
local sid
78+
sid="$(tr -dc 'a-f0-9' < /dev/urandom | head -c 16)"
79+
80+
# Execute the command they wanted with the propagation through TRACEPARENT
81+
if [[ $was_execution_trace == 1 ]]; then
82+
{ set -x; } 2>/dev/null
83+
fi
84+
85+
TRACEPARENT="00-${tid}-${sid}-01" "${@:2}"
86+
local result="$?"
87+
{ set +x; } 2>/dev/null
88+
89+
local end
90+
end="$(date -u +%s.%N)"
91+
92+
# Now report this span. We override the IDs to the ones we set before.
93+
otel-cli span \
94+
--service "${BASH_SOURCE[-1]}" \
95+
--name "$1" \
96+
--start "$start" \
97+
--end "$end" \
98+
--force-trace-id "$tid" \
99+
--force-span-id "$sid" \
100+
--attrs "$(_genattrs)"
101+
if [[ $was_execution_trace == 1 ]]; then
102+
{ set -x; } 2>/dev/null
103+
fi
104+
return "$result"
105+
}
106+
107+
# Usage: tracing::decorate <function>
108+
# Automatically makes a function traced.
109+
function tracing::decorate() {
110+
eval "\
111+
function $1() {
112+
_$(typeset -f "$1")
113+
tracing::run '$1' _$1
114+
}
115+
"
116+
}

0 commit comments

Comments
 (0)