SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
refactor(query-service): ♻️ Update ldflags and Makefile for dynamic versioning (#655)
Prashant Shahi48ac20885ffe
diff --git a/Makefile b/Makefileindex b63d793131..21edec847d 100644--- a/Makefile+++ b/Makefile@@ -1,7 +1,14 @@+## Reference Guide - https://www.gnu.org/software/make/manual/make.html#++# Build variables+BUILD_VERSION ?= $(shell git describe --always --tags)+BUILD_HASH ?= $(shell git rev-parse --short HEAD)+BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")+BUILD_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)+# Internal variables or constants.-#FRONTEND_DIRECTORY ?= frontendFLATTENER_DIRECTORY ?= pkg/processors/flattenerQUERY_SERVICE_DIRECTORY ?= pkg/query-service@@ -13,6 +20,15 @@ FRONTEND_DOCKER_IMAGE ?= frontendQUERY_SERVICE_DOCKER_IMAGE ?= query-serviceFLATTERNER_DOCKER_IMAGE ?= flattener-processor+# Build-time Go variables+PACKAGE?=go.signoz.io/query-service+buildVersion=${PACKAGE}/version.buildVersion+buildHash=${PACKAGE}/version.buildHash+buildTime=${PACKAGE}/version.buildTime+gitBranch=${PACKAGE}/version.gitBranch++LD_FLAGS="-X ${buildHash}=${BUILD_HASH} -X ${buildTime}=${BUILD_TIME} -X ${buildVersion}=${BUILD_VERSION} -X ${gitBranch}=${BUILD_BRANCH}"+all: build-push-frontend build-push-query-service build-push-flattener# Steps to build and push docker image of frontend.PHONY: build-frontend-amd64 build-push-frontend@@ -47,7 +63,9 @@ build-query-service-amd64:@echo "--> Building query-service docker image for amd64"@echo "------------------"@cd $(QUERY_SERVICE_DIRECTORY) && \- docker build -f Dockerfile --no-cache -t $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) . --build-arg TARGETPLATFORM="linux/amd64"+ docker build -f Dockerfile --no-cache -t $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) . \+ --build-arg TARGETPLATFORM="linux/amd64" \+ --build-arg LD_FLAGS=$(LD_FLAGS)# Step to build and push docker image of query in amd64 and arm64 (used in push pipeline)build-push-query-service:@@ -56,10 +74,15 @@ build-push-query-service:@echo "------------------"ifndef DOCKER_SECOND_TAG@cd $(QUERY_SERVICE_DIRECTORY) && \- docker buildx build --file Dockerfile --progress plane --no-cache --push --platform linux/arm64,linux/amd64 --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) .+ docker buildx build --file Dockerfile --progress plane --no-cache --push \+ --platform linux/arm64,linux/amd64 . \+ --build-arg LD_FLAGS=$(LD_FLAGS) \+ --tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG)else@cd $(QUERY_SERVICE_DIRECTORY) && \- docker buildx build --file Dockerfile --progress plane --no-cache --push --platform linux/arm64,linux/amd64 . \+ docker buildx build --file Dockerfile --progress plane --no-cache \+ --push --platform linux/arm64,linux/amd64 . \+ --build-arg LD_FLAGS=$(LD_FLAGS) \--tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_TAG) \--tag $(REPONAME)/$(QUERY_SERVICE_DOCKER_IMAGE):$(DOCKER_SECOND_TAG)endifdiff --git a/pkg/query-service/Dockerfile b/pkg/query-service/Dockerfileindex 26352bf734..fe93291f79 100644--- a/pkg/query-service/Dockerfile+++ b/pkg/query-service/Dockerfile@@ -1,8 +1,7 @@FROM golang:1.14-buster AS builder-# Add Maintainer Info-LABEL maintainer="signoz"-+# LD_FLAGS is passed as argument from Makefile. It will be empty, if no argument passed+ARG LD_FLAGSARG TARGETPLATFORMENV CGO_ENABLED=1@@ -21,15 +20,22 @@ RUN go mod download -x# Add the sources and proceed with buildADD . .-RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o ./bin/query-service ./main.go+RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w $LD_FLAGS" -o ./bin/query-service ./main.goRUN chmod +x ./bin/query-service+# use a minimal alpine imageFROM alpine:3.7++# Add Maintainer Info+LABEL maintainer="signoz"+# add ca-certificates in case you need themRUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*+# set working directoryWORKDIR /root+# copy the binary from builderCOPY --from=builder /go/src/github.com/signoz/signoz/pkg/query-service/bin/query-service .@@ -37,7 +43,8 @@ COPY config/prometheus.yml /root/config/prometheus.yml# run the binaryENTRYPOINT ["./query-service"]+CMD ["-config", "/root/config/prometheus.yml"]# CMD ["./query-service -config /root/config/prometheus.yml"]-EXPOSE 8080+EXPOSE 8080diff --git a/pkg/query-service/main.go b/pkg/query-service/main.goindex b8e614868e..3418154d79 100644--- a/pkg/query-service/main.go+++ b/pkg/query-service/main.go@@ -10,6 +10,7 @@ import ("go.signoz.io/query-service/app""go.signoz.io/query-service/constants"+ "go.signoz.io/query-service/version""go.uber.org/zap""go.uber.org/zap/zapcore"@@ -31,7 +32,7 @@ func main() {defer loggerMgr.Sync() // flushes buffer, if anylogger := loggerMgr.Sugar()- logger.Debug("STARTING!")+ version.PrintVersion()serverOptions := &app.ServerOptions{// HTTPHostPort: v.GetString(app.HTTPHostPort),diff --git a/pkg/query-service/version/version.go b/pkg/query-service/version/version.goindex cad1bfa6f0..f53f42c544 100644--- a/pkg/query-service/version/version.go+++ b/pkg/query-service/version/version.go@@ -1,24 +1,48 @@package versionimport (+ "fmt"+ "runtime"+"go.uber.org/zap")// These fields are set during an official build// Global vars set from command-line argumentsvar (- version = "--"- buildhash = "--"- buildtime = "--"+ buildVersion = "--"+ buildHash = "--"+ buildTime = "--"+ gitBranch = "--")-//PrintVersionInfo displays the kyverno version - git version-func PrintVersionInfo() {- zap.S().Info("Version: ", version)- zap.S().Info("BuildHash: ", buildhash)- zap.S().Info("BuildTime: ", buildtime)+// BuildDetails returns a string containing details about the SigNoz query-service binary.+func BuildDetails() string {+ licenseInfo := `Licensed under the MIT License`++ return fmt.Sprintf(`+SigNoz version : %v+Commit SHA-1 : %v+Commit timestamp : %v+Branch : %v+Go version : %v++For SigNoz Official Documentation, visit https://signoz.io/docs+For SigNoz Community Slack, visit http://signoz.io/slack+For discussions about SigNoz, visit https://community.signoz.io++%s.+Copyright 2022 SigNoz+`,+ buildVersion, buildHash, buildTime, gitBranch,+ runtime.Version(), licenseInfo)+}++// PrintVersion prints version and other helpful information.+func PrintVersion() {+ zap.S().Infof("\n%s\n", BuildDetails())}func GetVersion() string {- return version+ return buildVersion}