| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Command server runs a simple HTTP server with integrated Cloud Trace (OpenTelemetry) |
| 16 |
// and health checks. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"go.opentelemetry.io/otel" |
| 25 |
"log" |
| 26 |
"net/http" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
|
| 30 |
"gocloud.dev/gcp" |
| 31 |
"gocloud.dev/server" |
| 32 |
"gocloud.dev/server/health" |
| 33 |
"gocloud.dev/server/sdserver" |
| 34 |
) |
| 35 |
|
| 36 |
// GlobalMonitoredResource implements monitoredresource.Interface to provide a |
| 37 |
// basic global resource based on the project ID. If you're running this sample |
| 38 |
// on GCE or EC2, you may prefer to use monitoredresource.Autodetect() instead. |
| 39 |
type GlobalMonitoredResource struct { |
| 40 |
projectID string |
| 41 |
} |
| 42 |
|
| 43 |
// MonitoredResource returned the monitored resource. |
| 44 |
func (g GlobalMonitoredResource) MonitoredResource() (string, map[string]string) { |
| 45 |
return "global", map[string]string{"project_id": g.projectID} |
| 46 |
} |
| 47 |
|
| 48 |
func helloHandler(w http.ResponseWriter, req *http.Request) { |
| 49 |
fmt.Fprintf(w, "Hello\n") |
| 50 |
} |
| 51 |
|
| 52 |
func mainHandler(w http.ResponseWriter, req *http.Request) { |
| 53 |
fmt.Fprintf(w, "Welcome to the home page!\n") |
| 54 |
} |
| 55 |
|
| 56 |
// customHealthCheck is an example health check. It implements the |
| 57 |
// health.Checker interface and reports the server is healthy when the healthy |
| 58 |
// field is set to true. |
| 59 |
type customHealthCheck struct { |
| 60 |
mu sync.RWMutex |
| 61 |
healthy bool |
| 62 |
} |
| 63 |
|
| 64 |
func (h *customHealthCheck) CheckHealth() error { |
| 65 |
h.mu.RLock() |
| 66 |
defer h.mu.RUnlock() |
| 67 |
if !h.healthy { |
| 68 |
return errors.New("not ready yet") |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
func main() { |
| 74 |
addr := flag.String("listen", ":8080", "HTTP port to listen on") |
| 75 |
doTrace := flag.Bool("trace", true, "Export traces to Stackdriver") |
| 76 |
flag.Parse() |
| 77 |
|
| 78 |
ctx := context.Background() |
| 79 |
credentials, err := gcp.DefaultCredentials(ctx) |
| 80 |
if err != nil { |
| 81 |
log.Fatal(err) |
| 82 |
} |
| 83 |
|
| 84 |
projectID, err := gcp.DefaultProjectID(credentials) |
| 85 |
if err != nil { |
| 86 |
log.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
if *doTrace { |
| 90 |
fmt.Println("Exporting traces to Stackdriver") |
| 91 |
|
| 92 |
traceSampler := sdserver.NewTraceSampler(ctx) |
| 93 |
|
| 94 |
spanExporter, err0 := sdserver.NewTraceExporter(projectID) |
| 95 |
if err0 != nil { |
| 96 |
log.Fatal(err0) |
| 97 |
} |
| 98 |
tp, cleanup, err0 := sdserver.NewTraceProvider(ctx, spanExporter, traceSampler) |
| 99 |
if err0 != nil { |
| 100 |
log.Fatal(err0) |
| 101 |
} |
| 102 |
defer cleanup() |
| 103 |
otel.SetTracerProvider(tp) |
| 104 |
|
| 105 |
metricsReader, err0 := sdserver.NewMetricsReader(projectID) |
| 106 |
if err0 != nil { |
| 107 |
log.Fatal(err0) |
| 108 |
} |
| 109 |
mp, cleanup2, err0 := sdserver.NewMeterProvider(ctx, metricsReader) |
| 110 |
if err0 != nil { |
| 111 |
log.Fatal(err0) |
| 112 |
} |
| 113 |
defer cleanup2() |
| 114 |
otel.SetMeterProvider(mp) |
| 115 |
} |
| 116 |
|
| 117 |
mux := http.NewServeMux() |
| 118 |
mux.HandleFunc("/hello", helloHandler) |
| 119 |
mux.HandleFunc("/", mainHandler) |
| 120 |
|
| 121 |
// healthCheck will report the server is unhealthy for 10 seconds after |
| 122 |
// startup, and as healthy henceforth. Check the /healthz/readiness |
| 123 |
// HTTP path to see readiness. |
| 124 |
healthCheck := new(customHealthCheck) |
| 125 |
time.AfterFunc(10*time.Second, func() { |
| 126 |
healthCheck.mu.Lock() |
| 127 |
defer healthCheck.mu.Unlock() |
| 128 |
healthCheck.healthy = true |
| 129 |
}) |
| 130 |
|
| 131 |
options := &server.Options{ |
| 132 |
RequestLogger: sdserver.NewRequestLogger(), |
| 133 |
HealthChecks: []health.Checker{healthCheck}, |
| 134 |
|
| 135 |
Driver: &server.DefaultDriver{}, |
| 136 |
} |
| 137 |
|
| 138 |
s := server.New(mux, options) |
| 139 |
fmt.Printf("Listening on %s\n", *addr) |
| 140 |
|
| 141 |
err = s.ListenAndServe(*addr) |
| 142 |
if err != nil { |
| 143 |
log.Fatal(err) |
| 144 |
} |
| 145 |
} |