This code demonstrates development of a HTTP probe for monitoring of a web application. It take advantage of OpenCensus instrumentation with the Go HTTP client and other features in OpenCensus, including integration with Stackdriver.
This example can be run either locally, on Google Kubernetes Engine, or be adapted to run elsewhere. Before running it enable the Stackdriver Monitoring and Trace APIs in order to permit collection of metrics. Also, create a service account. Download service account credentials json file and export the shell environment variable GOOGLE_APPLICATION_CREDENTIALS:
export GOOGLE_APPLICATION_CREDENTIALS=credentials.json
Edit the file config.yaml and set the target URL to the target that you intend to monitor.
To run locally, first install Go and OpenCensus.
export ZONE=my-zone
go run http_probe.go
Install Docker. Run the commands
docker build -t http_probe_container .
docker run -it --rm \
-e "GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS" \
-e "ZONE=$ZONE" \
--name http_probe \
http_probe_container
Upload to the Google Container Repository
gcloud auth configure-docker
TAG=v1
PROJECT_ID=[your project id]
docker tag http_probe_container gcr.io/$PROJECT_ID/httpgoprobe:$TAG
docker push gcr.io/$PROJECT_ID/httpgoprobe:$TAG
Start up a GKE cluster in each zone:
export ZONE=us-central1-a
gcloud components install kubectl
gcloud config set project $PROJECT_ID
gcloud config set compute/zone $ZONE
CLUSTER_NAME=probes-$ZONE
gcloud container clusters create $CLUSTER_NAME \
--zone $ZONE \
--num-nodes 1 \
--enable-cloud-logging
gcloud container clusters get-credentials $CLUSTER_NAME --zone $ZONE
Deploy a probe for each zone
HTTP_PROBE_WORKLOAD=httpprobe-$ZONE
kubectl run $HTTP_PROBE_WORKLOAD --image gcr.io/$PROJECT_ID/httpgoprobe:$TAG \
--env "GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS" \
--env "ZONE=$ZONE" \
--limits="cpu=50m,memory=128Mi"
https://opencensus.io/gogrpc/index.html https://github.com/census-instrumentation/opencensus-go https://github.com/census-instrumentation/opencensus-go/tree/master/examples/http https://godoc.org/go.opencensus.io/plugin/ochttp
Apache 2.0 Copyright 2018 Google. All rights reserved.