Documentation
¶
Overview ¶
Package golangservice is a set of packages to help with creating services using golang for logging and testing
golangservice contains the following packages:
The log package provides some logging helpers for structured contextual logs ¶
The metrics package prodives helpers for statsd ¶
The handlers package provides a set of handlers that handle http.Request log the results ¶
The nettest package provides a set of helpers for use when testing networks ¶
The validate package provides input validate for user requests
Directories
¶
Path | Synopsis |
---|---|
Package handlers provides a collection of logging http.Handlers for use by HTTP services that take in configuration from environment variables Combining Handlers We can combine the following handlers automatically or manually Usage: r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.AllHandlers(r) http.ListenAndServe(":1123", loggedRouter) They can also be manually chained together loggedRouter := handlers.StatsdHandler(handlers.HealthdHandler(r)) Logging Context This creates a logging context to be passed into the handling function with information about the request Usage: r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { log.Ctx(r.Context()).Info("log a message with the context") w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.LogContextHandler(r) http.ListenAndServe(":1123", loggedRouter) Healthd This provides healthd logging (http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html) for health monitoring when using Elastic Beanstalk.
|
Package handlers provides a collection of logging http.Handlers for use by HTTP services that take in configuration from environment variables Combining Handlers We can combine the following handlers automatically or manually Usage: r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.AllHandlers(r) http.ListenAndServe(":1123", loggedRouter) They can also be manually chained together loggedRouter := handlers.StatsdHandler(handlers.HealthdHandler(r)) Logging Context This creates a logging context to be passed into the handling function with information about the request Usage: r := mux.NewRouter() r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { log.Ctx(r.Context()).Info("log a message with the context") w.Write([]byte("This is a catch-all route")) }) loggedRouter := handlers.LogContextHandler(r) http.ListenAndServe(":1123", loggedRouter) Healthd This provides healthd logging (http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/health-enhanced-serverlogs.html) for health monitoring when using Elastic Beanstalk. |
Package log provides some helpers for structured contextual logging Handle global logging with context.
|
Package log provides some helpers for structured contextual logging Handle global logging with context. |
Package metrics provides a some helpers for sending metrics Statsd Connect to a statsd endpoint using environment variables Environment Variables: STATSD_HOST: The host of the statsd server STATSD_PORT: The port of the statsd server STATSD_NAMESPACE: The namespace to prefix to every metric name STATSD_TAGS: A comma separared list of tags to apply to every metric reported Example: STATSD_HOST: localhost STATSD_PORT: 8125 STATSD_NAMESPACE: app.live.
|
Package metrics provides a some helpers for sending metrics Statsd Connect to a statsd endpoint using environment variables Environment Variables: STATSD_HOST: The host of the statsd server STATSD_PORT: The port of the statsd server STATSD_NAMESPACE: The namespace to prefix to every metric name STATSD_TAGS: A comma separared list of tags to apply to every metric reported Example: STATSD_HOST: localhost STATSD_PORT: 8125 STATSD_NAMESPACE: app.live. |
Package nettest provides a set of network helpers for when unit testing networks Uses a channel log the messages recieved by the server Usage: done := make(chan string) addr, sock, srvWg := CreateServer(t, tc.net, tc.la, done) defer srvWg.Wait() defer os.Remove(addr.String()) defer sock.Close() s, err := net.Dial(tc.net, addr.String()) defer s.Close() fmt.Fprintf(s, "test message\n") if "test message\n" != <-done { t.Error("message not recieved") }
|
Package nettest provides a set of network helpers for when unit testing networks Uses a channel log the messages recieved by the server Usage: done := make(chan string) addr, sock, srvWg := CreateServer(t, tc.net, tc.la, done) defer srvWg.Wait() defer os.Remove(addr.String()) defer sock.Close() s, err := net.Dial(tc.net, addr.String()) defer s.Close() fmt.Fprintf(s, "test message\n") if "test message\n" != <-done { t.Error("message not recieved") } |
Package validate provides a simple interface for validating JSON user input Example: type Item struct { Name string `json:"name"` Description string `json:"description"` } func (i *Item) Validate(ctx context.Context) error { if RuneCountInString(i.Name) == 0 { return fmt.Errorf("the field: name must be provided and not empty") } return nil } func CreateItem(w http.ResponseWriter, r *http.Request) { item := &Item{} if err := validate.JSONRequest(ctx, r, item); err != nil { w.WriteHeader(400) return } }
|
Package validate provides a simple interface for validating JSON user input Example: type Item struct { Name string `json:"name"` Description string `json:"description"` } func (i *Item) Validate(ctx context.Context) error { if RuneCountInString(i.Name) == 0 { return fmt.Errorf("the field: name must be provided and not empty") } return nil } func CreateItem(w http.ResponseWriter, r *http.Request) { item := &Item{} if err := validate.JSONRequest(ctx, r, item); err != nil { w.WriteHeader(400) return } } |
Click to show internal directories.
Click to hide internal directories.