Set Up Integration from SonarQube

SonarQube

Introduction

SonarQube is a self-hosted or cloud-enabled tool that scans source code to detect bugs, vulnerabilities, code smells, duplications, and coverage issues across 30+ languages. It integrates with build tools (Maven, Gradle, MSBuild) and CI/CD pipelines like Jenkins, GitLab, and GitHub Actions. With IDE plugins for VS Code, IntelliJ, Eclipse, and more, it enforces "Clean as You Code" practices during development. Built for DevSecOps, it can block pull requests or deployments if quality gates are not met.

Description:
SonarQube does not natively support direct integration with the Elastic Stack for sending code quality metrics. 

However, it can forward logs, metrics, or even SonarQube API data into Elasticsearch using custom setups.

What It Does:

Option 1: Send Logs to Elastic Using Filebeat

Description:

Use Filebeat to collect and forward SonarQube logs to Elasticsearch for centralized logging and visualization in Kibana.

What It Does:

Steps

Prepare SonarQube Logs

Install Filebeat on the SonarQube Host

Configure Filebeat to Read SonarQube Logs

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /opt/sonarqube/logs/*.log

output.elasticsearch:
  hosts: ["http://<elasticsearch-host>:9200"]
  username: "elastic"
  password: "your-password"

sudo systemctl enable filebeat
sudo systemctl start filebeat

Option 2: Push Metrics to Elastic via SonarQube API

Description:
Use SonarQube’s built-in Web API to extract code quality metrics and push them into Elasticsearch using a custom script.

What It Does:

Steps:

Enable API Access in SonarQube

GET /api/measures/component?component=<project_key>&metricKeys=bugs,vulnerabilities,coverage

Build a Custom Script (Python Example) 

import requests, json

sonar_url = "http://<sonarqube>/api/measures/component"
params = {"component": "your_project", "metricKeys": "bugs,vulnerabilities,code_smells"}

res = requests.get(sonar_url, params=params)
data = res.json()

# Send to Elasticsearch
es_url = "http://<elasticsearch>:9200/sonarqube-metrics/_doc"
requests.post(es_url, headers={"Content-Type": "application/json"}, data=json.dumps(data))

What Happens Next?

Action Result
SonarQube writes logs Filebeat ships them to Elasticsearch
Kibana receives log data Visualize system behavior and performance
Script pulls SonarQube metrics Elasticsearch stores code health data (via API)
Kibana dashboards can be created Visualize bugs, vulnerabilities, coverage, etc.

Requirements

Requirement Purpose
Filebeat Forwards log files to Elasticsearch
Elasticsearch Stores both logs and custom metrics
SonarQube API Source of code quality data
Custom Script Pulls metrics and pushes them to Elastic
Cron (optional) Automates periodic metric synchronization

Integration Mapping Summary

Component Function
SonarQube Logs Shipped to Elasticsearch via Filebeat
Filebeat Collects and forwards logs
Elasticsearch Stores logs and metrics
SonarQube API Retrieves code metrics like bugs, smells, coverage
Custom Script Pushes metrics to Elasticsearch
Kibana Visualizes logs and code quality over time

Revision #4
Created 19 June 2025 06:30:56 by Kent Lauron
Updated 19 June 2025 07:58:58 by Kent Lauron