Skip to content

Commit

Permalink
Re-enable code coverage using codecov (#2464)
Browse files Browse the repository at this point in the history
* Explicitly use `CODECOV_TOKEN`

* Verify that `CODECOV_TOKEN` is set

* Use an intermediary parameter

* Use an intermediary env var

* Use an intermediary env var

* Fix syntax error

* Setup CODECOV_TOKEN Environment Variable

* Pass `CODECOV_TOKEN` to the docker image

* Add initial `.codecov.yml`

* Remove debug logs

* Update `codecov.yml`

* Rename codecov.yml

* Validate codecov.yml

* Add graph to the comment layout

* [appsec] Install CodeCov Uploader Dependencies

* Install missing `sudo` dependency

* Set blocking coverage targets

* App appsec & tracer extension flags

* Create gpg file before valdation

* [appsec] Install gnupg

* Create `/root/.gnupg`

* Add slug env var (See Notes)

Notes: https://github.com/codecov/uploader/blob/6ccf3c3b145904308d38cdef1ad615df2ff6f9e0/src/ci_providers/provider_circleci.ts#L33-L49

* Detect CircleCI as the CI provider

* Run coverage while running the integration[s]/web tests

* fix: Only keep the last entry of xdebug.so files

* Merge coverage reports

* Add back again test_integrations_phpredis5 to the PHP 8.0 test suite

* Only include the Integrations/ directory

* Reset TEST_EXTRA_ENV after usage

* Only run test coverage for PHP 7.4+

* Fix circleci syntax

* Fix circleci syntax

* Upgrade phpredis5 scenario to redis-5.3.7

* Revert phpredis5 changes

* Revert phpredis5 changes

* Add gpg to the base buster container

* Use php-config to find the php extension directory

Co-authored-by: Bob Weinand <[email protected]>

* Merge coverage reports

* Add back again test_integrations_phpredis5 to the PHP 8.0 test suite

* Only include the Integrations/ directory

* Only run test coverage for PHP 7.4+

* Upgrade phpredis5 scenario to redis-5.3.7

* Revert phpredis5 changes

* Revert phpredis5 changes

* Merge master

* fix: Iteration over null

* fix: missing coverage_file definition

* fix: Undefined variable `data`

* fix: Unique codecov/upload upload_name

* fix: Unique codecov/upload upload_name

* Exclude `dogstatsd/`

---------

Co-authored-by: Bob Weinand <[email protected]>
  • Loading branch information
PROFeNoM and bwoebi authored Jan 16, 2024
1 parent ba0cf68 commit 32251e3
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 21 deletions.
127 changes: 114 additions & 13 deletions .circleci/continue_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ commands:
-e BASH_ENV=/home/circleci/bashenv/bash.sh \
-e CIRCLE_SHA1 \
-e CIRCLE_BRANCH \
-e CODECOV_TOKEN \
-e CI \
-e CIRCLECI \
-e CIRCLE_PROJECT_USERNAME \
-e CIRCLE_PROJECT_REPONAME \
-v $(pwd):/home/circleci/datadog \
-v /tmp/bashenv:/home/circleci/bashenv \
-v /rust:/rust \
Expand Down Expand Up @@ -982,7 +987,8 @@ jobs:
sudo apt install -y gpg
- codecov/upload:
file: tmp/coverage.info
upload_name: "PHP<< parameters.php_major_minor >>.dd-trace-php"
upload_name: "PHP<< parameters.php_major_minor >>.extension.dd-trace-php"
flags: tracer-extension
- run:
command: |
mkdir -p /tmp/artifacts/core_dumps
Expand Down Expand Up @@ -1135,7 +1141,7 @@ jobs:
command: |
export DEBIAN_FRONTEND=noninteractive
apt update
apt install -y wget git g++ gcc gcovr cmake make curl libcurl4-gnutls-dev clang clang-tidy clang-format git php-dev php-cgi cargo
apt install -y wget sudo git g++ gcc gcovr cmake make curl libcurl4-gnutls-dev clang clang-tidy clang-format git php-dev php-cgi cargo
- run: git config --global --add safe.directory /home/circleci/datadog/appsec/third_party/libddwaf
- run:
name: CMake
Expand Down Expand Up @@ -1168,9 +1174,17 @@ jobs:
pattern: "^[^.]+$"
value: << parameters.resource_class >>
steps:
- run:
name: Install CodeCov Uploader Dependencies
command: |
sudo apt update
sudo apt install -y gpg gnupg
sudo mkdir -p /root/.gnupg
sudo touch /root/.gnupg/trustedkeys.gpg
- codecov/upload:
file: appsec/coverage.xml
upload_name: "appsec-coverage >>"
flags: appsec-extension

lint_appsec:
parameters:
Expand Down Expand Up @@ -1353,6 +1367,9 @@ jobs:
disable_runner_distributed_tracing:
type: boolean
default: false
coverage:
type: boolean
default: false
<<: *BARE_DOCKER_MACHINE
environment:
COMPOSER_PROCESS_TIMEOUT: 0
Expand Down Expand Up @@ -1392,6 +1409,28 @@ jobs:
- run:
name: Run tests
command: DD_TRACE_AGENT_TIMEOUT=1000 <<# parameters.disable_runner_distributed_tracing >> DD_DISTRIBUTED_TRACING=false <</ parameters.disable_runner_distributed_tracing >> DD_TRACE_TEST_SAPI=<< parameters.sapi >> make << parameters.make_target >> PHPUNIT_OPTS="--log-junit test-results/php-composer/results.xml"
- when:
# codecov uploader only on amd64
condition:
and:
- equal: [ true, << parameters.coverage >> ]
- matches:
pattern: "^[^.]+$"
value: << parameters.resource_class >>
steps:
- run:
name: Merge coverage reports
command: |
make merge_coverage_reports
- run:
name: Install CodeCov Uploader Dependencies
command: |
sudo apt update
sudo apt install -y gpg
- codecov/upload:
file: reports/coverage.xml
upload_name: "PHP<< parameters.php_major_minor >>.<< parameters.make_target >>.dd-trace-php"
flags: tracer-integrations
- run:
command: |
mkdir -p /tmp/artifacts
Expand Down Expand Up @@ -1423,6 +1462,9 @@ jobs:
disable_runner_distributed_tracing:
type: boolean
default: false
coverage:
type: boolean
default: false
resource_class: << parameters.resource_class >>
executor:
name: with_integrations
Expand Down Expand Up @@ -1462,6 +1504,28 @@ jobs:
- run:
name: Run tests
command: DD_TRACE_AGENT_TIMEOUT=1000 <<# parameters.disable_runner_distributed_tracing >> DD_DISTRIBUTED_TRACING=false <</ parameters.disable_runner_distributed_tracing >> DD_TRACE_TEST_SAPI=<< parameters.sapi >> make << parameters.make_target >> RUST_DEBUG_SYMBOLS=1 PHPUNIT_OPTS="--log-junit test-results/php-composer/results.xml"
- when:
# codecov uploader only on amd64
condition:
and:
- equal: [ true, << parameters.coverage >> ]
- matches:
pattern: "^[^.]+$"
value: << parameters.resource_class >>
steps:
- run:
name: Merge coverage reports
command: |
make merge_coverage_reports
- run:
name: Install CodeCov Uploader Dependencies
command: |
sudo apt update
sudo apt install -y gpg
- codecov/upload:
file: reports/coverage.xml
upload_name: "PHP<< parameters.php_major_minor >>.<< parameters.make_target >>.dd-trace-php"
flags: tracer-integrations
- run:
command: |
mkdir -p /tmp/artifacts
Expand Down Expand Up @@ -4152,16 +4216,29 @@ workflows:
- '7.1'
- '7.2'
- '7.3'
make_target:
- test_composer
- test_integration
- test_distributed_tracing
- test_auto_instrumentation

- integration:
requires: [ 'Prepare Code' ]
resource_class: medium+
coverage: true
matrix:
parameters:
php_major_minor:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
make_target:
- test_composer
- test_integration
- test_distributed_tracing
- test_auto_instrumentation
- test_composer_coverage
- test_integration_coverage
- test_distributed_tracing_coverage
- test_auto_instrumentation_coverage

- integration_snapshots:
requires: [ 'Prepare Code' ]
Expand All @@ -4174,14 +4251,26 @@ workflows:
- '7.1'
- '7.2'
- '7.3'
make_target:
- test_web
- test_integrations

- integration_snapshots:
requires: [ 'Prepare Code' ]
# Due to Symfony OOM during composer update
resource_class: xlarge
coverage: true
matrix:
parameters:
php_major_minor:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
make_target:
- test_web
- test_integrations
- test_web_coverage
- test_integrations_coverage

- integration:
requires: [ 'Prepare Code' ]
Expand All @@ -4195,11 +4284,6 @@ workflows:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
make_target:
- test_distributed_tracing

Expand All @@ -4223,6 +4307,23 @@ workflows:
make_target:
- test_integrations_phpredis5

- integration:
requires: [ 'Prepare Code' ]
resource_class: medium+
sapi: fpm-fcgi
disable_runner_distributed_tracing: true
coverage: true
matrix:
parameters:
php_major_minor:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
make_target:
- test_distributed_tracing_coverage

- xdebug_tests:
requires: [ 'Prepare Code' ]
name: "PHP 70 Xdebug tests"
Expand Down
44 changes: 41 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TEST_OPCACHE_FILES = $(shell find tests/opcache -name '*.php*' -o -name '.gitkee
TEST_STUB_FILES = $(shell find tests/ext -type d -name 'stubs' -exec find '{}' -type f \; | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
INIT_HOOK_TEST_FILES = $(shell find tests/C2PHP -name '*.phpt' -o -name '*.inc' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
M4_FILES = $(shell find m4 -name '*.m4*' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' ) $(BUILD_DIR)/config.m4
XDEBUG_SO_FILE = $(shell find $(shell php-config --extension-dir) -type f -name "xdebug*.so" -exec basename {} \; | tail -n 1)

all: $(BUILD_DIR)/configure $(SO_FILE)

Expand Down Expand Up @@ -463,17 +464,20 @@ cores:
REQUEST_INIT_HOOK := -d ddtrace.request_init_hook=$(REQUEST_INIT_HOOK_PATH)
ENV_OVERRIDE := $(shell [ -n "${DD_TRACE_DOCKER_DEBUG}" ] && echo DD_AUTOLOAD_NO_COMPILE=true) DD_TRACE_CLI_ENABLED=true
TEST_EXTRA_INI ?=
TEST_EXTRA_ENV ?=

### DDTrace tests ###
TESTS_ROOT = ./tests
COMPOSER = $(if $(ASAN), ASAN_OPTIONS=detect_leaks=0) COMPOSER_MEMORY_LIMIT=-1 composer --no-interaction
COMPOSER_TESTS = $(COMPOSER) --working-dir=$(TESTS_ROOT)
PHPUNIT_OPTS ?=
PHPUNIT = $(TESTS_ROOT)/vendor/bin/phpunit $(PHPUNIT_OPTS) --config=$(TESTS_ROOT)/phpunit.xml
PHPUNIT_COVERAGE ?=
PHPBENCH_OPTS ?=
PHPBENCH_CONFIG ?= $(TESTS_ROOT)/phpbench.json
PHPBENCH_OPCACHE_CONFIG ?= $(TESTS_ROOT)/phpbench-opcache.json
PHPBENCH = $(TESTS_ROOT)/vendor/bin/phpbench $(PHPBENCH_OPTS) run
PHPCOV = $(TESTS_ROOT)/vendor/bin/phpcov

TEST_INTEGRATIONS_70 := \
test_integrations_deferred_loading \
Expand Down Expand Up @@ -938,14 +942,30 @@ TEST_WEB_83 := \

FILTER := .

define run_tests_without_coverage
$(TEST_EXTRA_ENV) $(ENV_OVERRIDE) php $(TEST_EXTRA_INI) $(REQUEST_INIT_HOOK) $(PHPUNIT) $(1) --filter=$(FILTER)
endef

define run_tests_with_coverage
$(TEST_EXTRA_ENV) $(ENV_OVERRIDE) php -d zend_extension=$(XDEBUG_SO_FILE) -d xdebug.mode=coverage $(TEST_EXTRA_INI) $(REQUEST_INIT_HOOK) $(PHPUNIT) $(1) --filter=$(FILTER) --coverage-php reports/cov/$(coverage_file)
endef

# Note: The condition below only checks for existence - i.e., whether PHPUNIT_COVERAGE is set to anything.
define run_tests
$(ENV_OVERRIDE) php $(TEST_EXTRA_INI) $(REQUEST_INIT_HOOK) $(PHPUNIT) $(1) --filter=$(FILTER)
$(eval coverage_file := $(shell echo $(1) | tr '[:upper:]' '[:lower:]' | tr '/=' '_' | tr -d '-').cov) \
$(if $(PHPUNIT_COVERAGE),$(call run_tests_with_coverage,$(1)),$(call run_tests_without_coverage,$(1)))
endef

define run_tests_debug
(set -o pipefail; { DD_TRACE_DEBUG=1 $(call run_tests,$(1)) 2>&1 >&3 | tee >(grep -vE '\[ddtrace\] \[debug\]|\[ddtrace\] \[info\]' >&2) | { ! (grep -E '\[error\]|\[warning\]|\[deprecated\]' >/dev/null && echo $$'\033[41m'"ERROR: Found debug log errors in the output."$$'\033[0m'); }; } 3>&1)
$(eval TEST_EXTRA_ENV=$(TEST_EXTRA_ENV) DD_TRACE_DEBUG=1)
(set -o pipefail; { $(call run_tests,$(1)) 2>&1 >&3 | \
tee >(grep -vE '\[ddtrace\] \[debug\]|\[ddtrace\] \[info\]' >&2) | \
{ ! (grep -E '\[error\]|\[warning\]|\[deprecated\]' >/dev/null && \
echo $$'\033[41m'"ERROR: Found debug log errors in the output."$$'\033[0m'); }; } 3>&1)
$(eval TEST_EXTRA_ENV=)
endef


define run_benchmarks
$(ENV_OVERRIDE) php $(TEST_EXTRA_INI) $(REQUEST_INIT_HOOK) $(PHPBENCH) --config=$(1) --filter=$(FILTER) --report=all --output=file --output=console
endef
Expand Down Expand Up @@ -997,17 +1017,25 @@ test_unit: global_test_run_dependencies

test_integration: global_test_run_dependencies
$(call run_tests,--testsuite=integration $(TESTS))
test_integration_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_integration

test_auto_instrumentation: global_test_run_dependencies
$(call run_tests,--testsuite=auto-instrumentation $(TESTS))
# Cleaning up composer.json files in tests/AutoInstrumentation modified for TLS during tests
git checkout $(TESTS_ROOT)/AutoInstrumentation/**/composer.json
test_auto_instrumentation_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_auto_instrumentation

test_composer: global_test_run_dependencies
$(call run_tests,--testsuite=composer-tests $(TESTS))
test_composer_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_composer

test_distributed_tracing: global_test_run_dependencies
$(call run_tests,--testsuite=distributed-tracing $(TESTS))
test_distributed_tracing_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_distributed_tracing

test_metrics: global_test_run_dependencies
$(call run_tests,--testsuite=metrics $(TESTS))
Expand All @@ -1028,7 +1056,9 @@ benchmarks_opcache: benchmarks_run_dependencies
test_opentelemetry_1: global_test_run_dependencies
rm -f tests/.scenarios.lock/opentelemetry1/composer.lock
$(MAKE) test_scenario_opentelemetry1
$(shell [ $(PHP_MAJOR_MINOR) -ge 81 ] && echo "OTEL_PHP_FIBERS_ENABLED=1" || echo "") DD_TRACE_OTEL_ENABLED=1 DD_TRACE_GENERATE_ROOT_SPAN=0 $(call run_tests,--testsuite=opentelemetry1 $(TESTS))
$(eval TEST_EXTRA_ENV=$(shell [ $(PHP_MAJOR_MINOR) -ge 81 ] && echo "OTEL_PHP_FIBERS_ENABLED=1" || echo '') DD_TRACE_OTEL_ENABLED=1 DD_TRACE_GENERATE_ROOT_SPAN=0)
$(call run_tests,--testsuite=opentelemetry1 $(TESTS))
$(eval TEST_EXTRA_ENV=)

test_opentracing_beta5: global_test_run_dependencies
$(MAKE) test_scenario_opentracing_beta5
Expand All @@ -1045,6 +1075,11 @@ test_opentracing_10: global_test_run_dependencies
test_integrations: $(TEST_INTEGRATIONS_$(PHP_MAJOR_MINOR))
test_web: $(TEST_WEB_$(PHP_MAJOR_MINOR))

test_web_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_web
test_integrations_coverage:
PHPUNIT_COVERAGE=1 $(MAKE) test_integrations

test_integrations_amqp2: global_test_run_dependencies
$(MAKE) test_scenario_amqp2
$(call run_tests_debug,tests/Integrations/AMQP)
Expand Down Expand Up @@ -1280,6 +1315,9 @@ test_web_custom: global_test_run_dependencies
test_scenario_%:
$(Q) $(COMPOSER_TESTS) scenario $*

merge_coverage_reports:
$(PHPCOV) merge --clover reports/coverage.xml reports/cov

### Api tests ###
API_TESTS_ROOT := ./tests/api

Expand Down
23 changes: 23 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Validate using: cat codecov.yml | curl --data-binary @- https://codecov.io/validate
codecov:
require_ci_to_pass: false # See coverage on CI test failures

coverage:
range: 90..100
round: down
precision: 2
status: # Blocking coverage targets
project: # Each PR must increase coverage
default:
target: auto
patch: # New code must be at least 90% tested
default:
target: 90%


comment: # See https://docs.codecov.com/docs/pull-request-comments
behavior: default
layout: "header,reach,diff,flags,files,footer"
require_changes: true
require_base: false
require_head: false
1 change: 1 addition & 0 deletions dockerfiles/ci/buster/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ENV DEVLIBS \
zlib1g-dev \
libasan5 \
gnupg \
gpg \
unixodbc-dev \
unixodbc

Expand Down
6 changes: 6 additions & 0 deletions src/Integrations/Integrations/Curl/CurlIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,17 @@ public function init()
$data = ObjectKVStore::get($hook->args[0], "span");
} elseif ($lastMh[0] == (int)$hook->args[0]) {
$data = $lastMh[1];
} else {
$data = null;
}

list(, $spans) = $data;

if (!isset($hook->returned["result"]) || $hook->returned["result"] == CURLE_OK) {
if (empty($spans)) {
return;
}

foreach ($spans as $requestSpan) {
list($ch, $requestSpan) = $requestSpan;
if ($ch === $handle) {
Expand Down
Loading

0 comments on commit 32251e3

Please sign in to comment.