Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:

permissions:
contents: read
security-events: write

jobs:
analyze:
Expand Down Expand Up @@ -58,7 +59,7 @@ jobs:
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

Expand All @@ -73,7 +74,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
uses: github/codeql-action/init@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
Expand Down Expand Up @@ -101,6 +102,6 @@ jobs:
exit 1

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
uses: github/codeql-action/analyze@ff0a06e83cb2de871e5a09832bc6a81e7276941f # v3.28.18
with:
category: "/language:${{matrix.language}}"
8 changes: 3 additions & 5 deletions .github/workflows/go-linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0
with:
egress-policy: audit

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
- uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@2226d7cb06a077cd73e56eedd38eecad18e5d837 # v6.5.0
with:
version: v1.60
uses: golangci/golangci-lint-action@v7
33 changes: 14 additions & 19 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
version: "2"
linters:
enable-all: true
#enable-all: true
disable:
# Deprecated
- gomnd

# We don't want these ones
# We don't want these
- forcetypeassert
- gochecknoglobals
- godox
Expand All @@ -29,21 +27,18 @@ linters:
- depguard
- tagalign

linters-settings:
funlen:
lines: -1 # (disabled)
statements: 100
settings:
funlen:
lines: -1 # (disabled)
statements: 100

cyclop:
max-complexity: 20
cyclop:
max-complexity: 20

lll:
line-length: 140
lll:
line-length: 140

nestif:
min-complexity: 18
nestif:
min-complexity: 18

gci:
sections:
- standard
- default
...
8 changes: 4 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ before:
- go mod tidy

builds:
# Generator build
- main: generator/main.go
binary: generator
id: generator
# Exporter build
- main: main.go
binary: gitlab-ci-exporter
id: gitlab-ci-exporter

env:
- CGO_ENABLED=0
Expand Down
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
##################################################
# Gitlab-CI Exporter - Makefile
##################################################
NAME := gitlab-ci-exporter
FILES := $(shell git ls-files */*.go)
COVERAGE_FILE := coverage.out
REPOSITORY := "helvethink/$(NAME)"
.DEFAULT_GOAL := help
GOLANG_VERSION := 1.23
GOLANGCI_LINT_VERSION ?= v2.1.5

.PHONY: lint
lint: ## Run all lint related tests againts the codebase
@echo "--- Linting code..."
docker run --rm -w /workdir -v $(PWD):/workdir golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) golangci-lint run -c .golangci.yml --fix

.PHONY: run
run: ## Run the tests against the codebase
@echo "Run app:"
$(GO) run cmd/${APP_NAME}/main.go

.PHONY: coverage
coverage: ## Prints coverage report
command

.PHONY: coverage-html
coverage-html: ## Generates coverage report and displays it in the browser
go tool cover -html=coverage.out

.PHONY: install
install: ## Build and install locally the binary (dev purpose)
go install ./cmd/$(NAME)

.PHONY: build
build: ## Build the binaries using local GOOS
go build -o ./bin/$(NAME) ./cmd/$(NAME)

.PHONY: clean
clean: ## Remove binary if it exists
rm -f $(NAME)

.PHONY: all
all: lint run build coverage ## Test, builds and ship package for all supported platforms

.PHONY: help
help: ## Displays this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
2 changes: 1 addition & 1 deletion main.go → cmd/gitlab-ci-exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
2025 Helvethink <technical@helvethink.ch>
*/
package main

Expand Down
15 changes: 7 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
// Package cmd
package cmd

import (
"fmt"
"log"
"os"

"github.com/helvethink/gitlab-ci-exporter/internal/collectors"
"github.com/helvethink/gitlab-ci-exporter/internal/httpServer"
"github.com/helvethink/gitlab-ci-exporter/internal/logging"
"github.com/prometheus/common/version"
"log"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "gitlab-ci-exporter",
Short: "A gitlab CI exporter",
Expand Down Expand Up @@ -56,6 +54,7 @@ func Execute() {
}
}

// gochecknoinits:skip
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
Expand Down Expand Up @@ -95,7 +94,7 @@ func checkCoreSettings() error {

// Check if the settings are correctly sets before starting app
if stgs.LogLevel == "" {
return fmt.Errorf("Missing Log Level")
return fmt.Errorf("missing Log Level")
}

return nil
Expand Down
29 changes: 29 additions & 0 deletions coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
#
# Code coverage generation

COVERAGE_DIR="${COVERAGE_DIR:-coverage}"
PKG_LIST=$(go list ./... | grep -v /vendor/)

# Create the coverage files directory
mkdir -p "$COVERAGE_DIR";

# Create a coverage file for each package
for package in ${PKG_LIST}; do
go test -covermode=count -coverprofile "${COVERAGE_DIR}/${package##*/}.cov" "$package" ;
done ;

# Merge the coverage profile files
echo 'mode: count' > "${COVERAGE_DIR}"/coverage.cov ;
tail -q -n +2 "${COVERAGE_DIR}"/*.cov >> "${COVERAGE_DIR}"/coverage.cov ;

# Display the global code coverage
go tool cover -func="${COVERAGE_DIR}"/coverage.cov ;

# If needed, generate HTML report
if [ "$1" == "html" ]; then
go tool cover -html="${COVERAGE_DIR}"/coverage.cov -o coverage.html ;
fi

# Remove the coverage files directory
rm -rf "$COVERAGE_DIR";
14 changes: 7 additions & 7 deletions internal/collectors/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,23 @@ type Exporter struct {
Logger *slog.Logger
}

// Describe
// Describe Metrics function.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
//TODO implement me
ch <- e.metrics.sampleMetric1
ch <- e.metrics.sampleMetric2
}

// Collect metrics configured and returns them as prometheus metrics
// Implements prometheus.Collector
// Implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

// Init collector Sample 1.
ch <- prometheus.MustNewConstMetric(
e.metrics.sampleMetric1,
prometheus.GaugeValue,
e.sampleMetric1(),
"labelValue",
)

// Init collector Sample 2.
ch <- prometheus.MustNewConstMetric(
e.metrics.sampleMetric2,
prometheus.GaugeValue,
Expand All @@ -64,7 +63,7 @@ func (e *Exporter) sampleMetric2() float64 {
return rand.Float64()
}

// Initializes the metrics
// NewMetrics Initializes the metrics.
func NewMetrics() *metrics {
return &metrics{
sampleMetric1: prometheus.NewDesc(
Expand All @@ -81,13 +80,14 @@ func NewMetrics() *metrics {
}
}

// Initialize the exporter
// NewExporter Initialize the exporter.
func NewExporter(settings *Settings, logger *slog.Logger) (*Exporter, error) {
metrics := NewMetrics()
exporter := &Exporter{
metrics: metrics,
Settings: settings,
Logger: logger,
}

return exporter, nil
}
10 changes: 5 additions & 5 deletions internal/httpServer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const (
</html>`
)

// Serves root page with html template on root page
// Serves metrics on settings.MetricPath
// NewServer Serves root page with html template on root page
// Serves metrics on settings.MetricPath.
func NewServer(e *collectors.Exporter) *http.Server {
s := e.Settings
t := template.Must(template.New("root").Parse(rootTemplate))

reg := prometheus.NewRegistry()
reg.MustRegister(
e,
//versioncollector.NewCollector("exporter"),
//promCollectors.NewBuildCollector(),
//promCollectors.NewGoCollector(),
// versioncollector.NewCollector("exporter"),
// promCollectors.NewBuildCollector(),
// promCollectors.NewGoCollector(),
)

promHandlerOpts := promhttp.HandlerOpts{
Expand Down
4 changes: 2 additions & 2 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewLogger(level, format string) (logger *slog.Logger, err error) {
slogHandlerOptions = slog.HandlerOptions{
Level: slog.LevelInfo,
}
err = fmt.Errorf("Log level not recognized, falling back to info")
err = fmt.Errorf("log level not recognized, falling back to info")
}

switch strings.ToLower(format) {
Expand All @@ -42,7 +42,7 @@ func NewLogger(level, format string) (logger *slog.Logger, err error) {
slogHandler = slog.NewJSONHandler(os.Stdout, &slogHandlerOptions)
default:
slogHandler = slog.NewTextHandler(os.Stdout, &slogHandlerOptions)
err = fmt.Errorf("Log format not recognized, falling back to text")
err = fmt.Errorf("log format not recognized, falling back to text")
}

return slog.New(slogHandler), err
Expand Down
Loading