Skip to content

Commit

Permalink
feat(monitoring): add Prometheus sample (GoogleCloudPlatform#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriatgoogle authored Apr 16, 2021
1 parent 296ed9b commit 7ddb87e
Show file tree
Hide file tree
Showing 8 changed files with 1,062 additions and 1 deletion.
1 change: 1 addition & 0 deletions eventarc/testing/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/sendgrid/sendgrid-go.v2 v2.0.0/go.mod h1:OfCfy6FQ/DAD54p60SEW3Kw3PH2a/eKKeQvLyXx0jSo=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
google.golang.org/grpc v1.36.1
google.golang.org/grpc/examples v0.0.0-20200707005602-4258d12073b4
google.golang.org/protobuf v1.26.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/sendgrid/sendgrid-go.v2 v2.0.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/sendgrid/sendgrid-go.v2 v2.0.0 h1:6UejeR8xeTdffLkeFogj+BolU0rULxUH7yXSjO73Cf0=
gopkg.in/sendgrid/sendgrid-go.v2 v2.0.0/go.mod h1:OfCfy6FQ/DAD54p60SEW3Kw3PH2a/eKKeQvLyXx0jSo=
Expand Down
16 changes: 16 additions & 0 deletions monitoring/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/GoogleCloudPlatform/golang-samples/monitoring

go 1.13

replace github.com/GoogleCloudPlatform/golang-samples => ./..

require (
cloud.google.com/go v0.81.0
contrib.go.opencensus.io/exporter/stackdriver v0.13.5
github.com/GoogleCloudPlatform/golang-samples v0.0.0-00010101000000-000000000000
github.com/golang/protobuf v1.5.2
github.com/prometheus/client_golang v1.10.0
go.opencensus.io v0.23.0
google.golang.org/api v0.44.0
google.golang.org/genproto v0.0.0-20210415145412-64678f1ae2d5
)
865 changes: 865 additions & 0 deletions monitoring/go.sum

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions monitoring/prometheus/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2021 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

// [START monitoring_sli_metrics_prometheus_setup]
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// [END monitoring_sli_metrics_prometheus_setup]
// [START monitoring_sli_metrics_prometheus_create_metrics]
// Sets up metrics.
var (
requestCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "go_request_count",
Help: "total request count",
})
failedRequestCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "go_failed_request_count",
Help: "failed request count",
})
responseLatency = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "go_response_latency",
Help: "response latencies",
})
)

// [END monitoring_sli_metrics_prometheus_create_metrics]

func main() {
http.HandleFunc("/", handle)
// [START monitoring_sli_metrics_prometheus_metrics_endpoint]
http.Handle("/metrics", promhttp.Handler())
// [END monitoring_sli_metrics_prometheus_metrics_endpoint]
log.Fatal(http.ListenAndServe(":8080", nil))
}

func handle(w http.ResponseWriter, r *http.Request) {
// [START monitoring_sli_metrics_prometheus_latency]
requestReceived := time.Now()
defer func() {
responseLatency.Observe(time.Since(requestReceived).Seconds())
}()
// [END monitoring_sli_metrics_prometheus_latency]
// [START monitoring_sli_metrics_prometheus_counts]
requestCount.Inc()

// Fails 10% of the time.
if rand.Intn(100) >= 90 {
log.Printf("intentional failure encountered")
failedRequestCount.Inc()
http.Error(w, "intentional error!", http.StatusInternalServerError)
return
}
// [END monitoring_sli_metrics_prometheus_counts]
delay := time.Duration(rand.Intn(1000)) * time.Millisecond
time.Sleep(delay)
fmt.Fprintf(w, "Succeeded after %v", delay)
return
}
97 changes: 97 additions & 0 deletions monitoring/prometheus/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2021 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 (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"

"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func TestStartingServer(t *testing.T) {

// Tests build.
m := testutil.BuildMain(t)
if !m.Built() {
t.Fatalf("failed to build app")
}

// Tests main endpoint.
req := httptest.NewRequest("GET", "/", strings.NewReader(""))
req.Header.Add("Content-Type", "application/json")

rr := httptest.NewRecorder()
handle(rr, req)

// Tests response status.
gotStatus := rr.Code
gotBody := rr.Body.String()
wantGoodStatus := http.StatusOK
wantBadStatus := http.StatusInternalServerError

if gotStatus != wantGoodStatus && gotStatus != wantBadStatus {
t.Fatalf("Returned wrong status code: got %v want %v", gotStatus, strconv.Itoa(wantGoodStatus)+" or "+strconv.Itoa(wantBadStatus))
}

// Test response body.
// Acceptable responses are "intentional error!" and "succeeded after ...".
wantSuccess := "Succeeded after "
wantIntentionalError := "intentional error!"

if gotBody != wantIntentionalError && !strings.Contains(gotBody, wantSuccess) {
t.Fatalf("Response does not match expected: got %v", gotBody)
}

}

func TestMetricsEndpoint(t *testing.T) {

// Tests build.
m := testutil.BuildMain(t)
if !m.Built() {
t.Fatalf("failed to build app")
}

// Tests metrics endpoint.
req := httptest.NewRequest("GET", "/metrics", nil)
req.Header.Add("Content-Type", "application/json")

rr := httptest.NewRecorder()
handler := promhttp.Handler()
handler.ServeHTTP(rr, req)

// Tests response status.
gotStatus := rr.Code
gotBody := rr.Body.String()
wantGoodStatus := http.StatusOK

if gotStatus != wantGoodStatus {
t.Fatalf("Returned wrong status code: got %v want %v", gotStatus, wantGoodStatus)
}

// Test response body.
// Prometheus /metrics endpoint will contain "# HELP".
wantSuccess := "# HELP"

if !strings.Contains(gotBody, wantSuccess) {
t.Fatalf("Response does not match expected: got %v", rr.Body.String())
}

}
1 change: 1 addition & 0 deletions run/testing/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/sendgrid/sendgrid-go.v2 v2.0.0/go.mod h1:OfCfy6FQ/DAD54p60SEW3Kw3PH2a/eKKeQvLyXx0jSo=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit 7ddb87e

Please sign in to comment.