Skip to content
master
Go to file
Code

README.md

Mortar

Go codecov PkgGoDev Go Report Card

Mortar is a GO framework/library for building gRPC (and REST) web services. Mortar has out-of-the-box support for configuration, application metrics, logging, tracing, profiling, dependency injection and more. While it comes with predefined defaults Mortar gives you total control to fully customize it.

Demo

Clone this demo repository and play around.

When you done, read the documentation or create your own service with some templates found here.

Features

Telemetry (Everything connected)

  • Logs have Tracing Information traceId=6ff7e7e38d1e86f across services logs

  • Also visible in Jaeger traceId=6ff7e7e38d1e86f if it's sampled. jaeger

Support for *http.Client Interceptors, so you can

  • Add request and response info to Trace traceId=6ff7e7e38d1e86f

    jaeger_http

  • Log/Dump requests and/or responses when http request fails.

    return func(req *http.Request, handler client.HTTPHandler) (resp *http.Response, err error) {
        var reqBytes, respBytes []byte
        // If the response is Bad Request, log both Request and Response
        reqBytes, _ = httputil.DumpRequestOut(req, true) // it can be nil and it's ok
        if resp, err = handler(req); err == nil && resp.StatusCode >= http.StatusBadRequest {
            respBytes, _ = httputil.DumpResponse(resp, true) // it can be nil
            logger.WithError(fmt.Errorf("http request failed")).
            WithField("status",resp.StatusCode).
            Warn(req.Context(), "\nRequest:\n%s\n\nResponse:\n%s\n", reqBytes, respBytes)
        }
        return
    }

    http_client

  • Alter requests and/or responses (useful in Tests)

    func(*http.Request, clientInt.HTTPHandler) (*http.Response, error) {
        // special case, don't go anywhere just return the response
        return &http.Response{
            Status:        "200 OK",
            StatusCode:    200,
            Proto:         "HTTP/1.1",
            ProtoMajor:    1,
            ProtoMinor:    1,
            ContentLength: 11,
            Body:          ioutil.NopCloser(strings.NewReader("car painted")),
        }, nil
    }

Monitoring/Metrics support

Export to either Prometheus/Datadog/statsd/etc, it's your choice. Mortar only provides the Interface and also caches the metrics so you don't have to.

counter := w.deps.Metrics.WithTags(monitor.Tags{
 "color":   request.GetDesiredColor(),
 "success": fmt.Sprintf("%t", err == nil),
}).Counter("paint_desired_color", "New paint color for car")

counter.Inc()

counter is actually a singleton, uniqueness calculated here

grafana

Additional Features

  • /debug/pprof and other useful handlers
  • Use config_test.yml during tests to override values in config.yml, it saves time.

There are some features not listed here, please check the Tutorial for more.

If you want to skip reading just jump to this part in the Tutorial.

Tutorial

Mortar is not a drop-in replacement.

It's important to read its Documentation first, starting with the step by step Tutorial which is also a bit-of-everything example.

Scaffolds

To help you bootstrap your services with Mortar here you can find different templates.