Skip to content

an opinionated thin-wrapper over the OpenTelemetry SDK

License

Notifications You must be signed in to change notification settings

wwmoraes/gotell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gotell

Golang OTEL library, an opinionated thin-wrapper over the OpenTelemetry SDK

Go Reference GitHub Issues GitHub Pull Requests Codecov

GitHub branch status License


📝 Table of Contents

🧐 About

Gotell is an opinionated thin wrapper on top of the OpenTelemetry Go SDK. It applies semantic convention metrics and attributes for you so you don't have to.

The name gotell comes from go + otel (OpenTelemetry) + library. 😉

🏁 Getting Started

Run go get github.com/wwmoraes/gotell or add the repository to your imports then run go mod tidy.

🔧 Running the tests

Run task test 😄

🎈 Usage

package main

import (
  "context"
  "errors"
  "fmt"
  "net"
  "net/http"
  "os"
  "os/signal"
  "time"

  telemetry "github.com/wwmoraes/gotell"
  "go.opentelemetry.io/otel/attribute"
  "go.opentelemetry.io/otel/sdk/resource"
)

const (
  NAMESPACE = "github.com/wwmoraes/gotell"
  NAME      = "http-server"
)

var version = "0.0.0"

func main() {
  ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
  defer cancel()

  hostname, err := os.Hostname()
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  err = telemetry.Initialize(ctx, resource.NewSchemaless(
    attribute.String("service.name", NAME),
    attribute.String("service.namespace", NAMESPACE),
    attribute.String("service.version", version),
    attribute.String("host.id", hostname),
  ))
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  log := telemetry.Logr(ctx)

  mux := http.NewServeMux()

  mux.Handle("/", telemetry.WithInstrumentationMiddleware(http.HandlerFunc(helloHandler)))

  listener, err := net.Listen("tcp4", "127.0.0.1:0")
  if !errors.Is(err, http.ErrServerClosed) {
    log.Error(err, "failed to create listener")
    os.Exit(1)
  }

  server := http.Server{
    Handler:           mux,
    ReadTimeout:       time.Minute / 2,
    ReadHeaderTimeout: time.Minute / 2,
  }

  log.Info("serving HTTP", "address", listener.Addr().String())

  err = server.Serve(listener)
  if !errors.Is(err, http.ErrServerClosed) {
    log.Error(err, "server stopped")
  }
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
  ctx, span := telemetry.Start(r.Context())
  defer span.End()

  log := telemetry.Logr(ctx)

  log.Info("hello handler triggered")

  name := r.URL.Query().Get("name")
  if name == "" {
    name = "stranger"
  }

  span.SetAttributes(attribute.String("name", name))

  fmt.Fprintf(w, "hello %s!", name)
}

🔧 Built Using

🧑‍💻 Authors

🎉 Acknowledgements

About

an opinionated thin-wrapper over the OpenTelemetry SDK

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published