stages:
  - docker-images-reliability-env
  - generate
  - benchmarks
  - post-pr-comment
  - macrobenchmarks
  - gates
  - config-validation
  - test-apps

variables:
  # This base image is created here: https://gitlab.ddbuild.io/DataDog/apm-reliability/benchmarking-platform/-/pipelines/73663692
  BASE_CI_IMAGE: 486234852809.dkr.ecr.us-east-1.amazonaws.com/ci/benchmarking-platform:dd-trace-go-73663692
  INDEX_FILE: index.txt
  BENCHMARK_TARGETS: "BenchmarkStartRequestSpan|BenchmarkHttpServeTrace|BenchmarkTracerAddSpans|BenchmarkStartSpan|BenchmarkSingleSpanRetention|BenchmarkOTelApiWithCustomTags|BenchmarkInjectW3C|BenchmarkExtractW3C|BenchmarkPartialFlushing|BenchmarkConfig|BenchmarkStartSpanConfig|BenchmarkGraphQL|BenchmarkSampleWAFContext|BenchmarkCaptureStackTrace|BenchmarkSetTagString|BenchmarkSetTagStringPtr|BenchmarkSetTagMetric|BenchmarkSetTagStringer|BenchmarkSerializeSpanLinksInMeta|BenchmarkLogs|BenchmarkParallelLogs|BenchmarkMetrics|BenchmarkParallelMetrics"

workflow:
  auto_cancel: 
    on_new_commit: interruptible
 
# In order to run benchmarks in parallel, we generate a matrix of test names based on the BENCHMARK_TARGETS variable.
# This will be used in tandem with bp-runner in benchmarks.yml.
# This will allow us to spin up a child job in GitLab CI that handles running all of the benchmarks in parallel.
generate_matrix:
  stage: generate
  image: $BASE_CI_IMAGE
  tags: ["arch:amd64"]
  rules:
    # We don't want to run benchmarks when pushing release tags for nested modules
    - if: '$CI_COMMIT_TAG =~ /^.*\/v\d+\.\d+\.\d+(?:-.*\.\d+)?$/'
      when: never
    # We don't want to run benchmarks on graphite commits
    - if: '$CI_COMMIT_BRANCH =~ /^graphite-base\/.*$/'
      when: never
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
      interruptible: false
    - when: always
      interruptible: true
  script: |
    cd .gitlab
    go run generate_config.go
    mv generated_benchmark_matrix.yml ../generated_benchmark_matrix.yml
  artifacts:
    paths:
      - generated_benchmark_matrix.yml
    expire_in: 1 hour # Artifact is temporary, needed only for the current pipeline

trigger_child_pipeline:
  stage: benchmarks
  trigger:
    include:
      - artifact: generated_benchmark_matrix.yml
        job: generate_matrix
    strategy: depend
  needs:
    - generate_matrix
  variables:
      PARENT_PIPELINE_ID: $CI_PIPELINE_ID

check-big-regressions:
  stage: benchmarks
  needs:
    - job: trigger_child_pipeline
  when: on_success
  tags:
    - "arch:amd64"
  image: $BASE_CI_IMAGE
  script:
    - |
      pwd
      export ARTIFACTS_DIR="$(pwd)/reports" && (mkdir "${ARTIFACTS_DIR}" || :)
      git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.ddbuild.io/DataDog/".insteadOf "https://github.com/DataDog/"
      git clone --branch dd-trace-go https://github.com/DataDog/benchmarking-platform platform && cd platform
      ./steps/download-child-pipeline-artifacts.sh
      pushd "${ARTIFACTS_DIR}/"
      pwd
      bp-runner ../platform/bp-runner.fail-on-regression.yml --debug
  artifacts:
    name: "artifacts"
    when: always
    paths:
      - reports/
    expire_in: "30 days"

# Config Registry CI Jobs
validate_supported_configurations_v2_local_file:
  stage: config-validation
  image: registry.ddbuild.io/ci/libdatadog-build/packaging:82290795
  tags: ["runner:apm-k8s-tweaked-metal"]
  rules:
    - when: on_success
  extends: .validate_supported_configurations_v2_local_file
  variables:
    LOCAL_JSON_PATH: "internal/env/supported_configurations.json"
    BACKFILLED: "false"

update_central_configurations_version_range_v2:
  stage: config-validation
  image: registry.ddbuild.io/ci/libdatadog-build/packaging:82290795
  tags: ["runner:apm-k8s-tweaked-metal"]
  extends: .update_central_configurations_version_range_v2
  variables:
    LOCAL_REPO_NAME: "dd-trace-go"
    LOCAL_JSON_PATH: "internal/env/supported_configurations.json"
    LANGUAGE_NAME: "golang"

include:
  - local: ".gitlab/macrobenchmarks.yml"
  - local: ".gitlab/test-apps.yml"
  - local: ".gitlab/docker-images-reliability-env.yml"
  - local: ".gitlab/configuration-central-validation.yml"
