forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(monitoring): add Prometheus sample (GoogleCloudPlatform#2046)
- Loading branch information
1 parent
296ed9b
commit 7ddb87e
Showing
8 changed files
with
1,062 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters