From dba83ca673a1cfa4e5560c1f64bda9e6104a2ba9 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Wed, 8 Feb 2023 11:08:34 +0100 Subject: [PATCH 01/28] Create init image for lib-injection --- lib-injection/Dockerfile | 13 +++++++++++++ lib-injection/auto_inject.rb | 21 +++++++++++++++++++++ lib-injection/copy-lib.sh | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 lib-injection/Dockerfile create mode 100644 lib-injection/auto_inject.rb create mode 100644 lib-injection/copy-lib.sh diff --git a/lib-injection/Dockerfile b/lib-injection/Dockerfile new file mode 100644 index 00000000000..8999bef6fce --- /dev/null +++ b/lib-injection/Dockerfile @@ -0,0 +1,13 @@ +# This image provides the files needed to install the dd-trace-rb +# and auto instrument Ruby applications in containerized environments. +FROM busybox + +ARG UID=10000 +ARG DDTRACE_RUBY_VERSION +ENV DDTRACE_RUBY_VERSION=$DDTRACE_RUBY_VERSION +RUN addgroup -g 10000 -S datadog && \ + adduser -u ${UID} -S datadog -G datadog +USER ${UID} +WORKDIR /datadog-init +ADD auto_inject.rb /datadog-init/auto_inject.rb +ADD copy-lib.sh /datadog-init/copy-lib.sh diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb new file mode 100644 index 00000000000..e6c151022ed --- /dev/null +++ b/lib-injection/auto_inject.rb @@ -0,0 +1,21 @@ +if ENV['DDTRACE_AUTOINJECT'] && !ENV['skip_autoinject'] + if system 'skip_autoinject=true bundle show ddtrace' + puts 'ddtrace already installed...' + else + puts 'ddtrace is not installed... Perform auto-injection...' + + if system 'skip_autoinject=true bundle add ddtrace --require ddtrace/auto_instrument' + puts 'ddtrace added to bundle...' + else + puts 'Something went wrong when adding ddtrace to bundle...' + end + end + + begin + require 'ddtrace' + rescue LoadError => e + puts e + else + puts 'ddtrace loaded...' + end +end diff --git a/lib-injection/copy-lib.sh b/lib-injection/copy-lib.sh new file mode 100644 index 00000000000..fa74f8292d7 --- /dev/null +++ b/lib-injection/copy-lib.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +# This script is used by the admission controller to install the library from the +# init container into the application container. +cp auto_inject.rb "$1/auto_inject.rb" From 76458d7a0123b71f405d78a528f46cc08c01bdf3 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Wed, 15 Feb 2023 13:26:00 +0100 Subject: [PATCH 02/28] Add version and remove DDTRACE_AUTOINJECT --- lib-injection/Dockerfile | 1 + lib-injection/auto_inject.rb | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib-injection/Dockerfile b/lib-injection/Dockerfile index 8999bef6fce..3f1359de791 100644 --- a/lib-injection/Dockerfile +++ b/lib-injection/Dockerfile @@ -10,4 +10,5 @@ RUN addgroup -g 10000 -S datadog && \ USER ${UID} WORKDIR /datadog-init ADD auto_inject.rb /datadog-init/auto_inject.rb +RUN sed -i "s~~${DDTRACE_RUBY_VERSION}~g" /datadog-init/auto_inject.rb ADD copy-lib.sh /datadog-init/copy-lib.sh diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index e6c151022ed..6c0e3391759 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,10 +1,12 @@ -if ENV['DDTRACE_AUTOINJECT'] && !ENV['skip_autoinject'] + +if !ENV['skip_autoinject'] if system 'skip_autoinject=true bundle show ddtrace' - puts 'ddtrace already installed...' + puts 'ddtrace already installed... skipping auto-injection' else - puts 'ddtrace is not installed... Perform auto-injection...' + version = "" + puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb:#{version}" - if system 'skip_autoinject=true bundle add ddtrace --require ddtrace/auto_instrument' + if system "skip_autoinject=true bundle add ddtrace --version #{version} --require ddtrace/auto_instrument" puts 'ddtrace added to bundle...' else puts 'Something went wrong when adding ddtrace to bundle...' From d2f48b48f33a52156a69ba40d7129e36c3f2cb1c Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 16 Feb 2023 13:36:02 +0100 Subject: [PATCH 03/28] Add build publish workflow --- .github/workflows/build-and-publish-image.yml | 41 +++++++++++++++++++ .github/workflows/lib-injection.yml | 15 +++++++ 2 files changed, 56 insertions(+) create mode 100644 .github/workflows/build-and-publish-image.yml create mode 100644 .github/workflows/lib-injection.yml diff --git a/.github/workflows/build-and-publish-image.yml b/.github/workflows/build-and-publish-image.yml new file mode 100644 index 00000000000..ee982ba15a2 --- /dev/null +++ b/.github/workflows/build-and-publish-image.yml @@ -0,0 +1,41 @@ +name: Build and publish image + +on: + workflow_call: + inputs: + tags: + required: true + type: string + platforms: + required: true + type: string + build-args: + required: true + type: string + context: + required: true + type: string + secrets: + token: + required: true + +jobs: + build_push: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + - name: Login to Docker + run: docker login -u publisher -p ${{ secrets.token }} ghcr.io + - name: Docker Build + uses: docker/build-push-action@v3 + with: + push: true + tags: ${{ inputs.tags }} + platforms: ${{ inputs.platforms }} + build-args: ${{ inputs.build-args }} + context: ${{ inputs.context }} diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml new file mode 100644 index 00000000000..32a272bae5a --- /dev/null +++ b/.github/workflows/lib-injection.yml @@ -0,0 +1,15 @@ +name: "Library Injection" +on: + # Build each branch for testing + push: + +jobs: + build-and-publish-test-image: + uses: ./.github/workflows/build-and-publish-image.yml + with: + tags: 'ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }}' + platforms: 'linux/amd64,linux/arm64/v8' + build-args: 'DDTRACE_RUBY_VERSION=git+https://github.com/Datadog/dd-trace-rb@${{ github.sha }}' + context: ./lib-injection + secrets: + token: ${{ secrets.GITHUB_TOKEN }} From d55978541b0c5fdf22b6c30501a59b92fc99fee5 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 16 Feb 2023 14:14:05 +0100 Subject: [PATCH 04/28] Add deployment to gitlab ci --- .gitlab-ci.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5f47cbd28a8..4ae7235475c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,3 +25,40 @@ deploy_to_reliability_env: UPSTREAM_COMMIT_SHA: $CI_COMMIT_SHA FORCE_TRIGGER: $FORCE_TRIGGER +deploy_to_docker_registries: + stage: deploy + rules: + - if: '$POPULATE_CACHE' + when: never + - if: '$CI_COMMIT_TAG =~ /^v.*/' + when: delayed + start_in: 1 day + - when: manual + allow_failure: true + trigger: + project: DataDog/public-images + branch: main + strategy: depend + variables: + IMG_SOURCES: ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:$CI_COMMIT_TAG + IMG_DESTINATIONS: dd-lib-ruby-init:$CI_COMMIT_TAG + IMG_SIGNING: "false" + +deploy_latest_tag_to_docker_registries: + stage: deploy + rules: + - if: '$POPULATE_CACHE' + when: never + - if: '$CI_COMMIT_TAG =~ /^v.*/' + when: delayed + start_in: 1 day + - when: manual + allow_failure: true + trigger: + project: DataDog/public-images + branch: main + strategy: depend + variables: + IMG_SOURCES: ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:$CI_COMMIT_TAG + IMG_DESTINATIONS: dd-lib-ruby-init:latest + IMG_SIGNING: "false" From 159657b3bcc52c070a7b3feb02944c8c1ce6409e Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 16 Feb 2023 20:29:17 +0100 Subject: [PATCH 05/28] WIP --- .github/workflows/lib-injection.yml | 12 +++++++++++- lib-injection/Dockerfile | 9 +++++++++ lib-injection/auto_inject.rb | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 32a272bae5a..01ac70e749e 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -4,12 +4,22 @@ on: push: jobs: + # build-and-publish-release-image: + # uses: ./.github/workflows/build-and-publish-image.yml + # with: + # tags: 'ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }}' + # platforms: 'linux/amd64,linux/arm64/v8' + # build-args: 'DDTRACE_RUBY_VERSION=${{input.version}}' + # context: ./lib-injection + # secrets: + # token: ${{ secrets.GITHUB_TOKEN }} + build-and-publish-test-image: uses: ./.github/workflows/build-and-publish-image.yml with: tags: 'ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }}' platforms: 'linux/amd64,linux/arm64/v8' - build-args: 'DDTRACE_RUBY_VERSION=git+https://github.com/Datadog/dd-trace-rb@${{ github.sha }}' + build-args: 'DDTRACE_RUBY_SHA=${{ github.sha }}' context: ./lib-injection secrets: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/lib-injection/Dockerfile b/lib-injection/Dockerfile index 3f1359de791..50e3461bcb5 100644 --- a/lib-injection/Dockerfile +++ b/lib-injection/Dockerfile @@ -3,12 +3,21 @@ FROM busybox ARG UID=10000 + ARG DDTRACE_RUBY_VERSION ENV DDTRACE_RUBY_VERSION=$DDTRACE_RUBY_VERSION + +ARG DDTRACE_RUBY_SHA +ENV DDTRACE_RUBY_SHA=$DDTRACE_RUBY_SHA + RUN addgroup -g 10000 -S datadog && \ adduser -u ${UID} -S datadog -G datadog + USER ${UID} WORKDIR /datadog-init ADD auto_inject.rb /datadog-init/auto_inject.rb + +RUN sed -i "s~~${DDTRACE_RUBY_SHA}~g" /datadog-init/auto_inject.rb RUN sed -i "s~~${DDTRACE_RUBY_VERSION}~g" /datadog-init/auto_inject.rb + ADD copy-lib.sh /datadog-init/copy-lib.sh diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 6c0e3391759..08e34e96cb5 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -4,9 +4,19 @@ puts 'ddtrace already installed... skipping auto-injection' else version = "" - puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb:#{version}" + sha = "" - if system "skip_autoinject=true bundle add ddtrace --version #{version} --require ddtrace/auto_instrument" + condition = if !version.empty? + "--version '#{version}'" + elsif !sha.empty? + "--github 'datadog/dd-trace-rb' --ref '#{sha}'" + else + puts "NO VERSION" + end + + puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" + + if system "skip_autoinject=true bundle add ddtrace #{condition} --require ddtrace/auto_instrument" puts 'ddtrace added to bundle...' else puts 'Something went wrong when adding ddtrace to bundle...' From 009f5213e9af0078de3971a193bebb7c27630d53 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 20 Feb 2023 16:24:49 +0100 Subject: [PATCH 06/28] Cleanup workflow --- .github/workflows/lib-injection.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 01ac70e749e..45d2a70c49f 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -4,16 +4,6 @@ on: push: jobs: - # build-and-publish-release-image: - # uses: ./.github/workflows/build-and-publish-image.yml - # with: - # tags: 'ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }}' - # platforms: 'linux/amd64,linux/arm64/v8' - # build-args: 'DDTRACE_RUBY_VERSION=${{input.version}}' - # context: ./lib-injection - # secrets: - # token: ${{ secrets.GITHUB_TOKEN }} - build-and-publish-test-image: uses: ./.github/workflows/build-and-publish-image.yml with: From 953ad81422df6e3dd25a687cb3839481cb16ede1 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 20 Feb 2023 17:37:52 +0100 Subject: [PATCH 07/28] Handle failure --- lib-injection/auto_inject.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 08e34e96cb5..01b0a0362fa 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,4 +1,3 @@ - if !ENV['skip_autoinject'] if system 'skip_autoinject=true bundle show ddtrace' puts 'ddtrace already installed... skipping auto-injection' @@ -16,11 +15,19 @@ puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" - if system "skip_autoinject=true bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + system "cp Gemfile Gemfile-datadog" + system "cp Gemfile.lock Gemfile-datadog.lock" + + if system "skip_autoinject=true BUNDLE_GEMFILE=Gemfile-datadog bundle add ddtrace #{condition} --require ddtrace/auto_instrument" puts 'ddtrace added to bundle...' + + system "cp Gemfile-datadog Gemfile" + system "cp Gemfile-datadog.lock Gemfile.lock" else puts 'Something went wrong when adding ddtrace to bundle...' end + + system "rm Gemfile-datadog Gemfile-datadog.lock" end begin From eba770628dc26b225d4a154ed2c5599f12684d28 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 20 Feb 2023 18:54:01 +0100 Subject: [PATCH 08/28] Change test for lib-injection to not included for gem packaging --- spec/ddtrace/release_gem_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/ddtrace/release_gem_spec.rb b/spec/ddtrace/release_gem_spec.rb index 9adfcb97042..55d26cb35e6 100644 --- a/spec/ddtrace/release_gem_spec.rb +++ b/spec/ddtrace/release_gem_spec.rb @@ -35,7 +35,7 @@ /x directories_excluded = %r{ - ^(sig|spec|docs|\.circleci|\.github|benchmarks|gemfiles|integration|tasks|sorbet|yard|vendor/rbs)/ + ^(sig|spec|docs|\.circleci|\.github|lib-injection|benchmarks|gemfiles|integration|tasks|sorbet|yard|vendor/rbs)/ }x expect(files) From 17ec104a23579d45e23fd99ce5c8cf84866475d2 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 21 Feb 2023 11:41:28 +0100 Subject: [PATCH 09/28] Refactor workflow --- .github/workflows/build-and-publish-image.yml | 41 ------------------- .github/workflows/lib-injection.yml | 26 ++++++++---- 2 files changed, 18 insertions(+), 49 deletions(-) delete mode 100644 .github/workflows/build-and-publish-image.yml diff --git a/.github/workflows/build-and-publish-image.yml b/.github/workflows/build-and-publish-image.yml deleted file mode 100644 index ee982ba15a2..00000000000 --- a/.github/workflows/build-and-publish-image.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Build and publish image - -on: - workflow_call: - inputs: - tags: - required: true - type: string - platforms: - required: true - type: string - build-args: - required: true - type: string - context: - required: true - type: string - secrets: - token: - required: true - -jobs: - build_push: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up QEMU - uses: docker/setup-qemu-action@v2 - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v2 - - name: Login to Docker - run: docker login -u publisher -p ${{ secrets.token }} ghcr.io - - name: Docker Build - uses: docker/build-push-action@v3 - with: - push: true - tags: ${{ inputs.tags }} - platforms: ${{ inputs.platforms }} - build-args: ${{ inputs.build-args }} - context: ${{ inputs.context }} diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 45d2a70c49f..1b8804884c9 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -5,11 +5,21 @@ on: jobs: build-and-publish-test-image: - uses: ./.github/workflows/build-and-publish-image.yml - with: - tags: 'ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }}' - platforms: 'linux/amd64,linux/arm64/v8' - build-args: 'DDTRACE_RUBY_SHA=${{ github.sha }}' - context: ./lib-injection - secrets: - token: ${{ secrets.GITHUB_TOKEN }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + - name: Login to Docker + run: docker login -u publisher -p ${{ secrets.GITHUB_TOKEN }} ghcr.io + - name: Docker Build + uses: docker/build-push-action@v3 + with: + push: true + tags: ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ github.sha }} + platforms: 'linux/amd64,linux/arm64/v8' + build-args: DDTRACE_RUBY_SHA=${{ github.sha }} + context: ./lib-injection From 4b648ee8433873eae3c2a9550f754d16b8b87cfa Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 21 Feb 2023 11:55:38 +0100 Subject: [PATCH 10/28] Release workflow --- .github/workflows/lib-injection.yml | 3 +- .github/workflows/release-lib-injection.yml | 33 +++++++++++++++++++++ lib-injection/auto_inject.rb | 20 ++++++++----- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/release-lib-injection.yml diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 1b8804884c9..e1e78e861e7 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -6,7 +6,7 @@ on: jobs: build-and-publish-test-image: runs-on: ubuntu-latest - steps: + steps: - uses: actions/checkout@v3 - name: Set up QEMU uses: docker/setup-qemu-action@v2 @@ -23,3 +23,4 @@ jobs: platforms: 'linux/amd64,linux/arm64/v8' build-args: DDTRACE_RUBY_SHA=${{ github.sha }} context: ./lib-injection + diff --git a/.github/workflows/release-lib-injection.yml b/.github/workflows/release-lib-injection.yml new file mode 100644 index 00000000000..43d3708d166 --- /dev/null +++ b/.github/workflows/release-lib-injection.yml @@ -0,0 +1,33 @@ + +name: "Release Library Injection" +on: + push: + tags: + - 'v*.*.*' + +jobs: + build-and-publish-release-image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set version + id: version + run: echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT + - name: Get version + run: echo "The selected version is ${{ steps.version.outputs.version }}" + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + - name: Login to Docker + run: docker login -u publisher -p ${{ secrets.GITHUB_TOKEN }} ghcr.io + - name: Docker Build + uses: docker/build-push-action@v3 + with: + push: true + tags: ghcr.io/datadog/dd-trace-rb/dd-lib-ruby-init:${{ steps.version.outputs.version }} + platforms: 'linux/amd64,linux/arm64/v8' + build-args: DDTRACE_RUBY_VERSION=${{ steps.version.outputs.version }} + context: ./lib-injection + diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 01b0a0362fa..cbe17370dab 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -5,28 +5,34 @@ version = "" sha = "" - condition = if !version.empty? - "--version '#{version}'" - elsif !sha.empty? - "--github 'datadog/dd-trace-rb' --ref '#{sha}'" - else - puts "NO VERSION" - end + condition = + if !version.empty? + # For public release + "--version '#{version.gsub(/^v/, '')}'" + elsif !sha.empty? + # For internal testing + "--github 'datadog/dd-trace-rb' --ref '#{sha}'" + else + puts "NO VERSION" + end puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" + # Copies for trial system "cp Gemfile Gemfile-datadog" system "cp Gemfile.lock Gemfile-datadog.lock" if system "skip_autoinject=true BUNDLE_GEMFILE=Gemfile-datadog bundle add ddtrace #{condition} --require ddtrace/auto_instrument" puts 'ddtrace added to bundle...' + # Trial success, replace the original system "cp Gemfile-datadog Gemfile" system "cp Gemfile-datadog.lock Gemfile.lock" else puts 'Something went wrong when adding ddtrace to bundle...' end + # Remove the copies system "rm Gemfile-datadog Gemfile-datadog.lock" end From 75c3fa93c8020380b04ab3eef86426a6c891d29c Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 21 Feb 2023 14:37:18 +0100 Subject: [PATCH 11/28] Dynamic gemfile --- lib-injection/auto_inject.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index cbe17370dab..ea4660e22a0 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -18,22 +18,32 @@ puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" + require 'bundler' + require 'fileutils' + + gemfile = Bundler::SharedHelpers.default_gemfile + lockfile = Bundler::SharedHelpers.default_lockfile + + datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") + datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") + # Copies for trial - system "cp Gemfile Gemfile-datadog" - system "cp Gemfile.lock Gemfile-datadog.lock" + FileUtils.cp gemfile, datadog_gemfile + FileUtils.cp lockfile, datadog_lockfile - if system "skip_autoinject=true BUNDLE_GEMFILE=Gemfile-datadog bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" puts 'ddtrace added to bundle...' # Trial success, replace the original - system "cp Gemfile-datadog Gemfile" - system "cp Gemfile-datadog.lock Gemfile.lock" + FileUtils.cp datadog_gemfile, gemfile + FileUtils.cp datadog_lockfile, lockfile else puts 'Something went wrong when adding ddtrace to bundle...' end # Remove the copies - system "rm Gemfile-datadog Gemfile-datadog.lock" + FileUtils.rm datadog_gemfile + FileUtils.rm datadog_lockfile end begin From d70d5d5edd94bac040ec012dca11a6df33742b54 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 23 Feb 2023 15:12:50 +0100 Subject: [PATCH 12/28] Add system test for lib injection --- .github/workflows/lib-injection.yml | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index e1e78e861e7..a2b5139ba42 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -24,3 +24,33 @@ jobs: build-args: DDTRACE_RUBY_SHA=${{ github.sha }} context: ./lib-injection + test: + needs: + - build-and-publish-test-image + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + strategy: + matrix: + lib-injection-connection: ['network'] + lib-injection-use-admission-controller: ['', 'use-admission-controller'] + weblog-variant: ['dd-lib-ruby-init-test-rails'] + fail-fast: false + env: + TEST_LIBRARY: ruby + WEBLOG_VARIANT: ${{ matrix.weblog-variant }} + LIBRARY_INJECTION_CONNECTION: ${{ matrix.lib-injection-connection }} + LIBRARY_INJECTION_ADMISSION_CONTROLLER: ${{ matrix.lib-injection-use-admission-controller }} + DOCKER_REGISTRY_IMAGES_PATH: ghcr.io/datadog + DOCKER_IMAGE_TAG: ${{ github.sha }} + BUILDX_PLATFORMS: linux/amd64 + steps: + - name: lib-injection test runner + id: lib-injection-test-runner + uses: DataDog/system-tests/lib-injection/runner@a8acd1e881cf020711b1ada4c054424135c17787 + with: + docker-registry: ghcr.io + docker-registry-username: ${{ github.repository_owner }} + docker-registry-password: ${{ secrets.GITHUB_TOKEN }} + test-script: ./lib-injection/run-manual-lib-injection.sh From f56abdf89ff17a901fc50e3690f7d35d59dd6309 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 23 Feb 2023 15:22:07 +0100 Subject: [PATCH 13/28] Change system test commit sha --- .github/workflows/lib-injection.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index a2b5139ba42..8fe7b395c32 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -48,7 +48,7 @@ jobs: steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@a8acd1e881cf020711b1ada4c054424135c17787 + uses: DataDog/system-tests/lib-injection/runner@51cdf2c11f1c238a84fc331e95bb5fbbb97a1881 with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} From 6eaf3d1b028ca78d55a29e2f0d33be329dadf6e5 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 17:20:04 +0100 Subject: [PATCH 14/28] Update commit sha --- .github/workflows/lib-injection.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 8fe7b395c32..bd48000b02f 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -48,7 +48,7 @@ jobs: steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@51cdf2c11f1c238a84fc331e95bb5fbbb97a1881 + uses: DataDog/system-tests/lib-injection/runner@5e02aeb8ff4555239d15ece4abf48db530b42510 with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} From 32d3d42f6f3ed1d158a96196f8a2dcd3f9b6361c Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 18:42:52 +0100 Subject: [PATCH 15/28] Add gems.rb app to system test --- .github/workflows/lib-injection.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index bd48000b02f..7e6734fe051 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -35,7 +35,7 @@ jobs: matrix: lib-injection-connection: ['network'] lib-injection-use-admission-controller: ['', 'use-admission-controller'] - weblog-variant: ['dd-lib-ruby-init-test-rails'] + weblog-variant: ['dd-lib-ruby-init-test-rails-gemsrb'] fail-fast: false env: TEST_LIBRARY: ruby @@ -44,11 +44,11 @@ jobs: LIBRARY_INJECTION_ADMISSION_CONTROLLER: ${{ matrix.lib-injection-use-admission-controller }} DOCKER_REGISTRY_IMAGES_PATH: ghcr.io/datadog DOCKER_IMAGE_TAG: ${{ github.sha }} - BUILDX_PLATFORMS: linux/amd64 + BUILDX_PLATFORMS: linux/amd64,linux/arm64/v8 steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@5e02aeb8ff4555239d15ece4abf48db530b42510 + uses: DataDog/system-tests/lib-injection/runner@d2ccf0691213a9d515e932ab933528c38ae69023 with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} From 686073fd78f91a441e671643f2eb7646ff19f3f8 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 21:14:28 +0100 Subject: [PATCH 16/28] Test gems.rb --- .github/workflows/lib-injection.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 7e6734fe051..673c7279e77 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -48,7 +48,7 @@ jobs: steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@d2ccf0691213a9d515e932ab933528c38ae69023 + uses: DataDog/system-tests/lib-injection/runner@1af3241d5b6a928199528a8cbfc5698564f5d260 with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} From 41721e32dcb9fdbddbbe6929ceea0a29f391df7c Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 22:15:01 +0100 Subject: [PATCH 17/28] Test explicit --- .github/workflows/lib-injection.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 673c7279e77..29d6f116bbf 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -35,7 +35,7 @@ jobs: matrix: lib-injection-connection: ['network'] lib-injection-use-admission-controller: ['', 'use-admission-controller'] - weblog-variant: ['dd-lib-ruby-init-test-rails-gemsrb'] + weblog-variant: ['dd-lib-ruby-init-test-rails-explicit'] fail-fast: false env: TEST_LIBRARY: ruby From 19a3a4b3ffac605decb061883ec88bb367132a21 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 22:31:08 +0100 Subject: [PATCH 18/28] Handle gems.rb lockfile for trial --- lib-injection/auto_inject.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index ea4660e22a0..1326f0f5bc6 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -24,8 +24,13 @@ gemfile = Bundler::SharedHelpers.default_gemfile lockfile = Bundler::SharedHelpers.default_lockfile - datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") - datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") + if gemfile.basename == 'gems.rb' + datadog_gemfile = gemfile.dirname + "datadog-Gemfile" + datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" + else + datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") + datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") + end # Copies for trial FileUtils.cp gemfile, datadog_gemfile From 322deda86542dd81fbf9920212ba0f40ce45dc85 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 22:33:00 +0100 Subject: [PATCH 19/28] All variant --- .github/workflows/lib-injection.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 29d6f116bbf..f5ec7211950 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -35,7 +35,10 @@ jobs: matrix: lib-injection-connection: ['network'] lib-injection-use-admission-controller: ['', 'use-admission-controller'] - weblog-variant: ['dd-lib-ruby-init-test-rails-explicit'] + weblog-variant: + - dd-lib-ruby-init-test-rails + - dd-lib-ruby-init-test-rails-explicit + - dd-lib-ruby-init-test-rails-gemsrb fail-fast: false env: TEST_LIBRARY: ruby From e6baa62b92dead824ea5bce3a8f185323f0dc984 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Fri, 24 Feb 2023 23:17:07 +0100 Subject: [PATCH 20/28] Fix `gems.rb` check --- lib-injection/auto_inject.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 1326f0f5bc6..831eee9e376 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -24,7 +24,10 @@ gemfile = Bundler::SharedHelpers.default_gemfile lockfile = Bundler::SharedHelpers.default_lockfile - if gemfile.basename == 'gems.rb' + puts gemfile + puts lockfile + + if gemfile.basename.to_s == 'gems.rb' datadog_gemfile = gemfile.dirname + "datadog-Gemfile" datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" else @@ -32,6 +35,9 @@ datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") end + puts datadog_gemfile + puts datadog_lockfile + # Copies for trial FileUtils.cp gemfile, datadog_gemfile FileUtils.cp lockfile, datadog_lockfile From f656a0070ba39bc53f5d8f783efb5d8b8297b53c Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 27 Feb 2023 16:31:56 +0100 Subject: [PATCH 21/28] Refactor --- lib-injection/auto_inject.rb | 91 +++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 831eee9e376..8e9c8e6225a 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,67 +1,74 @@ -if !ENV['skip_autoinject'] - if system 'skip_autoinject=true bundle show ddtrace' - puts 'ddtrace already installed... skipping auto-injection' - else - version = "" - sha = "" - condition = - if !version.empty? - # For public release - "--version '#{version.gsub(/^v/, '')}'" - elsif !sha.empty? - # For internal testing - "--github 'datadog/dd-trace-rb' --ref '#{sha}'" - else - puts "NO VERSION" - end +return if ENV['skip_autoinject'] - puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" +if system 'skip_autoinject=true bundle show ddtrace' + puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' + return +end - require 'bundler' - require 'fileutils' +begin + require 'bundler' + # require 'shellwords' - gemfile = Bundler::SharedHelpers.default_gemfile - lockfile = Bundler::SharedHelpers.default_lockfile + if Bundler.frozen_bundle? + puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler" + return + end - puts gemfile - puts lockfile + version = "" + sha = "" - if gemfile.basename.to_s == 'gems.rb' - datadog_gemfile = gemfile.dirname + "datadog-Gemfile" - datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" - else - datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") - datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") - end + # Stronger restrict + condition = if !version.empty? + # For public release + "--version #{version.gsub(/^v/, '')}" + elsif !sha.empty? + # For internal testing + "--github datadog/dd-trace-rb --ref #{sha}" + end + + unless condition + puts "[DATADOG LIB INJECTION] NO VERSION" + return + end + + puts "[DATADOG LIB INJECTION] ddtrace is not installed... Perform lib injection for dd-trace-rb." - puts datadog_gemfile - puts datadog_lockfile + gemfile = Bundler::SharedHelpers.default_gemfile + lockfile = Bundler::SharedHelpers.default_lockfile + if gemfile.basename.to_s == 'gems.rb' + datadog_gemfile = gemfile.dirname + "datadog-Gemfile" + datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" + else + datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") + datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") + end + + require 'fileutils' + + begin # Copies for trial FileUtils.cp gemfile, datadog_gemfile FileUtils.cp lockfile, datadog_lockfile if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" - puts 'ddtrace added to bundle...' + puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' # Trial success, replace the original FileUtils.cp datadog_gemfile, gemfile FileUtils.cp datadog_lockfile, lockfile else - puts 'Something went wrong when adding ddtrace to bundle...' + puts '[DATADOG LIB INJECTION] Something went wrong when adding ddtrace to bundle...' end + rescue => e + puts "[DATADOG LIB INJECTION] #{e}" + ensure # Remove the copies FileUtils.rm datadog_gemfile FileUtils.rm datadog_lockfile end - - begin - require 'ddtrace' - rescue LoadError => e - puts e - else - puts 'ddtrace loaded...' - end +rescue LoadError, Bundler::BundlerError => e + puts "[DATADOG LIB INJECTION] #{e}" end From 90bf0b60054b3e7f89572518860bb48e01fa7c03 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 27 Feb 2023 17:36:44 +0100 Subject: [PATCH 22/28] Shell escape --- .github/workflows/lib-injection.yml | 2 +- lib-injection/auto_inject.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index f5ec7211950..3c5b84e99b6 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -51,7 +51,7 @@ jobs: steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@1af3241d5b6a928199528a8cbfc5698564f5d260 + uses: DataDog/system-tests/lib-injection/runner@ee7687cc29e05351066a6e3d269d8181db5d9ecd with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 8e9c8e6225a..4c891afb2e3 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -8,7 +8,7 @@ begin require 'bundler' - # require 'shellwords' + require 'shellwords' if Bundler.frozen_bundle? puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler" @@ -21,10 +21,10 @@ # Stronger restrict condition = if !version.empty? # For public release - "--version #{version.gsub(/^v/, '')}" + "--version #{version.gsub(/^v/, '').shellwords}" elsif !sha.empty? # For internal testing - "--github datadog/dd-trace-rb --ref #{sha}" + "--github datadog/dd-trace-rb --ref #{sha.shellwords}" end unless condition @@ -52,7 +52,7 @@ FileUtils.cp gemfile, datadog_gemfile FileUtils.cp lockfile, datadog_lockfile - if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile.to_s.shellwords} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' # Trial success, replace the original From a9bfad6d6a4e8dc4fbbcdd3b0e5a4c734ca59a42 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 27 Feb 2023 18:17:36 +0100 Subject: [PATCH 23/28] Change with open3 --- .github/workflows/lib-injection.yml | 2 +- lib-injection/auto_inject.rb | 30 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.github/workflows/lib-injection.yml b/.github/workflows/lib-injection.yml index 3c5b84e99b6..f5ec7211950 100644 --- a/.github/workflows/lib-injection.yml +++ b/.github/workflows/lib-injection.yml @@ -51,7 +51,7 @@ jobs: steps: - name: lib-injection test runner id: lib-injection-test-runner - uses: DataDog/system-tests/lib-injection/runner@ee7687cc29e05351066a6e3d269d8181db5d9ecd + uses: DataDog/system-tests/lib-injection/runner@1af3241d5b6a928199528a8cbfc5698564f5d260 with: docker-registry: ghcr.io docker-registry-username: ${{ github.repository_owner }} diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 4c891afb2e3..e807e676f84 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,12 +1,15 @@ - return if ENV['skip_autoinject'] -if system 'skip_autoinject=true bundle show ddtrace' - puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' - return -end - begin + require 'open3' + + _, status = Open3.capture2e({'skip_autoinject' => 'true'}, 'bundle show ddtrace') + + if status.success? + puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' + return + end + require 'bundler' require 'shellwords' @@ -21,10 +24,10 @@ # Stronger restrict condition = if !version.empty? # For public release - "--version #{version.gsub(/^v/, '').shellwords}" + "--version #{version.gsub(/^v/, '').shellescape}" elsif !sha.empty? # For internal testing - "--github datadog/dd-trace-rb --ref #{sha.shellwords}" + "--github datadog/dd-trace-rb --ref #{sha.shellescape}" end unless condition @@ -52,16 +55,19 @@ FileUtils.cp gemfile, datadog_gemfile FileUtils.cp lockfile, datadog_lockfile - if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile.to_s.shellwords} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + output, status = Open3.capture2e( + { 'skip_autoinject' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, + "bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + ) + + if status.success? puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' - # Trial success, replace the original FileUtils.cp datadog_gemfile, gemfile FileUtils.cp datadog_lockfile, lockfile else - puts '[DATADOG LIB INJECTION] Something went wrong when adding ddtrace to bundle...' + puts "[DATADOG LIB INJECTION] #{output}" end - rescue => e puts "[DATADOG LIB INJECTION] #{e}" ensure From d6388c5671dbaab22aa383da9ea01759cc2e0d26 Mon Sep 17 00:00:00 2001 From: Marco Costa Date: Mon, 27 Feb 2023 12:19:05 -0800 Subject: [PATCH 24/28] Update Dockerfile --- lib-injection/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/lib-injection/Dockerfile b/lib-injection/Dockerfile index 50e3461bcb5..1f65949ac86 100644 --- a/lib-injection/Dockerfile +++ b/lib-injection/Dockerfile @@ -2,6 +2,7 @@ # and auto instrument Ruby applications in containerized environments. FROM busybox +# Set high UID to prevent possible conflict with existing users: http://www.linfo.org/uid.html ARG UID=10000 ARG DDTRACE_RUBY_VERSION From db8301c9945caed8f5183280b57ad93e8f71db69 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 27 Feb 2023 21:24:31 +0100 Subject: [PATCH 25/28] Split puts statement for different rescue --- lib-injection/auto_inject.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index e807e676f84..7edfa2e4f04 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -69,12 +69,14 @@ puts "[DATADOG LIB INJECTION] #{output}" end rescue => e - puts "[DATADOG LIB INJECTION] #{e}" + puts "[DATADOG LIB INJECTION] trial error: #{e}" ensure # Remove the copies FileUtils.rm datadog_gemfile FileUtils.rm datadog_lockfile end -rescue LoadError, Bundler::BundlerError => e - puts "[DATADOG LIB INJECTION] #{e}" +rescue Bundler::BundlerError => e + puts "[DATADOG LIB INJECTION] BundlerError: #{e}" +rescue LoadError => e + puts "[DATADOG LIB INJECTION] LoadError: #{e}" end From c2e74344c0b90915356e3a7d7a194111f2ca7f3e Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Mon, 27 Feb 2023 21:28:56 +0100 Subject: [PATCH 26/28] Rescue all --- lib-injection/auto_inject.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 7edfa2e4f04..7c5231db566 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -79,4 +79,6 @@ puts "[DATADOG LIB INJECTION] BundlerError: #{e}" rescue LoadError => e puts "[DATADOG LIB INJECTION] LoadError: #{e}" +rescue Exception => e + puts "[DATADOG LIB INJECTION] Exception: #{e}" end From befe4f780b6555de24d258766872ea34c8df1120 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 28 Feb 2023 11:17:55 +0100 Subject: [PATCH 27/28] Improve scripting by extrace the add cmd --- lib-injection/auto_inject.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 7c5231db566..be8f68731a5 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -21,16 +21,16 @@ version = "" sha = "" - # Stronger restrict - condition = if !version.empty? - # For public release - "--version #{version.gsub(/^v/, '').shellescape}" - elsif !sha.empty? - # For internal testing - "--github datadog/dd-trace-rb --ref #{sha.shellescape}" - end + bundle_add_ddtrace_cmd = + if !version.empty? + # For public release + "bundle add ddtrace --require ddtrace/auto_instrument --version #{version.gsub(/^v/, '').shellescape}" + elsif !sha.empty? + # For internal testing + "bundle add ddtrace --require ddtrace/auto_instrument --github datadog/dd-trace-rb --ref #{sha.shellescape}" + end - unless condition + unless bundle_add_ddtrace_cmd puts "[DATADOG LIB INJECTION] NO VERSION" return end @@ -57,7 +57,7 @@ output, status = Open3.capture2e( { 'skip_autoinject' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, - "bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + bundle_add_ddtrace_cmd ) if status.success? @@ -66,7 +66,7 @@ FileUtils.cp datadog_gemfile, gemfile FileUtils.cp datadog_lockfile, lockfile else - puts "[DATADOG LIB INJECTION] #{output}" + puts "[DATADOG LIB INJECTION] Unable to add ddtrace: #{output}" end rescue => e puts "[DATADOG LIB INJECTION] trial error: #{e}" From 04999177fa4fc6faa3d085d56fd549f6bc4d3651 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 28 Feb 2023 14:56:49 +0100 Subject: [PATCH 28/28] Improve logging --- lib-injection/auto_inject.rb | 39 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index be8f68731a5..a548fb200e6 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,12 +1,15 @@ -return if ENV['skip_autoinject'] +return if ENV['DD_TRACE_SKIP_LIB_INJECTION'] == 'true' begin require 'open3' - _, status = Open3.capture2e({'skip_autoinject' => 'true'}, 'bundle show ddtrace') + failure_prefix = 'Datadog lib injection failed:' + support_message = 'For help solving this issue, please contact Datadog support at https://docs.datadoghq.com/help/.' + + _, status = Open3.capture2e({'DD_TRACE_SKIP_LIB_INJECTION' => 'true'}, 'bundle show ddtrace') if status.success? - puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' + STDOUT.puts '[ddtrace] ddtrace already installed... skipping auto-injection' if ENV['DD_TRACE_DEBUG'] == 'true' return end @@ -14,10 +17,11 @@ require 'shellwords' if Bundler.frozen_bundle? - puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler" + STDERR.puts "[ddtrace] #{failure_prefix} Cannot inject with frozen Gemfile, run `bundle config unset deployment` to allow lib injection. To learn more about bundler deployment, check https://bundler.io/guides/deploying.html#deploying-your-application. #{support_message}" return end + # `version` and `sha` should be replaced by docker build arguments version = "" sha = "" @@ -31,22 +35,17 @@ end unless bundle_add_ddtrace_cmd - puts "[DATADOG LIB INJECTION] NO VERSION" + STDERR.puts "[ddtrace] #{failure_prefix} Missing version specification. #{support_message}" return end - puts "[DATADOG LIB INJECTION] ddtrace is not installed... Perform lib injection for dd-trace-rb." + STDOUT.puts "[ddtrace] Performing lib injection with `#{bundle_add_ddtrace_cmd}`" if ENV['DD_TRACE_DEBUG'] == 'true' gemfile = Bundler::SharedHelpers.default_gemfile lockfile = Bundler::SharedHelpers.default_lockfile - if gemfile.basename.to_s == 'gems.rb' - datadog_gemfile = gemfile.dirname + "datadog-Gemfile" - datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" - else - datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") - datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") - end + datadog_gemfile = gemfile.dirname + "datadog-Gemfile" + datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" require 'fileutils' @@ -56,29 +55,23 @@ FileUtils.cp lockfile, datadog_lockfile output, status = Open3.capture2e( - { 'skip_autoinject' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, + { 'DD_TRACE_SKIP_LIB_INJECTION' => 'true', 'BUNDLE_GEMFILE' => datadog_gemfile.to_s }, bundle_add_ddtrace_cmd ) if status.success? - puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' + STDOUT.puts '[ddtrace] Datadog lib injection successfully added ddtrace to the application.' FileUtils.cp datadog_gemfile, gemfile FileUtils.cp datadog_lockfile, lockfile else - puts "[DATADOG LIB INJECTION] Unable to add ddtrace: #{output}" + STDERR.puts "[ddtrace] #{failure_prefix} Unable to add ddtrace. Error output:\n#{output.split("\n").map {|l| "[ddtrace] #{l}"}.join("\n")}\n#{support_message}" end - rescue => e - puts "[DATADOG LIB INJECTION] trial error: #{e}" ensure # Remove the copies FileUtils.rm datadog_gemfile FileUtils.rm datadog_lockfile end -rescue Bundler::BundlerError => e - puts "[DATADOG LIB INJECTION] BundlerError: #{e}" -rescue LoadError => e - puts "[DATADOG LIB INJECTION] LoadError: #{e}" rescue Exception => e - puts "[DATADOG LIB INJECTION] Exception: #{e}" + STDERR.puts "[ddtrace] #{failure_prefix} #{e.class.name} #{e.message}\nBacktrace: #{e.backtrace.join("\n")}\n#{support_message}" end