├── examples ├── jenkins │ └── .keep ├── github_actions │ ├── simple.yaml │ └── strategy_matrix.yaml └── shell │ ├── basic_trace_sleep.sh │ ├── basic_trace_errorlevel_test.sh │ ├── basic_trace_httpbin.sh │ ├── basic_metrics_sum.sh │ └── basic_metrics_gauge.sh ├── tests ├── data │ ├── logs │ │ └── .gitkeep │ ├── metrics │ │ └── Dockerfile │ └── traces │ │ └── Dockerfile └── otelcol │ └── config.yaml ├── .github ├── CODEOWNERS ├── dependabot.yaml ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md └── workflows │ └── release.yaml ├── go ├── go.mod └── main.go ├── docs └── images │ └── readme │ ├── gcp_cloud_tracing.png │ └── gcp_metrics_explorer.png ├── Dockerfile.alpine ├── Dockerfile.debian ├── .editorconfig ├── library ├── otel_logs.sh ├── otel_logs_schema.sh ├── otel_exporters.sh ├── otel_ver.sh ├── strings.sh ├── otel_traces_detector_jenkins_x.sh ├── otel_traces_detector_google_cloud_build.sh ├── otel_traces_detector_buildkite.sh ├── otel_traces_detector_travis_ci.sh ├── otel_traces_detector_bitbucket_pipelines.sh ├── otel_traces_detector_circle_ci.sh ├── otel_traces_detector_azure_pipelines.sh ├── time.sh ├── uuid.sh ├── otel_traces_detector_gitlab_ci.sh ├── otel_traces_detector_jenkins.sh ├── otel_traces_detector_github_actions.sh ├── net.sh ├── otel_traces_detector.sh ├── otel_traces_detector_harness.sh ├── otel_init.sh ├── log.sh ├── otel_metrics.sh ├── otel_traces_schema.sh ├── otel_traces.sh └── otel_metrics_schema.sh ├── docker-compose.yaml ├── .gitignore ├── Makefile ├── .goreleaser.yaml ├── CONTRIBUTING.md ├── CODE_OF_CONDUCT.md ├── README.md └── LICENSE /examples/jenkins/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/data/logs/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @krzko 2 | -------------------------------------------------------------------------------- /examples/github_actions/simple.yaml: -------------------------------------------------------------------------------- 1 | # 2 | -------------------------------------------------------------------------------- /go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/krzko/opentelemetry-shell 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /docs/images/readme/gcp_cloud_tracing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krzko/opentelemetry-shell/HEAD/docs/images/readme/gcp_cloud_tracing.png -------------------------------------------------------------------------------- /go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Print("Came for the scripts, stayed for goreleaser") 7 | } 8 | -------------------------------------------------------------------------------- /docs/images/readme/gcp_metrics_explorer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krzko/opentelemetry-shell/HEAD/docs/images/readme/gcp_metrics_explorer.png -------------------------------------------------------------------------------- /tests/data/metrics/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3 2 | 3 | RUN apk add --update-cache bash curl jq uuidgen 4 | 5 | COPY . /root/opentelemetry-shell 6 | 7 | WORKDIR /root/opentelemetry-shell/examples 8 | 9 | ENTRYPOINT ["bash"] 10 | -------------------------------------------------------------------------------- /tests/data/traces/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3 2 | 3 | RUN apk add --update-cache bash curl jq 4 | 5 | COPY . /root/opentelemetry-shell 6 | 7 | RUN chmod +x /root/opentelemetry-shell/*.sh 8 | 9 | WORKDIR /root/opentelemetry-shell/examples 10 | 11 | ENTRYPOINT ["bash"] 12 | -------------------------------------------------------------------------------- /Dockerfile.alpine: -------------------------------------------------------------------------------- 1 | FROM alpine:3 2 | 3 | RUN apk add --update-cache bash curl jq 4 | 5 | COPY examples /root/opentelemetry-shell/examples/ 6 | COPY library /root/opentelemetry-shell/library/ 7 | 8 | WORKDIR /root/opentelemetry-shell/examples 9 | 10 | ENTRYPOINT ["bash"] 11 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: docker 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "04:00" 8 | open-pull-requests-limit: 10 9 | - package-ecosystem: github-actions 10 | directory: "/" 11 | schedule: 12 | interval: daily 13 | time: "04:00" 14 | open-pull-requests-limit: 10 15 | -------------------------------------------------------------------------------- /Dockerfile.debian: -------------------------------------------------------------------------------- 1 | FROM debian:11-slim 2 | 3 | RUN apt-get update \ 4 | && apt-get install -y --no-install-recommends jq \ 5 | && rm -rf /var/lib/apt/lists/* 6 | 7 | COPY examples /root/opentelemetry-shell/examples/ 8 | COPY library /root/opentelemetry-shell/library/ 9 | 10 | WORKDIR /root/opentelemetry-shell/examples 11 | 12 | ENTRYPOINT ["bash"] 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.json] 15 | indent_size = 2 16 | 17 | [*.js] 18 | indent_size = 2 19 | 20 | [*.jsx] 21 | indent_size = 2 22 | 23 | [*.scss] 24 | indent_size = 2 25 | 26 | [*.css] 27 | indent_size = 2 28 | 29 | [*.sh] 30 | indent_size = 2 31 | 32 | [*.yml] 33 | indent_size = 2 34 | 35 | [Makefile] 36 | indent_style = tab 37 | -------------------------------------------------------------------------------- /tests/otelcol/config.yaml: -------------------------------------------------------------------------------- 1 | receivers: 2 | otlp: 3 | protocols: 4 | http: 5 | endpoint: 0.0.0.0:4318 6 | 7 | processors: 8 | batch: 9 | 10 | exporters: 11 | logging: 12 | loglevel: debug 13 | sampling_initial: 1 14 | 15 | service: 16 | pipelines: 17 | logs: 18 | receivers: [otlp] 19 | processors: [batch] 20 | exporters: [logging] 21 | metrics: 22 | receivers: [otlp] 23 | processors: [batch] 24 | exporters: [logging] 25 | traces: 26 | receivers: [otlp] 27 | processors: [batch] 28 | exporters: [logging] 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: 'feature' 6 | assignees: '' 7 | 8 | --- 9 | 10 | #### Is your feature request related to a problem? 11 | 12 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 13 | 14 | #### Describe the solution you'd like 15 | 16 | A clear and concise description of what you want to happen. 17 | 18 | #### Describe alternatives you've considered 19 | 20 | A clear and concise description of any alternative solutions or features you've considered. 21 | 22 | #### Additional context 23 | 24 | Add any other context or screenshots about the feature request here. 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: 'bug' 6 | assignees: '' 7 | 8 | --- 9 | 10 | #### Describe the bug 11 | 12 | A clear and concise description of what the bug is. 13 | 14 | #### To Reproduce 15 | 16 | Steps to reproduce the behavior: 17 | 18 | 1. I ran '...' 19 | 2. See error 20 | 21 | #### Expected behavior 22 | 23 | A clear and concise description of what you expected to happen. 24 | 25 | #### Screenshots 26 | 27 | If applicable, add screenshots to help explain your problem. 28 | 29 | #### Environment 30 | 31 | - OS: [e.g. macOS, Linux] 32 | - Shell version [e.g. 3.x, 4.x, 5.x] 33 | 34 | #### Additional context 35 | 36 | Add any other context about the problem here. 37 | -------------------------------------------------------------------------------- /library/otel_logs.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # https://opentelemetry.io/docs/reference/specification/logs/overview/ 18 | # https://opentelemetry.io/docs/reference/specification/logs/data-model/ 19 | -------------------------------------------------------------------------------- /library/otel_logs_schema.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # https://opentelemetry.io/docs/reference/specification/logs/overview/ 18 | # https://opentelemetry.io/docs/reference/specification/logs/data-model/ 19 | -------------------------------------------------------------------------------- /library/otel_exporters.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # console_exporter 18 | # otlp_exporeter 19 | 20 | # TODO: Create getter for exporters 21 | # otel_trace_get_exporter() { 22 | # log_info "" 23 | # } 24 | 25 | # TODO: Create setter for exporters 26 | # otel_trace_set_exporter() { 27 | # log_info "" 28 | # } 29 | -------------------------------------------------------------------------------- /library/otel_ver.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | export telemetry_sdk_ver="0.0.9" 18 | 19 | ####################################### 20 | # Returns a version of the SDK 21 | # OUTPUTS: 22 | # Write to stdout 23 | ####################################### 24 | otel_sh_ver() { 25 | printf "OpenTelemetry Shell v%s\n" $telemetry_sdk_ver 26 | } 27 | 28 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | branches: 8 | - main 9 | 10 | env: 11 | GO_VERSION: "1.18" 12 | DOCKER_REGISTRY: "ghcr.io" 13 | 14 | jobs: 15 | goreleaser: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v3 20 | with: 21 | fetch-depth: 0 22 | 23 | - name: Set up Go 24 | uses: actions/setup-go@v4 25 | with: 26 | go-version: ${{ env.GO_VERSION }} 27 | 28 | - name: Login to GitHub Packages Docker Registry 29 | uses: docker/login-action@v2 30 | with: 31 | registry: ${{ env.DOCKER_REGISTRY }} 32 | username: ${{ github.repository_owner }} 33 | password: ${{ secrets.GITHUB_TOKEN }} 34 | 35 | - name: Run GoReleaser 36 | uses: goreleaser/goreleaser-action@v4 37 | with: 38 | version: latest 39 | args: release --clean 40 | env: 41 | GITHUB_TOKEN: ${{ secrets.GORELEASER_TOKEN }} 42 | -------------------------------------------------------------------------------- /library/strings.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | 19 | ####################################### 20 | # Returns a formatted strings and assigns back to variable 21 | # ARGUMENTS: 22 | # str, the string 23 | # var, the vveriable to return to 24 | # OUTPUTS: 25 | # Return to argument var 26 | ####################################### 27 | return_spaces_to_dashes() { 28 | local str="$1" 29 | local var="$2" 30 | 31 | str=${str// /_} 32 | 33 | eval "$var=\$str" 34 | } 35 | -------------------------------------------------------------------------------- /library/otel_traces_detector_jenkins_x.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Jenkins X..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${REPO_NAME}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "jenkins.branch:${BRANCH_NAME}" 28 | "jenkins.build.number:${BUILD_NUMBER}" 29 | "jenkins.pull.request.number:${PULL_NUMBER}" 30 | "jenkins.repo:${REPO_NAME}" 31 | ) 32 | -------------------------------------------------------------------------------- /library/otel_traces_detector_google_cloud_build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Google Cloud Build..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${REPO_NAME}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "cloud.build.branch:${BRANCH_NAME}" 28 | "cloud.build.build.number:${BUILD_ID}" 29 | "cloud.build.pull.request.branch:${HEAD_BRANCH}" 30 | "cloud.build.repo:${REPO_NAME}" 31 | "cloud.build.repo.owner:${REPO_OWNER}" 32 | ) 33 | -------------------------------------------------------------------------------- /library/otel_traces_detector_buildkite.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Buildkite..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${BUILDKITE_REPO}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "buildkite.branch:${BUILDKITE_BRANCH}" 28 | "buildkite.build.number:${BUILDKITE_BUILD_NUMBER}" 29 | "buildkite.build.url:${BUILDKITE_BUILD_URL}" 30 | "buildkite.pull.request.number:${BUILDKITE_PULL_REQUEST}" 31 | "buildkite.pull.request.repo:${BUILDKITE_PULL_REQUEST_REPO}" 32 | "buildkite.repo:${BUILDKITE_REPO}" 33 | ) 34 | -------------------------------------------------------------------------------- /library/otel_traces_detector_travis_ci.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Travis CI..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${TRAVIS_REPO_SLUG}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "travis.branch:${TRAVIS_BRANCH}" 28 | "travis.build.number:${BUILD_NUMBER}" 29 | "travis.build.url:${TRAVIS_BUILD_WEB_URL}" 30 | "travis.pull.request:${TRAVIS_PULL_REQUEST}" 31 | "travis.pull.request.branch:${TRAVIS_PULL_REQUEST_BRANCH}" 32 | "travis.pull.request.repo:${TRAVIS_PULL_REQUEST_SLUG}" 33 | "travis.repo:${TRAVIS_REPO_SLUG}" 34 | ) 35 | -------------------------------------------------------------------------------- /library/otel_traces_detector_bitbucket_pipelines.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Bitbucket Pipelines..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${BITBUCKET_REPO_FULL_NAME}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "buildkite.branch:${BITBUCKET_BRANCH}" 28 | "buildkite.build.number:${BITBUCKET_BUILD_NUMBER}" 29 | "buildkite.build.user:${BITBUCKET_STEP_TRIGGERER_UUID}" 30 | "buildkite.pipeline.id:${BITBUCKET_PIPELINE_UUID}" 31 | "buildkite.pull.request.id:${BITBUCKET_PR_ID}" 32 | "buildkite.repo:${BITBUCKET_REPO_FULL_NAME}" 33 | ) 34 | -------------------------------------------------------------------------------- /library/otel_traces_detector_circle_ci.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Circle CI..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${CIRCLE_REPOSITORY_URL}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "circle.ci.branch:${CIRCLE_BRANCH}" 28 | "circle.ci.build.number:${CIRCLE_BUILD_NUM}" 29 | "circle.ci.build.url:${CIRCLE_BUILD_URL}" 30 | "circle.ci.job.name:${CIRCLE_JOB}" 31 | "circle.ci.pull.request.number:${CIRCLE_PR_NUMBER}" 32 | "circle.ci.pull.request.user:${CIRCLE_PR_USER}" 33 | "circle.ci.pull.request.repo:${CIRCLE_PR_REPONAME}" 34 | "circle.ci.repo:${CIRCLE_REPOSITORY_URL}" 35 | ) 36 | -------------------------------------------------------------------------------- /examples/shell/basic_trace_sleep.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Add resource attributes 18 | # Use custom_resource_attributes specifically, opioninated 19 | custom_resource_attributes=( 20 | "environment:dev" 21 | "team:ops" 22 | "support:#ops-support" 23 | ) 24 | 25 | # Service variables 26 | service_version="0.0.1-dev" 27 | 28 | # Export library path 29 | export OTEL_SH_LIB_PATH="../../library" 30 | 31 | # Import functions 32 | . "${OTEL_SH_LIB_PATH}/otel_traces.sh" 33 | 34 | # Functions 35 | sleep_for() { 36 | local sec=$1 37 | 38 | log_info "Sleeping for ${sec} sec..." 39 | sleep $sec 40 | } 41 | 42 | # Main 43 | otel_trace_start_parent_span sleep_for 1 44 | 45 | otel_trace_start_child_span sleep_for 2 46 | 47 | otel_trace_start_child_span sleep_for 1 48 | 49 | log_info "TraceId: ${OTEL_TRACE_ID}" 50 | -------------------------------------------------------------------------------- /library/otel_traces_detector_azure_pipelines.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Azure Pipelines..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${BUILD_REPOSITORY_URI}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "azure.branch:${BUILD_SOURCEBRANCHNAME}" 28 | "azure.build.id:${BUILD_BUILDID}" 29 | "azure.build.number:${BUILD_BUILDNUMBER}" 30 | "azure.job.name:${SYSTEM_JOBDISPLAYNAME}" 31 | "azure.stage.name:${SYSTEM_STAGEDISPLAYNAME}" 32 | "azure.pull.request.id:${SYSTEM_PULLREQUEST_PULLREQUESTID}" 33 | "azure.pull.request.number:${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}" 34 | "azure.build.user:${BUILD_REQUESTEDFOR}" 35 | "azure.repo:${BUILD_REPOSITORY_URI}" 36 | ) 37 | -------------------------------------------------------------------------------- /library/time.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | 19 | ####################################### 20 | # Gets the Unix epoch timesyamp in nanoseconds 21 | # GLOBALS: 22 | # EPOCHREALTIME 23 | # OUTPUTS: 24 | # Write to stdout 25 | ####################################### 26 | get_epoch_now() { 27 | local epoch="" 28 | 29 | if hash gdate 2>/dev/null; then 30 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 31 | log_debug "Using gdate..." 32 | fi 33 | epoch="$(gdate +%s.%N)" 34 | elif [ -n "${EPOCHREALTIME-}" ]; then 35 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 36 | log_debug "Using ${EPOCHREALTIME}..." 37 | fi 38 | epoch=$EPOCHREALTIME 39 | else 40 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 41 | log_debug "Using date..." 42 | fi 43 | epoch=$(date +%s%N) 44 | fi 45 | 46 | echo "${epoch//.}" 47 | } 48 | -------------------------------------------------------------------------------- /examples/shell/basic_trace_errorlevel_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Add resource attributes 18 | # Use custom_resource_attributes specifically, opioninated 19 | custom_resource_attributes=( 20 | "environment:dev" 21 | "team:ops" 22 | "support:#ops-support" 23 | ) 24 | 25 | # Service variables 26 | service_version="0.0.1-dev" 27 | 28 | # Export library path 29 | export OTEL_SH_LIB_PATH="../../library" 30 | 31 | # Import functions 32 | . "${OTEL_SH_LIB_PATH}/otel_traces.sh" 33 | 34 | # Functions 35 | good() { 36 | local sec=$1 37 | 38 | log_info "Sleeping for ${sec} sec..." 39 | sleep $sec 40 | } 41 | 42 | bad() { 43 | local sec=$1 44 | 45 | log_info "Sleeping for ${sec} sec..." 46 | sleeeeeep $sec 47 | } 48 | 49 | # Main 50 | otel_trace_start_parent_span good 1 51 | 52 | otel_trace_start_child_span bad 2 53 | 54 | log_info "TraceId: ${OTEL_TRACE_ID}" 55 | -------------------------------------------------------------------------------- /examples/shell/basic_trace_httpbin.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Add resource attributes 18 | # Use custom_resource_attributes specifically, opioninated 19 | custom_resource_attributes=( 20 | "environment:dev" 21 | "team:ops" 22 | "support:#ops-support" 23 | ) 24 | 25 | # Service variables 26 | service_version="0.0.1-dev" 27 | 28 | # Export library path 29 | export OTEL_SH_LIB_PATH="../../library" 30 | 31 | # Import functions 32 | . "${OTEL_SH_LIB_PATH}/otel_traces.sh" 33 | 34 | # Functions 35 | curl_httpbin() { 36 | local status=$1 37 | 38 | log_info "curl -X GET https://httpbin.org/status/${status} -H 'accept: text/plain'" 39 | curl -X GET "https://httpbin.org/status/${status}" -H "accept: text/plain" 40 | } 41 | 42 | # Main 43 | otel_trace_start_parent_span curl_httpbin 200 44 | 45 | otel_trace_start_child_span curl_httpbin 201 46 | 47 | otel_trace_start_child_span curl_httpbin 203 48 | 49 | log_info "TraceId: ${OTEL_TRACE_ID}" 50 | -------------------------------------------------------------------------------- /library/uuid.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | ####################################### 18 | # Generates a UUID based on a desired length 19 | # GLOBALS: 20 | # RANDOM 21 | # ARGUMENTS: 22 | # length, the length of the UUID to generate 23 | # OUTPUTS: 24 | # Write to stdout 25 | ####################################### 26 | generate_uuid() 27 | { 28 | # local length=$1 29 | 30 | local N B C='89ab' 31 | 32 | for (( N=0; N < $1; ++N )) 33 | do 34 | B=$(( $RANDOM%256 )) 35 | 36 | case $N in 37 | 6) 38 | printf '4%x' $(( B%$1 )) 39 | ;; 40 | 8) 41 | printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%$1 )) 42 | ;; 43 | 3 | 5 | 7 | 9) 44 | # Add dashes '%02x-' 45 | printf '%02x' $B 46 | ;; 47 | *) 48 | printf '%02x' $B 49 | ;; 50 | esac 51 | done 52 | 53 | echo 54 | } 55 | -------------------------------------------------------------------------------- /library/otel_traces_detector_gitlab_ci.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Gitlab CI..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${CI_PROJECT_URL}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | if [ -n "${detector_resource_attributes-}" ]; then 27 | detector_resource_attributes=() 28 | fi 29 | 30 | detector_resource_attributes+=( 31 | "gitlab.ci.branch:${CI_COMMIT_REF_NAME}" 32 | "gitlab.ci.build.number:${CI_PIPELINE_ID}" 33 | "gitlab.ci.build.url:${CI_PIPELINE_URL}" 34 | "gitlab.ci.repo:${CI_PROJECT_URL}" 35 | ) 36 | 37 | if [ -n "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME-}" ]; then 38 | detector_resource_attributes+=( 39 | "gitlab.ci.pull.request.branch:${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}" 40 | "gitlab.ci.pull.request.number:${CI_MERGE_REQUEST_ID}" 41 | "gitlab.ci.pull.request.repo:${CI_MERGE_REQUEST_SOURCE_PROJECT_PATH}" 42 | ) 43 | fi 44 | -------------------------------------------------------------------------------- /library/otel_traces_detector_jenkins.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Jenkins..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${JOB_NAME}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "jenkins.branch.name:${BRANCH_NAME}" 28 | "jenkins.build.id:${BUILD_ID}" 29 | "jenkins.build.number:${BUILD_NUMBER}" 30 | "jenkins.build.tag:${BUILD_TAG}" 31 | "jenkins.build.url:${BUILD_URL}" 32 | "jenkins.change.author:${CHANGE_AUTHOR}" 33 | "jenkins.executor.number:${EXECUTOR_NUMBER}" 34 | "jenkins.git.branch:${GIT_BRANCH}" 35 | "jenkins.git.commit:${GIT_COMMIT}" 36 | "jenkins.hostname:${HOSTNAME}" 37 | "jenkins.java.home:${JAVA_HOME}" 38 | "jenkins.job.name:${JOB_NAME}" 39 | "jenkins.job.url:${JOB_URL}" 40 | "jenkins.node.name:${NODE_NAME}" 41 | "jenkins.url:${JENKINS_URL}" 42 | "jenkins.version:${JENKINS_VERSION}" 43 | "jenkins.workspace:${WORKSPACE}" 44 | ) 45 | -------------------------------------------------------------------------------- /library/otel_traces_detector_github_actions.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, GitHub Actions..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${GITHUB_REPOSITORY}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | detector_resource_attributes=( 27 | "github.action:${GITHUB_ACTION}" 28 | "github.action.repository:${GITHUB_ACTION_REPOSITORY}" 29 | "github.actor:${GITHUB_ACTOR}" 30 | "github.event.name:${GITHUB_EVENT_NAME}" 31 | "github.job:${GITHUB_JOB}" 32 | "github.ref:${GITHUB_REF}" 33 | "github.ref.name:${GITHUB_REF_NAME}" 34 | "github.repository:${GITHUB_REPOSITORY}" 35 | "github.repository.owner:${GITHUB_REPOSITORY_OWNER}" 36 | "github.run.number:${GITHUB_RUN_NUMBER}" 37 | "github.sha:${GITHUB_SHA}" 38 | "github.workflow:${GITHUB_WORKFLOW}" 39 | "github.workspace:${GITHUB_WORKSPACE}" 40 | "github.runner.arch:${RUNNER_ARCH}" 41 | "github.runner.name:${RUNNER_NAME}" 42 | "github.runner.os:${RUNNER_OS}" 43 | ) 44 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | # Used for testing 4 | 5 | services: 6 | otelcol: 7 | container_name: otelcol 8 | image: otel/opentelemetry-collector:0.54.0 9 | command: ["--config=/etc/otelcol/config.yaml"] 10 | ports: 11 | - "4318:4318" 12 | volumes: 13 | - ./tests/otelcol/config.yaml:/etc/otelcol/config.yaml 14 | 15 | otel-sh-metrics-gauge: 16 | container_name: otel-sh-metrics-gauge 17 | build: 18 | context: . 19 | dockerfile: Dockerfile.alpine 20 | entrypoint: ["/usr/bin/env", "bash", "-c"] 21 | command: ./basic_metrics_gauge.sh 22 | environment: 23 | - OTEL_EXPORTER_OTEL_ENDPOINT=http://otelcol:4318 24 | # - OTEL_LOG_LEVEL=debug 25 | depends_on: 26 | otelcol: 27 | condition: service_started 28 | links: 29 | - "otelcol:otelcol" 30 | 31 | otel-sh-metrics-sum: 32 | container_name: otel-sh-metrics-sum 33 | build: 34 | context: . 35 | dockerfile: Dockerfile.alpine 36 | entrypoint: ["/usr/bin/env", "bash", "-c"] 37 | command: ./basic_metrics_sum.sh 38 | environment: 39 | - OTEL_EXPORTER_OTEL_ENDPOINT=http://otelcol:4318 40 | # - OTEL_LOG_LEVEL=debug 41 | depends_on: 42 | otelcol: 43 | condition: service_started 44 | links: 45 | - "otelcol:otelcol" 46 | 47 | otel-sh-trace: 48 | container_name: otel-sh-trace 49 | build: 50 | context: . 51 | dockerfile: Dockerfile.alpine 52 | entrypoint: ["/usr/bin/env", "bash", "-c"] 53 | command: ./basic_trace_sleep.sh 54 | environment: 55 | - OTEL_EXPORTER_OTEL_ENDPOINT=http://otelcol:4318 56 | # - OTEL_LOG_LEVEL=debug 57 | depends_on: 58 | otelcol: 59 | condition: service_started 60 | links: 61 | - "otelcol:otelcol" 62 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Custom 2 | 3 | # Created by https://www.toptal.com/developers/gitignore/api/macos,windows,visualstudiocode 4 | # Edit at https://www.toptal.com/developers/gitignore?templates=macos,windows,visualstudiocode 5 | 6 | ### macOS ### 7 | # General 8 | .DS_Store 9 | .AppleDouble 10 | .LSOverride 11 | 12 | # Icon must end with two \r 13 | Icon 14 | 15 | 16 | # Thumbnails 17 | ._* 18 | 19 | # Files that might appear in the root of a volume 20 | .DocumentRevisions-V100 21 | .fseventsd 22 | .Spotlight-V100 23 | .TemporaryItems 24 | .Trashes 25 | .VolumeIcon.icns 26 | .com.apple.timemachine.donotpresent 27 | 28 | # Directories potentially created on remote AFP share 29 | .AppleDB 30 | .AppleDesktop 31 | Network Trash Folder 32 | Temporary Items 33 | .apdisk 34 | 35 | ### macOS Patch ### 36 | # iCloud generated files 37 | *.icloud 38 | 39 | ### VisualStudioCode ### 40 | .vscode/* 41 | !.vscode/settings.json 42 | !.vscode/tasks.json 43 | !.vscode/launch.json 44 | !.vscode/extensions.json 45 | !.vscode/*.code-snippets 46 | 47 | # Local History for Visual Studio Code 48 | .history/ 49 | 50 | # Built Visual Studio Code Extensions 51 | *.vsix 52 | 53 | ### VisualStudioCode Patch ### 54 | # Ignore all local history of files 55 | .history 56 | .ionide 57 | 58 | # Support for Project snippet scope 59 | .vscode/*.code-snippets 60 | 61 | # Ignore code-workspaces 62 | *.code-workspace 63 | 64 | ### Windows ### 65 | # Windows thumbnail cache files 66 | Thumbs.db 67 | Thumbs.db:encryptable 68 | ehthumbs.db 69 | ehthumbs_vista.db 70 | 71 | # Dump file 72 | *.stackdump 73 | 74 | # Folder config file 75 | [Dd]esktop.ini 76 | 77 | # Recycle Bin used on file shares 78 | $RECYCLE.BIN/ 79 | 80 | # Windows Installer files 81 | *.cab 82 | *.msi 83 | *.msix 84 | *.msm 85 | *.msp 86 | 87 | # Windows shortcuts 88 | *.lnk 89 | 90 | # End of https://www.toptal.com/developers/gitignore/api/macos,windows,visualstudiocode 91 | -------------------------------------------------------------------------------- /examples/shell/basic_metrics_sum.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Add resource attributes 18 | # Use custom_resource_attributes specifically, opioninated 19 | custom_resource_attributes=( 20 | "environment:dev" 21 | "team:ops" 22 | "support:#ops-support" 23 | ) 24 | 25 | # Service variables 26 | service_version="0.0.1-dev" 27 | 28 | # Export library path 29 | export OTEL_SH_LIB_PATH="../../library" 30 | 31 | # Import functions 32 | . "${OTEL_SH_LIB_PATH}/otel_metrics.sh" 33 | 34 | # Sum of type double, metric 35 | log_info "Pushing metric ko.wal.ski/script/usage_time..." 36 | otel_metrics_push_sum "ko.wal.ski/script/usage_time" \ 37 | "Script usuage time in seconds." \ 38 | "s" \ 39 | "time" \ 40 | "latency" \ 41 | "${RANDOM}.616987465" \ 42 | double \ 43 | "$(date +%s)000000000" 44 | 45 | # Check script errorlevel (success) using gauge of type int, metric 46 | log_info "Checking errorlevel..." 47 | if [ $? -eq 0 ]; then 48 | log_info "Pushing metric ko.wal.ski/script/is_failed as 0" 49 | log_info "${0##*/} ran successfully" 50 | otel_metrics_push_gauge "ko.wal.ski/script/is_failed" \ 51 | "If the script was successful." \ 52 | "By" \ 53 | "script_name" \ 54 | "${0##*/}" \ 55 | 0 \ 56 | int 57 | else 58 | log_info "Pushing metric ko.wal.ski/script/is_failed as 1" 59 | log_error "${0##*/} failed" 60 | otel_metrics_push_gauge "ko.wal.ski/script/is_failed" \ 61 | "If the script was successful." \ 62 | "By" \ 63 | "script_name" \ 64 | "${0##*/}" \ 65 | 1 \ 66 | int 67 | fi 68 | -------------------------------------------------------------------------------- /library/net.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | 19 | ####################################### 20 | # A net client with a POST method 21 | # ARGUMENTS: 22 | # data, the data payload 23 | # url, the url to POST to 24 | ####################################### 25 | net_client_post() { 26 | local data=$1 27 | local url=$2 28 | local response="" 29 | 30 | # local fail=$3 # --fail 31 | # local connect_timeout=$4 # --connect-timeout 3 32 | # local retry=$5 # --retry 0 33 | 34 | if [[ "$OTEL_EXPORTER_OTEL_ENDPOINT" = https* ]]; then 35 | log_debug "curl -ik -X POST -H 'Content-Type: application/json' -d ${data} ${url}" 36 | set +e 37 | response=$(curl --fail -ik -X POST -H 'Content-Type: application/json' -d "${data}" "${url}" -o /dev/null -s -w "%{http_code}") 38 | if [ $? -eq 0 ]; then 39 | log_info "POST ${response} ${url}" 40 | else 41 | log_error "The requested URL returned an error: ${url}" 42 | fi 43 | elif [[ "$OTEL_EXPORTER_OTEL_ENDPOINT" = http* ]]; then 44 | log_debug "curl -X POST -H 'Content-Type: application/json' -d ${data} ${url}" 45 | set +e 46 | response=$(curl --fail -X POST -H 'Content-Type: application/json' -d "${data}" "${url}" -o /dev/null -s -w "%{http_code}") 47 | if [ $? -eq 0 ]; then 48 | log_info "POST ${response} ${url}" 49 | else 50 | log_error "The requested URL returned an error: ${url}" 51 | fi 52 | else 53 | log_fatal "OTEL_EXPORTER_OTEL_ENDPOINT needs to include a http:// or https:// prefix" 54 | exit 1 55 | fi 56 | } 57 | -------------------------------------------------------------------------------- /library/otel_traces_detector.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | 19 | # Azure Pipelines 20 | if [ "${TF_BUILD-}" ]; then 21 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_azure_pipelines.sh" 22 | fi 23 | 24 | # Bitbucket Pipelines 25 | if [ "${BITBUCKET_BUILD_NUMBER-}" ]; then 26 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_bitbucket_pipelines.sh" 27 | fi 28 | 29 | # Buildkite 30 | if [ "${BUILDKITE-}" ]; then 31 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_buildkite.sh" 32 | fi 33 | 34 | # Circle CI 35 | if [ "${CIRCLECI-}" ]; then 36 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_circle_ci.sh" 37 | fi 38 | 39 | # Github Actions 40 | if [ "${GITHUB_ACTIONS-}" ]; then 41 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_github_actions.sh" 42 | fi 43 | 44 | # Gitlab CI 45 | if [ "${GITLAB_CI-}" ]; then 46 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_gitlab_ci.sh" 47 | fi 48 | 49 | # Google Cloud Build 50 | # if [ "${GOOGLE-CLOUD-BUILD-}" ]; then 51 | # . "${OTEL_SH_LIB_PATH}/otel_traces_detector_google_cloud_build.sh" 52 | # fi 53 | 54 | # TODO: Harness 55 | # if [ "${DEPLOYMENT_ID-}" ]; then 56 | # . "${OTEL_SH_LIB_PATH}/otel_traces_detector_harness.sh" 57 | # fi 58 | 59 | # Jenkins 60 | if [ "${CI-}" ] && [ "${JENKINS_URL-}" ]; then 61 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_jenkins.sh" 62 | fi 63 | 64 | # Jenkins X 65 | # if [ "${JENKINS-X-}" ]; then 66 | # . "${OTEL_SH_LIB_PATH}/otel_traces_detector_jenkins_x.sh" 67 | # fi 68 | 69 | # Travis CI 70 | if [ "${TRAVIS-}" ]; then 71 | . "${OTEL_SH_LIB_PATH}/otel_traces_detector_travis_ci.sh" 72 | fi 73 | -------------------------------------------------------------------------------- /library/otel_traces_detector_harness.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/log.sh" 18 | . "${OTEL_SH_LIB_PATH}/strings.sh" 19 | 20 | log_info "Detected, Harness..." 21 | 22 | if [ "$OTEL_SERVICE_NAME" == "unknown_service" ]; then 23 | return_spaces_to_dashes "${REPO_NAME}" "OTEL_SERVICE_NAME" 24 | fi 25 | 26 | declare -t detector_resource_attributes 27 | 28 | detector_resource_attributes+=( 29 | "harness.app.name:${app.name}" 30 | "harness.app.desc:${app.description}" 31 | "harness.service.name:${service.name}" 32 | "harness.env.name:${env.name}" 33 | "harness.env.environmentType:${env.environmentType}" 34 | "harness.infra.name:${infra.name}" 35 | "harness.workflow.displayName:${workflow.displayName}" 36 | "harness.currentStep.name:${currentStep.name}" 37 | "harness.currentStep.type:${currentStep.type}" 38 | "harness.deploymentTriggeredBy:${deploymentTriggeredBy}" 39 | ) 40 | 41 | # Shell script step 42 | if [ ${context.published_name.var_name-} ]; then 43 | detector_resource_attributes+=( 44 | "harness.context.published_name.var_name:${context.published_name.var_name}" 45 | ) 46 | fi 47 | 48 | # Instance 49 | if [ ${instance.name-} ]; then 50 | detector_resource_attributes+=( 51 | "harness.instance.name:${instance.name}" 52 | "harness.instance.hostName:${instance.hostName}" 53 | ) 54 | fi 55 | 56 | # Approval 57 | if [ ${published_name.approvedBy.name-} ]; then 58 | detector_resource_attributes+=( 59 | "harness.published_name.approvedBy.name:${published_name.approvedBy.name}" 60 | "harness.published_name.approvedBy.email:${published_name.approvedBy.email}" 61 | "harness.published_name.approvedOn:${published_name.approvedOn}" 62 | ) 63 | fi 64 | -------------------------------------------------------------------------------- /library/otel_init.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -euo pipefail 18 | 19 | if [ -z "${OTEL_SH_LIB_PATH-}" ]; then 20 | export OTEL_SH_LIB_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 21 | fi 22 | 23 | . "${OTEL_SH_LIB_PATH}/log.sh" 24 | . "${OTEL_SH_LIB_PATH}/otel_ver.sh" 25 | 26 | export os_version=$(uname -a) 27 | export hostname=$(hostname) 28 | 29 | export telemetry_sdk_name="opentelemetry.sh" 30 | export telemetry_sdk_lang="bash" 31 | 32 | printf "\n" 33 | printf " _____ _____ _____ _____ _____ _____ __ _____ _____ _____ _____ _____ __ __ \n" 34 | printf "| | _ | __| | |_ _| __| | | __| | __|_ _| __ | | |\n" 35 | printf "| | | __| __| | | | | | | __| |__| __| | | | __| | | | -|_ _|\n" 36 | printf "|_____|__| |_____|_|___| |_| |_____|_____|_____|_|_|_|_____| |_| |__|__| |_| \n" 37 | printf " _____ _____ _____ __ __ \n" 38 | printf "| __| | | __| | | | \n" 39 | printf "|__ | | __| |__| |__ \n" 40 | printf "|_____|__|__|_____|_____|_____| \n" 41 | printf "\n" 42 | 43 | log_info "Initialising OpenTelemetry Shell v${telemetry_sdk_ver}" 44 | 45 | # OTEL_EXPORTER_OTLP_TRACES_ENDPOINT 46 | # OTEL_EXPORTER_OTLP_METRICS_ENDPOINT 47 | # OTEL_EXPORTER_OTLP_LOGS_ENDPOINT 48 | 49 | if [ -z "${OTEL_EXPORTER_OTEL_ENDPOINT-}" ]; then 50 | log_error "OTEL_EXPORTER_OTEL_ENDPOINT not exported" 51 | exit 1 52 | fi 53 | 54 | if [ -z "${OTEL_SERVICE_NAME-}" ]; then 55 | export OTEL_SERVICE_NAME="unknown_service" 56 | fi 57 | 58 | if [ -z "${service_version-}" ]; then 59 | export service_version="undefined" 60 | fi 61 | -------------------------------------------------------------------------------- /examples/shell/basic_metrics_gauge.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # Add resource attributes 18 | # Use custom_resource_attributes specifically, opioninated 19 | custom_resource_attributes=( 20 | "environment:dev" 21 | "team:ops" 22 | "support:#ops-support" 23 | ) 24 | 25 | # Service variables 26 | service_version="0.0.1-dev" 27 | 28 | # Export library path 29 | export OTEL_SH_LIB_PATH="../../library" 30 | 31 | # Import functions 32 | . "${OTEL_SH_LIB_PATH}/otel_metrics.sh" 33 | 34 | # Gauge of type int, metric 35 | log_info "Pushing metric ko.wal.ski/brain/memory/used_bytes..." 36 | otel_metrics_push_gauge "ko.wal.ski/brain/memory/used_bytes" \ 37 | "Memory usage in bytes." \ 38 | "By" \ 39 | "memory_type" \ 40 | "evictable" \ 41 | $RANDOM \ 42 | int 43 | 44 | # Gauge of type double, metric 45 | log_info "Pushing metric ko.wal.ski/person/uptime..." 46 | otel_metrics_push_gauge "ko.wal.ski/person/uptime" \ 47 | "Uptime in seconds." \ 48 | "s" \ 49 | "person_name" \ 50 | "foo" \ 51 | 88927.690019019 \ 52 | double 53 | 54 | # Check script errorlevel (success) using gauge of type int, metric 55 | log_info "Checking errorlevel..." 56 | if [ $? -eq 0 ]; then 57 | log_info "Pushing metric ko.wal.ski/${0##*/}/is_failed as 0" 58 | log_info "${0##*/} ran successfully" 59 | otel_metrics_push_gauge "ko.wal.ski/${0##*/}/is_failed" \ 60 | "If the script was successful." \ 61 | "By" \ 62 | "hostname" \ 63 | "${hostname}" \ 64 | 0 \ 65 | int 66 | else 67 | log_info "Pushing metric ko.wal.ski/${0##*/}/is_failed as 1" 68 | log_error "${0##*/} failed" 69 | otel_metrics_push_gauge "ko.wal.ski/${0##*/}/is_failed" \ 70 | "If the script was successful." \ 71 | "By" \ 72 | "hostname" \ 73 | "${hostname}" \ 74 | 1 \ 75 | int 76 | fi 77 | -------------------------------------------------------------------------------- /examples/github_actions/strategy_matrix.yaml: -------------------------------------------------------------------------------- 1 | name: github-actions-strategy-matrix 2 | on: 3 | push: 4 | 5 | env: 6 | OTEL_EXPORTER_OTEL_ENDPOINT: https://otelcol.domain 7 | # OTEL_LOG_LEVEL: debug 8 | OTEL_SH_LIB_PATH: opentelemetry-shell/library 9 | 10 | jobs: 11 | opentelemetry-shell: 12 | runs-on: [ubuntu-latest] 13 | outputs: 14 | OTEL_TRACE_ID: ${{ steps.init-otel-sh.outputs.OTEL_TRACE_ID }} 15 | steps: 16 | - name: ✨ Checkout krzko/opentelemetry-shell 17 | uses: actions/checkout@v2 18 | with: 19 | repository: krzko/opentelemetry-shell 20 | path: './opentelemetry-shell' 21 | - name: 🚦 Initialise OpenTelemetry Shell 22 | id: init-otel-sh 23 | run: | 24 | . opentelemetry-shell/library/otel_traces.sh 25 | trace_id=$(generate_uuid 16) 26 | echo "::set-output name=OTEL_TRACE_ID::${trace_id}" 27 | log_info "TraceId: ${trace_id}" 28 | 29 | awesome-job: 30 | needs: 31 | - opentelemetry-shell 32 | runs-on: [ubuntu-latest] 33 | strategy: 34 | matrix: 35 | service: [foo, bar] 36 | env: 37 | OTEL_TRACE_ID: ${{ needs.opentelemetry-shell.outputs.OTEL_TRACE_ID }} 38 | steps: 39 | - name: ✨ Checkout 40 | uses: actions/checkout@v2 41 | - name: ✨ Checkout krzko/opentelemetry-shell 42 | uses: actions/checkout@v2 43 | with: 44 | repository: krzko/opentelemetry-shell 45 | path: './opentelemetry-shell' 46 | - name: ⛏ Work 47 | run: | 48 | . opentelemetry-shell/library/otel_traces.sh 49 | otel_trace_start_parent_span echo ${{ matrix.service }} 50 | otel_trace_start_child_span sleep 1 51 | otel_trace_start_child_span sleep 2 52 | - name: ✅ Push success metric 53 | if: success() 54 | run: | 55 | . opentelemetry-shell/library/otel_metrics.sh 56 | otel_metrics_push_gauge "ko.wal.ski/${GITHUB_REPOSITORY}/workflow/is_failed" \ 57 | "If the workflow was successful." \ 58 | "By" \ 59 | "workflow_name" \ 60 | "${GITHUB_WORKFLOW}" \ 61 | 0 \ 62 | int 63 | - name: ❌ Push failure metric 64 | if: failure() 65 | run: | 66 | . opentelemetry-shell/library/otel_metrics.sh 67 | otel_metrics_push_gauge "ko.wal.ski/${GITHUB_REPOSITORY}/workflow/is_failed" \ 68 | "If the workflow was successful." \ 69 | "By" \ 70 | "workflow_name" \ 71 | "${GITHUB_WORKFLOW}" \ 72 | 1 \ 73 | int 74 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .SILENT: 2 | .DEFAULT_GOAL := help 3 | 4 | SHELL := /bin/bash 5 | .SHELLFLAGS = -e 6 | 7 | name := opentelemetry-shell 8 | prefix ?= ~/.local 9 | libdir ?= $(prefix)/lib/$(name) 10 | 11 | ##@ Install 12 | 13 | .PHONY: install 14 | install: ## Install OpenTelemetry Shell 15 | ifeq ("$(wildcard $(libdir))", "") 16 | echo "Installing OpenTelemetry Shell..." 17 | mkdir -p $(libdir) 18 | cp -r library $(libdir) 19 | echo "Installed. Ensure you export your variables, eg:" 20 | echo "---" 21 | echo 'export OTEL_EXPORTER_OTEL_ENDPOINT="http://localhost:4318"' 22 | echo "export OTEL_SH_LIB_PATH=\"$(libdir)/library\"" 23 | else 24 | echo "OpenTelemetry Shell exists, overwriting..." 25 | rm -Rf $(libdir) 26 | mkdir -p $(libdir) 27 | cp -r library $(libdir) 28 | echo "Installed. Ensure you export your variables, eg:" 29 | echo "---" 30 | echo 'export OTEL_EXPORTER_OTEL_ENDPOINT="http://localhost:4318"' 31 | echo "export OTEL_SH_LIB_PATH=\"$(libdir)/library\"" 32 | endif 33 | 34 | .PHONY: uninstall 35 | uninstall: ## Uninstall OpenTelemetry Shell 36 | ifeq ("$(wildcard $(libdir))", "") 37 | echo "$(libdir) does not exist. Skipping." 38 | else 39 | echo "Uninstalling OpenTelemetry Shell..." 40 | rm -Rf $(libdir) 41 | echo "Uninstalled" 42 | endif 43 | 44 | ##@ Operate 45 | 46 | .PHONY: validate 47 | validate: ## Validate OpenTelemetry Shell variables 48 | ifndef OTEL_EXPORTER_OTEL_ENDPOINT 49 | echo "❌ export OTEL_EXPORTER_OTEL_ENDPOINT not set" 50 | else 51 | echo "✅ OTEL_EXPORTER_OTEL_ENDPOINT found" 52 | endif 53 | ifndef OTEL_SH_LIB_PATH 54 | echo "❌ export OTEL_SH_LIB_PATH not set" 55 | else 56 | echo "✅ OTEL_SH_LIB_PATH found" 57 | endif 58 | 59 | .PHONY: version 60 | version: ## Show OpenTelemetry Shell 61 | . $(libdir)/library/otel_ver.sh; otel_sh_ver 62 | 63 | .PHONY: _banner 64 | _banner: 65 | printf " _____ _____ _____ _____ _____ _____ __ _____ _____ _____ _____ _____ __ __ \n" 66 | printf "| | _ | __| | |_ _| __| | | __| | __|_ _| __ | | |\n" 67 | printf "| | | __| __| | | | | | | __| |__| __| | | | __| | | | -|_ _|\n" 68 | printf "|_____|__| |_____|_|___| |_| |_____|_____|_____|_|_|_|_____| |_| |__|__| |_| \n" 69 | printf " _____ _____ _____ __ __ \n" 70 | printf "| __| | | __| | | | \n" 71 | printf "|__ | | __| |__| |__ \n" 72 | printf "|_____|__|__|_____|_____|_____| \n" 73 | printf "\n" 74 | 75 | ##@ Help 76 | 77 | .PHONY: help 78 | help: _banner ## Display this help 79 | awk \ 80 | 'BEGIN { \ 81 | FS = ":.*##"; printf "\033[37;1mUsage\n \033[32;1mmake\033[0m \033[34;1m\033[0m\n" \ 82 | } /^[a-zA-Z_-]+:.*?##/ { \ 83 | printf " \033[34;1m%-15s\033[0m %s\n", $$1, $$2 \ 84 | } /^##@/ { \ 85 | printf "\n\033[37;1m%s\033[0m\n", substr($$0, 5) \ 86 | }' $(MAKEFILE_LIST) 87 | -------------------------------------------------------------------------------- /.goreleaser.yaml: -------------------------------------------------------------------------------- 1 | # .goreleaser.yml 2 | 3 | builds: 4 | - env: [CGO_ENABLED=0] 5 | dir: go 6 | goos: 7 | - linux 8 | - darwin 9 | goarch: 10 | - amd64 11 | - arm64 12 | 13 | # dockers: 14 | # - image_templates: ["ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-amd64"] 15 | # dockerfile: Dockerfile 16 | # use: buildx 17 | # build_flag_templates: 18 | # - --platform=linux/amd64 19 | # - --label=org.opencontainers.image.title={{ .ProjectName }} 20 | # - --label=org.opencontainers.image.description={{ .ProjectName }} 21 | # - --label=org.opencontainers.image.url=https://github.com/krzko/{{ .ProjectName }} 22 | # - --label=org.opencontainers.image.source=https://github.com/krzko/{{ .ProjectName }} 23 | # - --label=org.opencontainers.image.version={{ .Version }} 24 | # - --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }} 25 | # - --label=org.opencontainers.image.revision={{ .FullCommit }} 26 | # - --label=org.opencontainers.image.licenses=MIT 27 | # - image_templates: ["ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-arm64v8"] 28 | # goarch: arm64 29 | # dockerfile: Dockerfile 30 | # use: buildx 31 | # build_flag_templates: 32 | # - --platform=linux/arm64/v8 33 | # - --label=org.opencontainers.image.title={{ .ProjectName }} 34 | # - --label=org.opencontainers.image.description={{ .ProjectName }} 35 | # - --label=org.opencontainers.image.url=https://github.com/krzko/{{ .ProjectName }} 36 | # - --label=org.opencontainers.image.source=https://github.com/krzko/{{ .ProjectName }} 37 | # - --label=org.opencontainers.image.version={{ .Version }} 38 | # - --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }} 39 | # - --label=org.opencontainers.image.revision={{ .FullCommit }} 40 | # - --label=org.opencontainers.image.licenses=MIT 41 | 42 | # docker_manifests: 43 | # - name_template: ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }} 44 | # image_templates: 45 | # - ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-amd64 46 | # - ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-arm64v8 47 | # - name_template: ghcr.io/krzko/{{ .ProjectName }}:latest 48 | # image_templates: 49 | # - ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-amd64 50 | # - ghcr.io/krzko/{{ .ProjectName }}:{{ .Version }}-arm64v8 51 | 52 | archives: 53 | - files: 54 | - Makefile 55 | - library/* 56 | 57 | checksum: 58 | name_template: checksums.txt 59 | 60 | snapshot: 61 | name_template: "{{ incpatch .Version }}-next" 62 | 63 | changelog: 64 | sort: asc 65 | filters: 66 | exclude: 67 | - '^docs:' 68 | - '^test:' 69 | - Merge pull request 70 | - Merge branch 71 | 72 | brews: 73 | - name: opentelemetry-shell 74 | tap: 75 | owner: 'krzko' 76 | name: 'homebrew-tap' 77 | homepage: https://github.com/krzko/opentelemetry-shell 78 | description: "OpenTelemetry functions for shells" 79 | install: | 80 | system "make", "install" 81 | test: | 82 | system "make", "version" 83 | dependencies: 84 | - name: curl 85 | - name: jq 86 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | ## Reporting Bugs/Feature Requests 10 | 11 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 12 | 13 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 14 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 15 | 16 | * A reproducible test case or series of steps 17 | * The version of our code being used 18 | * Any modifications you've made relevant to the bug 19 | * Anything unusual about your environment or deployment 20 | 21 | ## New Component Contributions 22 | 23 | We are currently not supporting pull requests for new ADOT Collector components from outside contributors. 24 | If you would like to see a new component integrated into the ADOT Collector please open a Feature Request 25 | issue. 26 | 27 | ## Contributing via Pull Requests 28 | 29 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 30 | 31 | 1. You are working against the latest source on the `main` branch. 32 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 33 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 34 | 35 | To send us a pull request, please: 36 | 37 | 1. Fork the repository. 38 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 39 | 3. Ensure local tests pass. 40 | 4. Commit to your fork using clear commit messages. 41 | 5. Send us a pull request, answering any default questions in the pull request interface. 42 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 43 | 44 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 45 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 46 | 47 | ## Finding contributions to work on 48 | 49 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 50 | 51 | ## Code of Conduct 52 | 53 | For more information see the [Code of Conduct](CODE_OF_CONDUCT.md) or contact 54 | 55 | ## Licensing 56 | 57 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 58 | 59 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 60 | -------------------------------------------------------------------------------- /library/log.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # https://opentelemetry.io/docs/reference/specification/logs/data-model/#severity-fields 18 | 19 | ####################################### 20 | # Echo to stderr 21 | # ARGUMENTS: 22 | # $@, expands to the positional parameters 23 | # OUTPUTS: 24 | # Write to stderr 25 | ####################################### 26 | function echo_stderr { 27 | >&2 echo "$@" 28 | } 29 | 30 | # Log the given message at the given level. 31 | ####################################### 32 | # Log the given message at level, DEBUG. 33 | # All logs are written to stderr with a timestamp. 34 | # ARGUMENTS: 35 | # message, the entry to be printed 36 | # OUTPUTS: 37 | # Write to stderr 38 | ####################################### 39 | function log { 40 | local -r level="$1" 41 | local -r message="$2" 42 | local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S") 43 | local -r script_name="${0##*/}#L${BASH_LINENO[1]}" 44 | local -r function="${FUNCNAME[2]}" 45 | echo_stderr -e "${timestamp} [${level}] [$script_name] [${function}()] ${message}" 46 | } 47 | 48 | ####################################### 49 | # Log the given message at level, TRACE 50 | # ARGUMENTS: 51 | # message, the text to be printed 52 | # OUTPUTS: 53 | # Write to stderr 54 | ####################################### 55 | function log_trace { 56 | local -r message="$1" 57 | log "TRACE" "$message" 58 | } 59 | 60 | ####################################### 61 | # Log the given message at level, DEBUG 62 | # ARGUMENTS: 63 | # message, the text to be printed 64 | # OUTPUTS: 65 | # Write to stderr 66 | ####################################### 67 | function log_debug { 68 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 69 | local -r message="$1" 70 | else 71 | local -r message="$1" 72 | log "DEBUG" "$message" 73 | fi 74 | } 75 | 76 | ####################################### 77 | # Log the given message at level, INFO 78 | # ARGUMENTS: 79 | # message, the text to be printed 80 | # OUTPUTS: 81 | # Write to stderr 82 | ####################################### 83 | function log_info { 84 | local -r message="$1" 85 | log "INFO" "$message" 86 | } 87 | 88 | ####################################### 89 | # Log the given message at level, WARN 90 | # ARGUMENTS: 91 | # message, the text to be printed 92 | # OUTPUTS: 93 | # Write to stderr 94 | ####################################### 95 | function log_warn { 96 | local -r message="$1" 97 | log "WARN" "$message" 98 | } 99 | 100 | ####################################### 101 | # Log the given message at level, ERROR 102 | # ARGUMENTS: 103 | # message, the text to be printed 104 | # OUTPUTS: 105 | # Write to stderr 106 | ####################################### 107 | function log_error { 108 | local -r message="$1" 109 | log "ERROR" "$message" 110 | } 111 | 112 | ####################################### 113 | # Log the given message at level, FATAL 114 | # ARGUMENTS: 115 | # message, the text to be printed 116 | # OUTPUTS: 117 | # Write to stderr 118 | ####################################### 119 | function log_fatal { 120 | local -r message="$1" 121 | log "FATAL" "$message" 122 | } 123 | -------------------------------------------------------------------------------- /library/otel_metrics.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # https://opentelemetry.io/docs/reference/specification/metrics/datamodel/ 18 | 19 | . "${OTEL_SH_LIB_PATH}/log.sh" 20 | . "${OTEL_SH_LIB_PATH}/net.sh" 21 | . "${OTEL_SH_LIB_PATH}/time.sh" 22 | 23 | . "${OTEL_SH_LIB_PATH}/otel_init.sh" 24 | . "${OTEL_SH_LIB_PATH}/otel_metrics_schema.sh" 25 | 26 | ####################################### 27 | # Adds a span object into .resourceSpans[].scopeSpans[].spans array 28 | # ARGUMENTS: 29 | # name of calling command/function 30 | # traceId, the top level Trace Id 31 | # spanId, the current Span Id 32 | # parentSpanId, the Id to asscociate the current span to 33 | # startTimeUnixNano, starting epoc time of the span 34 | # endTimeUnixNano, ending epoch time of the span 35 | ####################################### 36 | function otel_metrics_push_gauge { 37 | local name=$1 38 | local description=$2 39 | local unit=$3 40 | local key=$4 41 | local value=$5 42 | local as_value=$6 43 | local type=$7 44 | 45 | local time_unix_namo=$(get_epoch_now) 46 | 47 | if [ -n "${custom_resource_attributes-}" ]; then 48 | log_debug "Appending custom resource attributes" 49 | for attr in "${custom_resource_attributes[@]}"; do 50 | otel_metrics_add_resourcemetrics_resource_attrib_string "${attr%%:*}" "${attr#*:}" 51 | done 52 | fi 53 | 54 | otel_metrics_add_gauge "$name" \ 55 | "$description" \ 56 | "$unit" 57 | 58 | if [[ ${type} == "double" ]]; then 59 | otel_metrics_add_gauge_datapoint_double "$key" \ 60 | "$value" \ 61 | "$as_value" 62 | elif [[ ${type} == "int" ]]; then 63 | otel_metrics_add_gauge_datapoint_int "$key" \ 64 | "$value" \ 65 | "$as_value" 66 | else 67 | log_error "'as_value' arg needs to be double|int" 68 | exit 1 69 | fi 70 | 71 | if [ -z $"{OTEL_LOG_LEVEL-}" ]; then 72 | net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" 73 | else 74 | log_debug "[$( caller )] $*" >&2 75 | log_debug "BASH_SOURCE: ${BASH_SOURCE[*]}" 76 | log_debug "BASH_LINENO: ${BASH_LINENO[*]}" 77 | log_debug "FUNCNAME: ${FUNCNAME[*]}" 78 | 79 | log_debug "name: ${name}" 80 | log_debug "key: ${key}" 81 | log_debug "value: ${value}" 82 | log_debug "OTEL_EXPORTER_OTEL_ENDPOINT=${OTEL_EXPORTER_OTEL_ENDPOINT}" 83 | net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" 84 | fi 85 | } 86 | 87 | ####################################### 88 | # Adds a span object into .resourceSpans[].scopeSpans[].spans array 89 | # ARGUMENTS: 90 | # name of calling command/function 91 | # traceId, the top level Trace Id 92 | # spanId, the current Span Id 93 | # parentSpanId, the Id to asscociate the current span to 94 | # startTimeUnixNano, starting epoc time of the span 95 | # endTimeUnixNano, ending epoch time of the span 96 | ####################################### 97 | function otel_metrics_push_sum { 98 | local name=$1 99 | local description=$2 100 | local unit=$3 101 | local key=$4 102 | local value=$5 103 | local as_value=$6 104 | local type=$7 105 | local start_time_unix_nano=$8 106 | 107 | local time_unix_namo=$(get_epoch_now) 108 | 109 | if [ "$custom_resource_attributes" ]; then 110 | for attr in "${custom_resource_attributes[@]}"; do 111 | otel_metrics_add_resourcemetrics_resource_attrib_string "${attr%%:*}" "${attr#*:}" 112 | done 113 | fi 114 | 115 | otel_metrics_add_gauge "$name" \ 116 | "$description" \ 117 | "$unit" 118 | 119 | if [[ ${type} == "double" ]]; then 120 | otel_metrics_add_sum_datapoint_double "$key" \ 121 | "$value" \ 122 | "$as_value" \ 123 | "$start_time_unix_nano" 124 | elif [[ ${type} == "int" ]]; then 125 | otel_metrics_add_sum_datapoint_int "$key" \ 126 | "$value" \ 127 | "$as_value" \ 128 | "$start_time_unix_nano" 129 | else 130 | log_error "'as_value' arg needs to be double|int" 131 | exit 1 132 | fi 133 | 134 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 135 | net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" 136 | else 137 | log_debug "[$( caller )] $*" >&2 138 | log_debug "BASH_SOURCE: ${BASH_SOURCE[*]}" 139 | log_debug "BASH_LINENO: ${BASH_LINENO[*]}" 140 | log_debug "FUNCNAME: ${FUNCNAME[*]}" 141 | 142 | log_debug "name: ${name}" 143 | log_debug "key: ${key}" 144 | log_debug "value: ${value}" 145 | net_client_post "${otel_metrics_resource_metrics}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/metrics" 146 | fi 147 | } 148 | -------------------------------------------------------------------------------- /library/otel_traces_schema.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | # otel_trace_resource_spans stores the .resourceSpans[].resource.attributes[]. 18 | # This is the outer json schema 19 | otel_trace_resource_spans=$(cat <&3 ; } 2>&1); } 3>&1 58 | log_debug "Command: ${*} Errorlevel: ${exit_code}" 59 | local end_time_unix_nano=$(get_epoch_now) 60 | 61 | if [ -n "${custom_resource_attributes-}" ]; then 62 | log_debug "Appending custom resource attributes" 63 | for attr in "${custom_resource_attributes[@]}"; do 64 | otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" 65 | done 66 | fi 67 | 68 | if [ -n "${detector_resource_attributes-}" ]; then 69 | log_debug "Appending detector resource attributes" 70 | for attr in "${detector_resource_attributes[@]}"; do 71 | otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" 72 | done 73 | fi 74 | 75 | otel_trace_add_resource_scopespans_span "$name" \ 76 | "$OTEL_TRACE_ID" \ 77 | "$span_id" \ 78 | '' \ 79 | "$start_time_unix_nano" \ 80 | "$end_time_unix_nano" \ 81 | "$exit_code" 82 | 83 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "command" "$*" 84 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "errorlevel" "${exit_code}" 85 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "code.url" "${PWD}/${0##*/}#L${BASH_LINENO[0]}" 86 | 87 | if [ -z "${FUNCNAME-}" ]; then 88 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "function" "${FUNCNAME[1]}()" 89 | fi 90 | 91 | if [ -n "${linked_span-}" ]; then 92 | local linkedTraceId=${linked_span[0]} 93 | local linkedTraceState=${linked_span[1]} 94 | local linkedSpanId=${linked_span[2]} 95 | log_debug "linkedTraceId: $linkedTraceId, linkedTraceState: $linkedTraceState, linkedSpanId: $linkedSpanId" 96 | otel_trace_add_resourcespan_scopespans_spans_link $linkedTraceId $linkedTraceState $linkedSpanId 97 | fi 98 | 99 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 100 | log_debug "curling ${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" 101 | net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" 102 | else 103 | log_debug "[$( caller )] $*" >&2 104 | log_debug "BASH_SOURCE: ${BASH_SOURCE[*]}" 105 | log_debug "BASH_LINENO: ${BASH_LINENO[*]}" 106 | log_debug "FUNCNAME: ${FUNCNAME[*]}" 107 | 108 | log_debug "traceId: ${OTEL_TRACE_ID}" 109 | log_debug "spanId: ${span_id}" 110 | log_debug "parentSpanId: ${PARENT_SPAN_ID}" 111 | net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" 112 | fi 113 | 114 | if [ $exit_code -ne 0 ]; then 115 | log_fatal "Exiting with Errorlevel: ${exit_code}" 116 | exit $exit_code 117 | fi 118 | 119 | PARENT_SPAN_ID=${span_id} 120 | } 121 | 122 | ####################################### 123 | # Starts a new child trace bound to the OTEL_TRACE_ID and PARENT_SPAN_ID 124 | # GLOBALS: 125 | # OTEL_TRACE_ID 126 | # PARENT_SPAN_ID 127 | # ARGUMENTS: 128 | # name of calling command/function 129 | # OUTPUTS: 130 | # Write to stdout via ConsoleExporter 131 | # Curl to OTLP (HTTP) Receiver 132 | # RETURN: 133 | # 0 if curl succeeds, non-zero on error. 134 | ####################################### 135 | otel_trace_start_child_span() { 136 | local name=$1 137 | local span_id=$(generate_uuid 8) 138 | 139 | if [ -n "${span_name-}" ]; then 140 | name=$span_name 141 | fi 142 | 143 | local start_time_unix_nano=$(get_epoch_now) 144 | local exit_code=0 145 | "$@" && exit_code=$? || exit_code=$? 146 | # { local capture=$( { { "$@" && exit_code=$? ; } 1>&3 ; } 2>&1); } 3>&1 147 | log_debug "Command: ${*} Errorlevel: ${exit_code}" 148 | local end_time_unix_nano=$(get_epoch_now) 149 | 150 | if [ -n "${custom_resource_attributes-}" ]; then 151 | log_debug "Appending custom resource attributes" 152 | for attr in "${custom_resource_attributes[@]}"; do 153 | otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" 154 | done 155 | fi 156 | 157 | if [ -n "${detector_resource_attributes-}" ]; then 158 | log_debug "Appending detector resource attributes" 159 | for attr in "${detector_resource_attributes[@]}"; do 160 | otel_trace_add_resourcespan_resource_attrib_string "${attr%%:*}" "${attr#*:}" 161 | done 162 | fi 163 | 164 | otel_trace_add_resource_scopespans_span "$name" \ 165 | "$OTEL_TRACE_ID" \ 166 | "$span_id" \ 167 | "$PARENT_SPAN_ID" \ 168 | "$start_time_unix_nano" \ 169 | "$end_time_unix_nano" \ 170 | "$exit_code" 171 | 172 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "command" "$*" 173 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "errorlevel" "${exit_code}" 174 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "function" "${FUNCNAME[1]}()" 175 | 176 | if [ -z "${FUNCNAME-}" ]; then 177 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "function" "${FUNCNAME[1]}()" 178 | fi 179 | 180 | otel_trace_add_resourcespan_scopespans_spans_attrib_string "code.url" "${PWD}/${0##*/}#L${BASH_LINENO[0]}" 181 | 182 | if [ -n "${linked_span-}" ]; then 183 | local linkedTraceId=${linked_span[0]} 184 | local linkedTraceState=${linked_span[1]} 185 | local linkedSpanId=${linked_span[2]} 186 | log_debug "linkedTraceId: $linkedTraceId, linkedTraceState: $linkedTraceState, linkedSpanId: $linkedSpanId" 187 | otel_trace_add_resourcespan_scopespans_spans_link $linkedTraceId $linkedTraceState $linkedSpanId 188 | fi 189 | 190 | if [ -z "${OTEL_LOG_LEVEL-}" ]; then 191 | net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" 192 | else 193 | log_debug "[$( caller )] $*" >&2 194 | log_debug "BASH_SOURCE: ${BASH_SOURCE[*]}" 195 | log_debug "BASH_LINENO: ${BASH_LINENO[*]}" 196 | log_debug "FUNCNAME: ${FUNCNAME[*]}" 197 | 198 | log_debug "traceId: ${OTEL_TRACE_ID}" 199 | log_debug "spanId: ${span_id}" 200 | log_debug "parentSpanId: ${PARENT_SPAN_ID}" 201 | log_debug "OTEL_EXPORTER_OTEL_ENDPOINT=${OTEL_EXPORTER_OTEL_ENDPOINT}" 202 | net_client_post "${otel_trace_resource_spans}" "${OTEL_EXPORTER_OTEL_ENDPOINT}/v1/traces" 203 | fi 204 | 205 | if [ "$exit_code" -ne 0 ]; then 206 | log_fatal "Exiting with Errorlevel: ${exit_code}" 207 | exit $exit_code 208 | fi 209 | 210 | PARENT_SPAN_ID=${span_id} 211 | } 212 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # opentelemetry-shell 2 | 3 | ```sh 4 | _____ _____ _____ _____ _____ _____ __ _____ _____ _____ _____ _____ __ __ 5 | | | _ | __| | |_ _| __| | | __| | __|_ _| __ | | | 6 | | | | __| __| | | | | | | __| |__| __| | | | __| | | | -|_ _| 7 | |_____|__| |_____|_|___| |_| |_____|_____|_____|_|_|_|_____| |_| |__|__| |_| 8 | _____ _____ _____ __ __ 9 | | __| | | __| | | | 10 | |__ | | __| |__| |__ 11 | |_____|__|__|_____|_____|_____| 12 | ``` 13 | 14 | **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. 15 | 16 | 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. 17 | 18 | Why not ensure that these scripts are observable and send back telemetry data as well? This is the aim of [opentelemetry.sh](#), a set of [OpenTelemetry](https://opentelemetry.io/) functions for shells. 19 | 20 | The functions utilise the [OpenTelemetry Protocol Specification (OTLP)](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md) to send telemtry data back to any service that supports [OTLP (HTTP)](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlphttp). This is traditionally via an [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) or a vendor that supports this API specification. 21 | 22 | ## Requirements 23 | 24 | These functions have been tested on several `bash` versions, the list is as follows, `3.x`, although looking to deprecate support for this soon (tm), `4.x` an `5.x`. 25 | 26 | Other versions and shells, your mileage may vary. 27 | 28 | Whilst all effort has been made to limit the use of external binaries from the standard `bash` built-in commands, this is the list of binaries required or this library: 29 | 30 | - `curl`: a tool for transferring data from or to a server 31 | - `date`: display or set date and time 32 | - `hostname`: set or print name of current host system 33 | - `jq`: Command-line JSON processor 34 | - `uname`: Print operating system name 35 | 36 | As a future enhancement all effort will be made to remove the need for as many external binaries as possible and just utilise the built-in functions. 37 | 38 | ## Supported Specifications 39 | 40 | Whilst the [OpenTelemetry Protocol Specification (OTLP)](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md) specifications are too extensive to implement with-in a shell environment, a select few have been added, and will continue to expand as time goes on. 41 | 42 | The following features are supported. 43 | 44 | ### Logs 45 | 46 | Yet to be implemented. 47 | 48 | ### Metrics 49 | 50 | You can currently create the following metric types; 51 | 52 | - Gauge, double and int values 53 | - Sum, double and int values 54 | 55 | ### Traces 56 | 57 | Tracing is supported with a rudimentary, parent/child relationship. The following attributes are being associated to each span: 58 | 59 | - Calling function, whether in main() or an outer function 60 | - Invoked command with-in script 61 | - Invoking line number with-in script 62 | - Script name 63 | - Line number 64 | - Ability to add user specified resource attributes 65 | 66 | #### Resource Detectors 67 | 68 | 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: 69 | 70 | - [X] Azure Pipelines 71 | - [X] Bitbucket Pipelines 72 | - [X] Buildkite 73 | - [X] Circle CI 74 | - [ ] Codefresh 75 | - [X] Github Actions 76 | - [X] Gitlab CI 77 | - [ ] Google Cloud Build 78 | - [ ] Harness 79 | - [X] Jenkins 80 | - [ ] Jenkins X 81 | - [X] Travis CI 82 | 83 | ## Examples 84 | 85 | The easiest way to get started is to clone the repository and then `. opentelemetry-shell/library/otel_metrics.sh` or `. opentelemetry-shell/library/otel_traces.sh` to source the functions. 86 | 87 | A set of examples have been created to show case the ease of use, in creating traces and custom metrics. Please have a look at the [examples](https://github.com/krzko/opentelemetry-shell/tree/main/examples). 88 | 89 | Firstly, define your library and exporter endpoint variables, before testing any of the examples, both `http` and `https` prefixes are supported: 90 | 91 | ```sh 92 | export OTEL_EXPORTER_OTEL_ENDPOINT="http://localhost:4318" 93 | export OTEL_SH_LIB_PATH="opentelemetry-shell/library" 94 | ``` 95 | 96 | ### Metrics Example 97 | 98 | Simply, `.` source as follows `. opentelemetry-shell/library/otel_metrics.sh`: 99 | 100 | ```sh 101 | #!/usr/bin/env bash 102 | 103 | # Import functions 104 | . opentelemetry-shell/library/otel_metrics.sh 105 | 106 | # Main 107 | log_info "Pushing metric ko.wal.ski/brain/memory/used_bytes..." 108 | otel_metrics_push_gauge "ko.wal.ski/brain/memory/used_bytes" \ 109 | "Memory usage in bytes." \ 110 | "By" \ 111 | "memory_type" \ 112 | "evictable" \ 113 | $RANDOM \ 114 | int 115 | ``` 116 | 117 | Yields the following: 118 | 119 | GCP Metrics Explorer 124 | 125 | ### Trace Example 126 | 127 | Simply, `.` source as follows `. opentelemetry-shell/library/otel_traces.sh`: 128 | 129 | ```sh 130 | #!/usr/bin/env bash 131 | 132 | # Import functions 133 | . opentelemetry-shell/library/otel_traces.sh 134 | 135 | # Functions 136 | sleep_for() { 137 | local sec=$1 138 | 139 | log_info "Sleeping for ${sec} sec..." 140 | sleep $sec 141 | } 142 | 143 | # Start a parent span 144 | otel_trace_start_parent_span sleep_for 1 145 | 146 | # Start a child span, associated to the parent 147 | otel_trace_start_child_span sleep_for 2 148 | # Start a child span with a custom name 149 | local span_name="Sleeping" 150 | otel_trace_start_child_span sleep_for 3 151 | 152 | # Add a SpanLink 153 | local linked_span=("$linkedTraceId" "$linkedSpanId" "$linkedTraceState") 154 | otel_trace_start_child_span sleep_for 3 155 | 156 | log_info "TraceId: ${OTEL_TRACE_ID}" 157 | ``` 158 | 159 | Yields the following: 160 | 161 | GCP Cloud Tracing 166 | 167 | ## Environment Variables 168 | 169 | The project aims to follow the standards of the [OpenTelemetry Environment Variable Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md) 170 | 171 | The following environment variables will be currently used: 172 | 173 | **[General SDK Configuration](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#general-sdk-configuration)** 174 | 175 | 176 | - `OTEL_SERVICE_NAME`: Sets the value of the `service.name` resource attribute 177 | - `OTEL_LOG_LEVEL`: Log level used by the logger, `debug`. Unset variable to disable verbose logging 178 | 179 | **[Exporter Selection](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#exporter-selection)** 180 | 181 | 184 | - `OTEL_EXPORTER_OTEL_ENDPOINT`: Exporter endpoint 185 | 186 | **OpenTelemetry Shell Specific** 187 | 188 | - `OTEL_SH_TRACE_ID`: Set a pre-defined `traceId`. Default behaviour is the functions will create this value for you automatically 189 | -------------------------------------------------------------------------------- /library/otel_metrics_schema.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2022 Krzysztof Kowalski 4 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | . "${OTEL_SH_LIB_PATH}/time.sh" 18 | 19 | # otel_trace_resource_spans stores the .resourceSpans[].resource.attributes[]. 20 | # This is the outer json schema 21 | otel_metrics_resource_metrics=$(cat <