komuw / otero

MIT License
11 stars 3 forks source link

integrate metrics with logs #5

Open komuw opened 1 year ago

komuw commented 1 year ago

We could take the metric tags/labels/attributes and add them to logs and/or traces; https://github.com/komuw/otero/blob/d9e1e7755fde6d56049c15624221e1a1630b27b7/service.go#L84-L88

komuw commented 1 year ago
diff --git a/service.go b/service.go
index 43ad22e..79481d8 100644
--- a/service.go
+++ b/service.go
@@ -7,6 +7,7 @@ import (
    "net/http"

    "github.com/komuw/otero/log"
+   "github.com/sirupsen/logrus"

    "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
    "go.opentelemetry.io/otel"
@@ -78,19 +79,25 @@ func serviceA_HttpHandler(w http.ResponseWriter, r *http.Request) {
        "service_a_called_counter",
        instrument.WithDescription("how many time the serviceA handler has been called."),
    )
+   attrs := []attribute.KeyValue{
+       attribute.String("handler_name", "serviceA_HttpHandler"),
+       attribute.Int64("req_size", r.ContentLength),
+   }
    counter.Add(
        ctx,
        1,
        // labels/tags
-       []attribute.KeyValue{
-           attribute.String("handler_name", "serviceA_HttpHandler"),
-           attribute.Int64("req_size", r.ContentLength),
-       }...,
+       attrs...,
    )

-   log := log.NewLogrus(ctx)
+   log := log.NewLogrus(ctx).WithFields(logrus.Fields{
+       "handler_name": attrs[0].Value,
+       "req_size":     attrs[1].Value,
+   })
    log.Info("serviceA_HttpHandler called")

+   span.SetAttributes(attrs...)
+
    // When serviceA is called, it calls serviceB over tcp network.