Skip to content

Commit

Permalink
Add Go sample app (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jul 26, 2023
1 parent 4615702 commit 66edb36
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ the operator in various languages:
* [Java](sample-apps/java)
* [Python](sample-apps/python)
* DotNET (coming soon)
* Go (coming soon)
* [Go](sample-apps/go)
* [NodeJS + Java](sample-apps/nodejs-java)

Each of these sample apps works well with the [recipes](recipes) listed below.
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ the operator and auto-instrumentation. See below to get started:
* [Java](java)
* [Python](python)
* DotNET
* Go
* [Go](go)
* [NodeJS + Java](nodejs-java)

## Setup
Expand Down
26 changes: 26 additions & 0 deletions sample-apps/go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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
#
# https://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.

include ../../Makefile

.PHONY: build
build: sample-replace
docker build -t ${REGISTRY_LOCATION}-docker.pkg.dev/${GCLOUD_PROJECT}/${CONTAINER_REGISTRY}/go-sample-app app
docker build -t ${REGISTRY_LOCATION}-docker.pkg.dev/${GCLOUD_PROJECT}/${CONTAINER_REGISTRY}/go-sample-server server

.PHONY: push
push:
docker push ${REGISTRY_LOCATION}-docker.pkg.dev/${GCLOUD_PROJECT}/${CONTAINER_REGISTRY}/go-sample-app:latest
docker push ${REGISTRY_LOCATION}-docker.pkg.dev/${GCLOUD_PROJECT}/${CONTAINER_REGISTRY}/go-sample-server:latest

54 changes: 54 additions & 0 deletions sample-apps/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Go sample app

This is a sample app consisting of a basic client and server written in Go. The
server listens for requests which the client makes on a timed loop.

## Prerequisites

* OpenTelemetry Operator installed in your cluster
* Artifact Registry set up in your GCP project (see the
[main README.md](../../README.md#sample-applications))
* An `OpenTelemetryCollector` object already created in the current namespace,
such as [the sample `collector-config.yaml`](../../README.md#starting-the-Collector)
from the main [README](../../README.md)
* An `Instrumentation` object already created in the current namespace,
such as [the sample `instrumentation.yaml`](../../README.md#auto-instrumenting-applications)
from the main [README](../../README.md)

## Running

1. Build the sample app:
```
make build
```
This command will also update the local [manifests](k8s)
to refer to your image location.

2. Push the local image to the Artifact Registry you created
in the setup steps (if you did not create one, or are using an already created registry,
make sure to set the `REGISTRY_LOCATION` and `CONTAINER_REGISTRY` variables):
```
make push
```

3. Deploy the app in your cluster:
```
kubectl apply -f k8s/.
```
If you want to run the sample app in a specific namespace, pass `-n <your-namespace>`.

4. Run the following commands to patch the `app` and `server` deployments for auto-instrumentation:
```
kubectl patch deployment.apps/goshowcase-app -p '{"spec":{"template":{"metadata":{"annotations":{"instrumentation.opentelemetry.io/inject-go": "true", "instrumentation.opentelemetry.io/otel-go-auto-target-exe": "/app/main"}}}}}'
kubectl patch deployment.apps/goshowcase-server -p '{"spec":{"template":{"metadata":{"annotations":{"instrumentation.opentelemetry.io/inject-go": "true", "instrumentation.opentelemetry.io/otel-go-auto-target-exe": "/server/main"}}}}}'
```
These commands will use the `Instrumentation` created as part of the Prerequisites.

## View your Spans

To stream logs from the otel-collector, which will include spans from this sample application, run:
```
kubectl logs deployment/otel-collector -f
```

Alternatively, follow the [cloud-trace recipe](../../recipes/cloud-trace/) to view your spans in Google Cloud Trace.
18 changes: 18 additions & 0 deletions sample-apps/go/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
#
# https://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.

FROM golang:1.19
WORKDIR /app
COPY . .
RUN go build -o main
3 changes: 3 additions & 0 deletions sample-apps/go/app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/GoogleCloudPlatform/opentelemetry-operator-sample/sample-apps/go/app

go 1.20
42 changes: 42 additions & 0 deletions sample-apps/go/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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
//
// https://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 (
"io"
"log"
"net/http"
"time"
)

func main() {
for {
resp, err := http.Get("http://goshowcase-server/hello")
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

log.Printf("Body: %s\n", string(body))
err = resp.Body.Close()
if err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
}
}
40 changes: 40 additions & 0 deletions sample-apps/go/k8s/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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
#
# https://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.

apiVersion: apps/v1
kind: Deployment
metadata:
name: goshowcase-app
spec:
selector:
matchLabels:
app: goshowcase-app
template:
metadata:
name: goshowcase-app
labels:
app: goshowcase-app
spec:
containers:
- name: goshowcase-app
image: "%REGISTRY_LOCATION%-docker.pkg.dev/%GCLOUD_PROJECT%/%CONTAINER_REGISTRY%/go-sample-app:latest"
command: ['/app/main']
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "250m"
restartPolicy: Always
51 changes: 51 additions & 0 deletions sample-apps/go/k8s/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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
#
# https://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.

apiVersion: v1
kind: Service
metadata:
name: goshowcase-server
spec:
selector:
app: goshowcase-server
ports:
- port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: goshowcase-server
spec:
selector:
matchLabels:
app: goshowcase-server
template:
metadata:
labels:
app: goshowcase-server
spec:
containers:
- name: goshowcase-server
image: "%REGISTRY_LOCATION%-docker.pkg.dev/%GCLOUD_PROJECT%/%CONTAINER_REGISTRY%/go-sample-server:latest"
command: ['/server/main']
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "250m"
18 changes: 18 additions & 0 deletions sample-apps/go/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
#
# https://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.

FROM golang:1.19
WORKDIR /server
COPY . .
RUN go build -o main
3 changes: 3 additions & 0 deletions sample-apps/go/server/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/GoogleCloudPlatform/opentelemetry-operator-sample/sample-apps/go/server

go 1.20
33 changes: 33 additions & 0 deletions sample-apps/go/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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
//
// https://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 (
"fmt"
"log"
"net/http"
)

func hello(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, "hello\n")
}

func main() {
http.HandleFunc("/hello", hello)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 66edb36

Please sign in to comment.