Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: RobustPerception/azure_metrics_exporter
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: ministryofjustice/azure_metrics_exporter
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Mar 1, 2022

  1. Pttp 9482 (#1)

    * Firs attempt at a pipeline to build docker image
    
    * fix minor typo
    
    * try with latest version of go
    
    * mInor Typo, get it?
    
    I'll see myself out
    
    Co-authored-by: reallydontask <12355.noreply@notifications.github.com>
    reallydontask and reallydontask authored Mar 1, 2022
    Copy the full SHA
    3a968d3 View commit details

Commits on Apr 28, 2022

  1. Hack to avoid scrape going belly up due to duplicate metrics (#3)

    See comments on extracMetrics method
    
    Co-authored-by: reallydontask <12355.noreply@notifications.github.com>
    reallydontask and reallydontask authored Apr 28, 2022
    Copy the full SHA
    95a8288 View commit details
Showing with 87 additions and 2 deletions.
  1. +75 −0 .github/workflows/build-and-publish-docker-image.yaml
  2. +1 −1 Dockerfile
  3. +11 −1 main.go
75 changes: 75 additions & 0 deletions .github/workflows/build-and-publish-docker-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: build-and-publish-docker-image
on:
push:
branches:
- master
paths:
- '**.go'
- Dockerfile
- .github/workflows/build-and-publish-docker-image.yaml
pull_request:
branches:
- master
paths:
- '**.go'
- Dockerfile
- .github/workflows/build-and-publish-docker-image.yaml
workflow_dispatch:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
MAJOR_VERSION: 0
MINOR_VERSION: 1

jobs:
build-and-push:
outputs:
image_version: ${{env.MAJOR_VERSION}}.${{env.MINOR_VERSION}}.${{ github.run_number }}
runs-on:
- ubuntu-20.04
steps:
- name: Clone repo
uses: actions/checkout@v2

- name: Generate image tags
id: tag
shell: bash
run: |
echo "Extracting branch tag from ${GITHUB_REF}"
BRANCH=$(echo ${GITHUB_REF##*/} | tr -cd '[:alnum:]._-')
echo "::set-output name=tag1::${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.MAJOR_VERSION}}.${{env.MINOR_VERSION}}.${{ github.run_number }}"
echo "Setting tag1=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.MAJOR_VERSION}}.${{env.MINOR_VERSION}}.${{ github.run_number }}"
echo "::set-output name=tag2::${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_SHA}"
echo "Setting tag2=${GITHUB_SHA}"
if [[ ( "${GITHUB_EVENT_NAME}" == "push" || "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ) && ( "${BRANCH}" == "master" || "${BRANCH}" == "main" ) ]]; then
echo "::set-output name=tag3::${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
echo "Setting tag3=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
else
echo "::set-output name=tag3::${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop"
echo "Setting tag3=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:develop"
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@27d0a4f181a40b142cce983c5393082c365d1480 # version 1.2.0

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@94ab11c41e45d028884a99163086648e898eed25 # version 1.6.0

- name: Log in to the Container registry
uses: docker/login-action@6af3c118c8376c675363897acf1757f7a9be6583 # version 1.13.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}


- name: Build and push Docker images
uses: docker/build-push-action@7f9d37fa544684fb73bfe4835ed7214c255ce02b # version 2.9.0
with:
context: .
push: true
tags: "${{ steps.tag.outputs.tag1 }},${{ steps.tag.outputs.tag2 }},${{ steps.tag.outputs.tag3 }}"


2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11 as builder
FROM golang:1.17 as builder
WORKDIR /go/src/github.com/RobustPerception/azure_metrics_exporter
COPY . .
RUN make build
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -54,6 +54,12 @@ type resourceMeta struct {
}

func (c *Collector) extractMetrics(ch chan<- prometheus.Metric, rm resourceMeta, httpStatusCode int, metricValueData AzureMetricValueResponse, publishedResources map[string]bool) {

// The Azure Monitor API seems to sometimes return the same metric multiple times, I guess depending on the sliding window, thus we store whether it's been published already.
// This does mean that we WILL lose metrics if the same metric is published multiple times, but that's better than the alternative, which means losing all metrics on that processing run.
// After some investigation I'm not 100% that we will lose metrics, guess we need to test properly
processedMetrics := make(map[string]bool)

if httpStatusCode != 200 {
log.Printf("Received %d status for resource %s. %s", httpStatusCode, rm.resourceURL, metricValueData.APIError.Message)
return
@@ -78,7 +84,7 @@ func (c *Collector) extractMetrics(ch chan<- prometheus.Metric, rm resourceMeta,
}
metricName = invalidMetricChars.ReplaceAllString(metricName, "_")

if len(value.Timeseries) > 0 {
if len(value.Timeseries) > 0 && !processedMetrics[rm.resource.ID+metricName] {
metricValue := value.Timeseries[0].Data[len(value.Timeseries[0].Data)-1]
labels := CreateResourceLabels(rm.resourceURL)

@@ -113,7 +119,11 @@ func (c *Collector) extractMetrics(ch chan<- prometheus.Metric, rm resourceMeta,
metricValue.Maximum,
)
}
} else if len(value.Timeseries) > 0 && processedMetrics[rm.resource.ID+metricName] {
metricValue := value.Timeseries[0].Data[len(value.Timeseries[0].Data)-1]
log.Printf("Skipping metric %s to avoid duplicate metrics. TimeStamp: %s Total: %f Average: %f Min: %f Max: %f.\n", metricName, metricValue.TimeStamp, metricValue.Total, metricValue.Average, metricValue.Minimum, metricValue.Maximum)
}
processedMetrics[rm.resource.ID+metricName] = true
}

if _, ok := publishedResources[rm.resource.ID]; !ok {