Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit b69cca1

Browse files
committed
cloud-run button
1 parent b52628e commit b69cca1

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,9 @@ ss.AddSample([]string{"method", "const"}, 200)
2525

2626
The [full example](example/main.go) can be run from a cloud shell console to test how metrics are collected and displayed.
2727

28+
You can also try out the example using Cloud Run!
29+
30+
[![Run on Google Cloud](https://storage.googleapis.com/cloudrun/button.svg)](https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/google/go-metrics-stackdriver.git&cloudshell_working_dir=example)
31+
2832

2933
__This is not an officially supported Google product.__

example/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.12 as build
2+
3+
WORKDIR /go/src/app
4+
ADD . /go/src/app
5+
6+
ENV GOPROXY https://proxy.golang.org
7+
RUN go get -d -v ./...
8+
RUN go build -v -o /go/bin/app
9+
10+
# Now copy it into our base image.
11+
FROM gcr.io/distroless/base
12+
COPY --from=build /go/bin/app /
13+
CMD ["/app"]
14+

example/main.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ package main
1515

1616
import (
1717
"context"
18+
"fmt"
1819
"log"
1920
"math/rand"
21+
"net/http"
2022
"os"
2123
"os/signal"
2224
"time"
@@ -46,13 +48,38 @@ func main() {
4648
projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")
4749
}
4850

49-
log.Printf("initializing sink")
51+
log.Printf("initializing sink, project_id: %q", projectID)
5052

5153
// create sink
5254
ss := stackdriver.NewSink(client, &stackdriver.Config{
5355
ProjectID: projectID,
5456
Location: "us-east1-c",
5557
})
58+
cfg := metrics.DefaultConfig("go-metrics-stackdriver")
59+
cfg.EnableHostname = false
60+
metrics.NewGlobal(metrics.DefaultConfig("go-metrics-stackdriver"), ss)
61+
62+
// start listener
63+
log.Printf("starting server")
64+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
65+
defer metrics.MeasureSince([]string{"handler"}, time.Now())
66+
metrics.IncrCounter([]string{"requests"}, 1.0)
67+
fmt.Fprintf(w, "Hello from go-metrics-stackdriver")
68+
})
69+
70+
port := os.Getenv("PORT")
71+
if port == "" {
72+
port = "8080"
73+
}
74+
75+
srv := http.Server{
76+
Addr: ":" + port,
77+
}
78+
go func() {
79+
if err := srv.ListenAndServe(); err != nil {
80+
log.Printf("server error: %s", err)
81+
}
82+
}()
5683

5784
// capture ctrl+c
5885
c := make(chan os.Signal, 1)
@@ -61,6 +88,7 @@ func main() {
6188
<-c
6289
log.Printf("ctrl+c detected... shutting down")
6390
cancel()
91+
srv.Shutdown(ctx)
6492
}()
6593

6694
// generate data

0 commit comments

Comments
 (0)