Skip to content

Commit

Permalink
Set up OTel collector distribution
Browse files Browse the repository at this point in the history
Change-Id: Id50c8ba3443ac2808f7a672799afc4c55eafbd32
  • Loading branch information
ridwanmsharif committed Oct 5, 2023
1 parent bb3d1d4 commit 6e5c9ec
Show file tree
Hide file tree
Showing 306 changed files with 18,733 additions and 37 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.20 as builder
WORKDIR /sidecar
COPY . .

RUN apt-get update && apt-get install gettext-base
RUN go install github.com/client9/misspell/cmd/[email protected] \
&& go install github.com/golangci/golangci-lint/cmd/[email protected] \
&& go install github.com/google/[email protected]
RUN apt update && apt install -y make
RUN make build-collector

FROM alpine:3
RUN apk add --no-cache ca-certificates
COPY --from=builder /sidecar/bin/rungmpcol /rungmpcol
COPY collector-config.yaml /etc/rungmp/config.yaml

ENTRYPOINT ["/rungmpcol"]
CMD ["--config", "/etc/rungmp/config.yaml"]
148 changes: 148 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# read PKG_VERSION from VERSION file
include VERSION

# if GOOS is not supplied, set default value based on user's system, will be overridden for OS specific packaging commands
GOOS ?= $(shell go env GOOS)

ALL_SRC := $(shell find . -name '*.go' -type f | sort)
ALL_DOC := $(shell find . \( -name "*.md" -o -name "*.yaml" \) -type f | sort)
GIT_SHA := $(shell git rev-parse --short HEAD)

BUILD_INFO_IMPORT_PATH := collector/internal/version
BUILD_X1 := -X $(BUILD_INFO_IMPORT_PATH).GitHash=$(GIT_SHA)
BUILD_X2 := -X $(BUILD_INFO_IMPORT_PATH).Version=$(PKG_VERSION)
LD_FLAGS := -ldflags "${BUILD_X1} ${BUILD_X2}"

TOOLS_DIR := internal/tools

.EXPORT_ALL_VARIABLES:

.DEFAULT_GOAL := presubmit

# --------------------------
# Helper Commands
# --------------------------

.PHONY: update-components-old
update-components-old:
grep -o github.com/open-telemetry/opentelemetry-collector-contrib/[[:lower:]]*/[[:lower:]]* go.mod | xargs -I '{}' go get {}
go mod tidy
cd $(TOOLS_DIR) && go get -u github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen
cd $(TOOLS_DIR) && go mod tidy

OTEL_VER ?= latest
.PHONY: update-components
update-components:
go list -m -f '{{if not (or .Indirect .Main)}}{{.Path}}{{end}}' all | \
grep "^go.opentelemetry.io" | \
grep -v "go.opentelemetry.io/collector/featuregate" | \
grep -v "go.opentelemetry.io/collector/pdata" | \
xargs -t -I '{}' go get {}@$(OTEL_VER)
go list -m -f '{{if not (or .Indirect .Main)}}{{.Path}}{{end}}' all | \
grep "^github.com/open-telemetry/opentelemetry-collector-contrib" | \
xargs -t -I '{}' go get {}@$(OTEL_VER)
go mod tidy
cd $(TOOLS_DIR) && go get -u github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen@$(OTEL_VER)
cd $(TOOLS_DIR) && go mod tidy

# We can bring this target back when https://github.com/open-telemetry/opentelemetry-collector/issues/8063 is resolved.
update-opentelemetry:
$(MAKE) update-components
$(MAKE) install-tools
$(MAKE) GO_BUILD_TAGS=gpu generate

# --------------------------
# Tools
# --------------------------

.PHONY: install-tools
install-tools:
cd $(TOOLS_DIR) && \
go install \
github.com/client9/misspell/cmd/misspell \
github.com/golangci/golangci-lint/cmd/golangci-lint \
github.com/google/addlicense \
github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen

.PHONY: addlicense
addlicense:
addlicense -c "Google LLC" -l apache $(ALL_SRC)

.PHONY: checklicense
checklicense:
@output=`addlicense -check $(ALL_SRC)` && echo checklicense finished successfully || (echo checklicense errors: $$output && exit 1)

.PHONY: lint
lint:
golangci-lint run --allow-parallel-runners --build-tags=$(GO_BUILD_TAGS) --timeout=20m

.PHONY: misspell
misspell:
@output=`misspell -error $(ALL_DOC)` && echo misspell finished successfully || (echo misspell errors:\\n$$output && exit 1)

# --------------------------
# CI
# --------------------------

# Adds license headers to files that are missing it, quiet tests
# so full output is visible at a glance.
.PHONY: precommit
precommit: addlicense lint misspell test

# Checks for the presence of required license headers, runs verbose
# tests for complete information in CI job.
.PHONY: presubmit
presubmit: checklicense lint misspell test_verbose

# --------------------------
# Build and Test
# --------------------------

GO_BUILD_OUT ?= ./bin/rungmpcol
.PHONY: build-collector
build-collector:
CGO_ENABLED=0 go build -tags=$(GO_BUILD_TAGS) -o $(GO_BUILD_OUT) $(LD_FLAGS) -buildvcs=false ./collector/cmd/rungmpcol

OTELCOL_BINARY = google-cloud-run-rmp-sidecar-$(GOOS)
.PHONY: build-collector-full-name
build-collector-full-name:
$(MAKE) GO_BUILD_OUT=./bin/$(OTELCOL_BINARY) build-collector

.PHONY: test
test:
go test -tags=$(GO_BUILD_TAGS) $(GO_TEST_VERBOSE) -race ./...

.PHONY: test_quiet
test_verbose:
$(MAKE) GO_TEST_VERBOSE=-v test

.PHONY: generate
generate:
go generate ./...

# --------------------
# Docker
# --------------------

# set default docker build image name
BUILD_IMAGE_NAME ?= rungmpcol-build
BUILD_IMAGE_REPO ?= gcr.io/stackdriver-test-143416/opentelemetry-operations-collector:test

.PHONY: docker-build-image
docker-build-image:
docker build -t $(BUILD_IMAGE_NAME) .

.PHONY: docker-push-image
docker-push-image:
docker tag $(BUILD_IMAGE_NAME) $(BUILD_IMAGE_REPO)
docker push $(BUILD_IMAGE_REPO)

.PHONY: docker-build-and-push
docker-build-and-push: docker-build-image docker-push-image

# Usage: make TARGET=<target> docker-run
# Example: make TARGET=build-collector docker-run
TARGET ?= build_collector
.PHONY: docker-run
docker-run:
docker run -e PKG_VERSION -v $(CURDIR):/mnt -w /mnt $(BUILD_IMAGE_NAME) /bin/bash -c "make $(TARGET)"
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The bundled configuration file for Cloud Build (`cloudbuild.yaml`) requires a ne
* `roles/artifactregistry.createOnPushWriter`
* `roles/run.admin`

Running `create-service-account.sh` creates a new service account `run-otel-example-sa@<project-id>.iam.gserviceaccount.com` for you. Then launch a Cloud Build task with `gcloud` command.
Running `create-service-account.sh` creates a new service account `run-gmp-sa@<project-id>.iam.gserviceaccount.com` for you. Then launch a Cloud Build task with `gcloud` command.

```console
./create-service-account.sh
Expand All @@ -67,7 +67,7 @@ gcloud builds submit . --config=cloudbuild.yaml
After the build, run the following command to check the endpoint URL.

```console
gcloud run services describe opentelemetry-cloud-run-sample --region=us-east1 --format="value(status.url)"
gcloud run services describe run-gmp-sidecar-service --region=us-east1 --format="value(status.url)"
```

#### Build and Run Manually
Expand All @@ -82,7 +82,7 @@ commands:

```
export GCP_PROJECT=<project-id>
gcloud artifacts repositories create run-otel-example \
gcloud artifacts repositories create run-gmp \
--repository-format=docker \
--location=us-east1
```
Expand All @@ -98,8 +98,8 @@ Build and push the app with the following commands:

```
pushd app
docker build -t us-east1-docker.pkg.dev/$GCP_PROJECT/run-otel-example/sample-app .
docker push us-east1-docker.pkg.dev/$GCP_PROJECT/run-otel-example/sample-app
docker build -t us-east1-docker.pkg.dev/$GCP_PROJECT/run-gmp/sample-app .
docker push us-east1-docker.pkg.dev/$GCP_PROJECT/run-gmp/sample-app
popd
```

Expand All @@ -112,10 +112,8 @@ config file with it.
Build the Collector image with the following commands:

```
pushd collector
docker build -t us-east1-docker.pkg.dev/$GCP_PROJECT/run-otel-example/collector .
docker push us-east1-docker.pkg.dev/$GCP_PROJECT/run-otel-example/collector
popd
docker build -t us-east1-docker.pkg.dev/$GCP_PROJECT/run-gmp/collector .
docker push us-east1-docker.pkg.dev/$GCP_PROJECT/run-gmp/collector
```

##### Create the Cloud Run Service
Expand All @@ -127,8 +125,8 @@ Replace the `%SAMPLE_APP_IMAGE%` and `%OTELCOL_IMAGE%` placeholders in
`run-service.yaml` with the images you built above, ie:

```
sed -i s@%OTELCOL_IMAGE%@us-east1-docker.pkg.dev/${GCP_PROJECT}/run-otel-example/collector@g run-service.yaml
sed -i s@%SAMPLE_APP_IMAGE%@us-east1-docker.pkg.dev/${GCP_PROJECT}/run-otel-example/sample-app@g run-service.yaml
sed -i s@%OTELCOL_IMAGE%@us-east1-docker.pkg.dev/${GCP_PROJECT}/run-gmp/collector@g run-service.yaml
sed -i s@%SAMPLE_APP_IMAGE%@us-east1-docker.pkg.dev/${GCP_PROJECT}/run-gmp/sample-app@g run-service.yaml
```

Create the Service with the following command:
Expand All @@ -145,7 +143,7 @@ Finally before you make make the request to the URL, you need to change
the Cloud Run service policy to accept unauthenticated HTTP access.

```
gcloud run services set-iam-policy opentelemetry-cloud-run-sample policy.yaml
gcloud run services set-iam-policy run-gmp-sidecar-service policy.yaml
```

### View telemetry in Google Cloud
Expand All @@ -170,8 +168,8 @@ Updated sidecar-sample-counter metric!
After running the demo, please make sure to clean up your project so that you don't consume unexpected resources and get charged.

```console
gcloud run services delete opentelemetry-cloud-run-sample --region us-east1 --quiet
gcloud artifacts repositories delete run-otel-example \
gcloud run services delete run-gmp-sidecar-service --region us-east1 --quiet
gcloud artifacts repositories delete run-gmp \
--location=us-east1 \
--quiet
```
3 changes: 3 additions & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The release version and build number of the google-cloud-metrics-agent package.
PKG_VERSION=0.0.2
PKG_BUILD=1
14 changes: 14 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand Down
Binary file removed app/sample-app
Binary file not shown.
6 changes: 3 additions & 3 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ steps:
- BUILD_SAMPLE_APP

- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "${_IMAGE_COLLECTOR}", "./collector"]
args: ["build", "-t", "${_IMAGE_COLLECTOR}", "."]
id: BUILD_COLLECTOR
waitFor: ["-"]

Expand Down Expand Up @@ -81,10 +81,10 @@ steps:

substitutions:
_REGION: us-east1
_REGISTRY: ${_REGION}-docker.pkg.dev/${PROJECT_ID}/run-otel-example
_REGISTRY: ${_REGION}-docker.pkg.dev/${PROJECT_ID}/run-gmp
_IMAGE_APP: ${_REGISTRY}/sample-app
_IMAGE_COLLECTOR: ${_REGISTRY}/collector
_SA_NAME: run-otel-example-sa
_SA_NAME: run-gmp-sa

images:
- ${_IMAGE_APP}
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions collector/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# See golang.org/issue/9281
* -text
27 changes: 27 additions & 0 deletions collector/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
bin/
dist/

## Editors

# GoLand IDEA
/.idea/
*.iml

# VS Code
.vscode

# Emacs
*~
\#*\#

# Miscellaneous files
*.sw[op]
*.DS_Store

# Coverage
coverage.txt
coverage.html

# Wix
*.wixobj
*.wixpdb
62 changes: 62 additions & 0 deletions collector/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

run:
timeout: 5m

skip-dirs:
- receiver/prometheusreceiver

linters:
enable:
- gocritic
- gofmt
- goimports
- revive
- govet
- misspell
- exportloopref
- staticcheck
- unconvert

issues:
exclude-rules:
- path: _test\.go
linters:
- exportloopref

linters-settings:
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/GoogleCloudPlatform/opentelemetry-operations-collector
govet:
# report about shadowed variables
check-shadowing: true
# settings per analyzer
settings:
printf: # analyzer name, run `go tool vet help` to see all analyzers
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
disable:
- fieldalignment
enable-all: true
misspell:
locale: US
Loading

0 comments on commit 6e5c9ec

Please sign in to comment.