Skip to content

Commit

Permalink
chore(images): extract Go OTel SDK setup into reusable module
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Sep 3, 2024
1 parent c600f41 commit f862a1a
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 227 deletions.
1 change: 1 addition & 0 deletions .github/actions/build-image/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ runs:
uses: docker/build-push-action@v6
with:
context: ${{ inputs.context }}
file: ${{ inputs.file != '' && inputs.file || format('{0}/Dockerfile', inputs.context) }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Maintenance note: keep the expression for building both platforms in sync with the "push" expression.
Expand Down
26 changes: 14 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ jobs:
with:
fetch-depth: 0

- name: build operator controller image
uses: ./.github/actions/build-image
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
imageName: operator-controller
imageTitle: Dash0 Kubernetes Operator Controller
imageDescription: the controller for the Dash0 Kubernetes operator
imageUrl: https://github.com/dash0hq/dash0-operator/tree/main
context: .

- name: build instrumentation image
uses: ./.github/actions/build-image
with:
Expand Down Expand Up @@ -106,17 +116,8 @@ jobs:
imageTitle: Dash0 Kubernetes Configuration Reloader
imageDescription: the configuration reloader for the Dash0 Kubernetes operator
imageUrl: https://github.com/dash0hq/dash0-operator/tree/main/images/configreloader
context: images/configreloader

- name: build operator controller image
uses: ./.github/actions/build-image
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
imageName: operator-controller
imageTitle: Dash0 Kubernetes Operator Controller
imageDescription: the controller for the Dash0 Kubernetes operator
imageUrl: https://github.com/dash0hq/dash0-operator/tree/main
context: .
context: images
file: images/configreloader/Dockerfile

- name: build filelog offset synch image
uses: ./.github/actions/build-image
Expand All @@ -126,7 +127,8 @@ jobs:
imageTitle: Dash0 Kubernetes Filelog Offset Synch
imageDescription: the filelog offset synch for the Dash0 Kubernetes operator
imageUrl: https://github.com/dash0hq/dash0-operator/tree/main
context: images/filelogoffsetsynch
context: images
file: images/filelogoffsetsynch/Dockerfile

publish-helm-chart-dry-run:
name: Publish Helm Chart (Dry Run)
Expand Down
29 changes: 17 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,26 @@ define build_container_image
$(eval $@_IMAGE_REPOSITORY = $(1))
$(eval $@_IMAGE_TAG = $(2))
$(eval $@_CONTEXT = $(3))
if [[ -n "$($@_IMAGE_REPOSITORY)" ]]; then \
if [[ "$($@_IMAGE_REPOSITORY)" = *"/"* ]]; then \
echo "not rebuilding the image $($@_IMAGE_REPOSITORY), this looks like a remote image"; \
else \
echo $(CONTAINER_TOOL) build -t $($@_IMAGE_REPOSITORY):$($@_IMAGE_TAG) $($@_CONTEXT); \
$(CONTAINER_TOOL) build -t $($@_IMAGE_REPOSITORY):$($@_IMAGE_TAG) $($@_CONTEXT); \
fi; \
elif [[ -n "$(OPERATOR_HELM_CHART_URL)" ]]; then \
echo "not rebuilding image, a remote Helm chart is used with the default image from the chart"; \
$(eval $@_DOCKERFILE = $(4))
if [[ -n "$($@_IMAGE_REPOSITORY)" ]]; then \
if [[ "$($@_IMAGE_REPOSITORY)" = *"/"* ]]; then \
echo "not rebuilding the image $($@_IMAGE_REPOSITORY), this looks like a remote image"; \
else \
dockerfile=$($@_DOCKERFILE); \
if [[ -z $$dockerfile ]]; then \
dockerfile=$($@_CONTEXT)/Dockerfile; \
fi; \
echo $(CONTAINER_TOOL) build -t $($@_IMAGE_REPOSITORY):$($@_IMAGE_TAG) -f $$dockerfile $($@_CONTEXT); \
$(CONTAINER_TOOL) build -t $($@_IMAGE_REPOSITORY):$($@_IMAGE_TAG) -f $$dockerfile $($@_CONTEXT); \
fi; \
elif [[ -n "$(OPERATOR_HELM_CHART_URL)" ]]; then \
echo "not rebuilding image, a remote Helm chart is used with the default image from the chart"; \
fi
endef

.PHONY: docker-build-controller
docker-build-controller: ## Build the manager container image.
@$(call build_container_image,$(CONTROLLER_IMG_REPOSITORY),$(CONTROLLER_IMG_TAG),".")
@$(call build_container_image,$(CONTROLLER_IMG_REPOSITORY),$(CONTROLLER_IMG_TAG),.)

.PHONY: docker-build-instrumentation
docker-build-instrumentation: ## Build the instrumentation image.
Expand All @@ -222,11 +227,11 @@ docker-build-collector: ## Build the OpenTelemetry collector container image.

.PHONY: docker-build-config-reloader
docker-build-config-reloader: ## Build the config reloader container image.
@$(call build_container_image,$(CONFIGURATION_RELOADER_IMG_REPOSITORY),$(CONFIGURATION_RELOADER_IMG_TAG),images/configreloader)
@$(call build_container_image,$(CONFIGURATION_RELOADER_IMG_REPOSITORY),$(CONFIGURATION_RELOADER_IMG_TAG),images,images/configreloader/Dockerfile)

.PHONY: docker-build-filelog-offset-synch
docker-build-filelog-offset-synch: ## Build the filelog offset synch container image.
@$(call build_container_image,$(FILELOG_OFFSET_SYNCH_IMG_REPOSITORY),$(FILELOG_OFFSET_SYNCH_IMG_TAG),images/filelogoffsetsynch)
@$(call build_container_image,$(FILELOG_OFFSET_SYNCH_IMG_REPOSITORY),$(FILELOG_OFFSET_SYNCH_IMG_TAG),images,images/filelogoffsetsynch/Dockerfile)

ifndef ignore-not-found
ignore-not-found = false
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.20.1
github.com/onsi/gomega v1.34.1
github.com/onsi/gomega v1.34.2
go.opentelemetry.io/collector/pdata v1.14.1
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.31.0
Expand Down Expand Up @@ -37,7 +37,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
Expand Down Expand Up @@ -71,6 +73,8 @@ github.com/onsi/ginkgo/v2 v2.20.1 h1:YlVIbqct+ZmnEph770q9Q7NVAz4wwIiVNahee6JyUzo
github.com/onsi/ginkgo/v2 v2.20.1/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
4 changes: 3 additions & 1 deletion images/configreloader/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Note: Expects dash0-operator/images as the build context.
FROM golang:alpine AS builder
COPY ./src /usr/local/go/src/configreloader
COPY configreloader/src /usr/local/go/src/configreloader
COPY pkg /usr/local/go/pkg
WORKDIR /usr/local/go/src/configreloader
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' configreloader

Expand Down
98 changes: 4 additions & 94 deletions images/configreloader/src/configreloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import (
"syscall"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
otelmetric "go.opentelemetry.io/otel/metric"
metricnoop "go.opentelemetry.io/otel/metric/noop"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"

"github.com/dash0hq/dash0-operator/images/pkg/common"
)

const (
Expand All @@ -43,7 +38,6 @@ var (
configurationFileHashes = make(map[string]string)

metricNamePrefix = fmt.Sprintf("%s.", meterName)
meterProvider otelmetric.MeterProvider

configFilesChangesMetricName = fmt.Sprintf("%s%s", metricNamePrefix, "config_file_changes")
configFilesChangesMetric otelmetric.Int64Counter
Expand Down Expand Up @@ -77,7 +71,7 @@ func main() {
done := make(chan bool, 1)
signal.Notify(shutdown, syscall.SIGTERM)

meter, selfMonitoringShutdownFunctions := setUpSelfMonitoring(ctx)
meter, selfMonitoringShutdownFunctions := common.InitOTelSdk(ctx, meterName)
setUpSelfMonitoringMetrics(meter)

go func() {
Expand All @@ -102,7 +96,7 @@ func main() {

<-done

shutDownSelfMonitoring(ctx, selfMonitoringShutdownFunctions)
common.ShutDownOTelSdk(ctx, selfMonitoringShutdownFunctions)
}

func initializeHashes(configurationFilePaths []string) error {
Expand Down Expand Up @@ -240,90 +234,6 @@ func triggerConfigurationReload(collectorPid OTelColPid) error {
return nil
}

func setUpSelfMonitoring(ctx context.Context) (otelmetric.Meter, []func(ctx context.Context) error) {
podUid, isSet := os.LookupEnv("K8S_POD_UID")
if !isSet {
log.Println("Env var 'K8S_POD_UID' is not set")
}

nodeName, isSet := os.LookupEnv("K8S_NODE_NAME")
if !isSet {
log.Println("Env var 'K8S_NODE_NAME' is not set")
}

var doMeterShutdown func(ctx context.Context) error

if _, isSet = os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT"); isSet {
var metricExporter sdkmetric.Exporter

protocol, isProtocolSet := os.LookupEnv("OTEL_EXPORTER_OTLP_PROTOCOL")
if !isProtocolSet {
// http/protobuf is the default transport protocol, see spec:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
protocol = "http/protobuf"
}

var err error
switch protocol {
case "grpc":
if metricExporter, err = otlpmetricgrpc.New(ctx); err != nil {
log.Fatalf("Cannot create the OTLP gRPC metrics exporter: %v", err)
}
case "http/protobuf":
if metricExporter, err = otlpmetrichttp.New(ctx); err != nil {
log.Fatalf("Cannot create the OTLP HTTP metrics exporter: %v", err)
}
case "http/json":
log.Fatalf("Cannot create the OTLP HTTP exporter: the protocol 'http/json' is currently unsupported")
default:
log.Fatalf("Unexpected OTLP protocol set as value of the 'OTEL_EXPORTER_OTLP_PROTOCOL' environment variable: %v", protocol)
}

r, err := resource.New(ctx,
resource.WithAttributes(
semconv.K8SPodUID(podUid),
semconv.K8SNodeName(nodeName),
),
)
if err != nil {
log.Fatalf("Cannot setup the OTLP resource: %v", err)
}

sdkMeterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(r),
sdkmetric.WithReader(
sdkmetric.NewPeriodicReader(
metricExporter,
sdkmetric.WithTimeout(10*time.Second),
sdkmetric.WithInterval(5*time.Second),
)),
)

meterProvider = sdkMeterProvider
doMeterShutdown = sdkMeterProvider.Shutdown
} else {
meterProvider = metricnoop.MeterProvider{}
doMeterShutdown = func(ctx context.Context) error { return nil }
}

otel.SetMeterProvider(meterProvider)

return meterProvider.Meter(meterName), []func(ctx context.Context) error{
doMeterShutdown,
}
}

func shutDownSelfMonitoring(ctx context.Context, shutdownFunctions []func(ctx context.Context) error) {
var err error
for _, shutdownFunction := range shutdownFunctions {
timeoutCtx, cancelFun := context.WithTimeout(ctx, time.Second)
if err = shutdownFunction(timeoutCtx); err != nil {
log.Printf("Failed to shutdown self monitoring, telemetry may have been lost:%v\n", err)
}
cancelFun()
}
}

func setUpSelfMonitoringMetrics(meter otelmetric.Meter) {
var err error

Expand Down
11 changes: 7 additions & 4 deletions images/configreloader/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ module github.com/dash0hq/dash0-operator/images/configreloader
go 1.22.4

require (
github.com/dash0hq/dash0-operator/images/pkg/common v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.29.0
go.opentelemetry.io/otel/metric v1.29.0
go.opentelemetry.io/otel/sdk v1.29.0
go.opentelemetry.io/otel/sdk/metric v1.29.0
)

require (
Expand All @@ -17,6 +14,10 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.28.0 // indirect
Expand All @@ -27,3 +28,5 @@ require (
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

replace github.com/dash0hq/dash0-operator/images/pkg/common => ../../pkg/common
4 changes: 3 additions & 1 deletion images/filelogoffsetsynch/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Note: Expects dash0-operator/images as the build context.
FROM golang:alpine AS builder
COPY ./src /usr/local/go/src/filelogoffsetsynch
COPY filelogoffsetsynch/src /usr/local/go/src/filelogoffsetsynch
COPY pkg /usr/local/go/pkg
WORKDIR /usr/local/go/src/filelogoffsetsynch
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' filelogoffsetsynch

Expand Down
Loading

0 comments on commit f862a1a

Please sign in to comment.