Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Codecov + doc improvements #1

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Go

on:
push:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ jobs:

- name: Testing
run: make test

7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
PACKAGES := $(shell find . -name "*.go" | grep -v -e vendor -e bindata | xargs -n1 dirname | sort -u)
TEST_FLAGS := -race -count=1

default: test

.PHONY: lint
lint:
golangci-lint run $(PACKAGES)
golangci-lint run ./...

.PHONY: test
test:
go test $(TEST_FLAGS) $(PACKAGES)
go test -race -count=1 ./...

.PHONY: bench
bench:
Expand Down
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@

Go package that provides an implementation of `log/slog`'s [Handler interface](https://pkg.go.dev/log/slog#Handler) that ensures a strong correlation between log records and [Open-Telemetry spans](https://opentelemetry.io/docs/concepts/signals/traces/#spans) by...

1. Adding span and trace IDs to the log record.
2. Adding context baggage members to the log record.
3. Adding log record as span event.
1. Adding [span and trace IDs](https://opentelemetry.io/docs/concepts/signals/traces/#span-context) to the log record.
2. Adding context [baggage](https://opentelemetry.io/docs/concepts/signals/baggage/) members to the log record.
3. Adding log record as [span event](https://opentelemetry.io/docs/concepts/signals/traces/#span-events).
4. Adding log record attributes to the span event.
5. Setting span status based on slog record level (only if >= slog.LevelError).
5. Setting [span status](https://opentelemetry.io/docs/concepts/signals/traces/#span-status) based on slog record level (only if >= slog.LevelError).

## Usage
```go
import (
"context"
"log/slog"

"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
slogotel "github.com/remychantenay/slog-otel"
)

// 1. Configure slog.
slog.SetDefault(slog.New(slogotel.OtelHandler{
Next: slog.NewJSONHandler(os.Stdout, nil),
Expand All @@ -23,11 +33,17 @@ slog.SetDefault(slog.New(slogotel.OtelHandler{
logger := slog.Default()
logger = logger.With("component", "server")

// 3. Start your span.
// 3. (Optional) Add baggage to your context.
m1, _ := baggage.NewMember("key_1", "value_1")
m2, _ := baggage.NewMember("key_2", "value_2")
bag, _ := baggage.New(m1, m2)
ctx := baggage.ContextWithBaggage(context.Background(), bag)

// 4. Start your span.
ctx, span := tracer.Start(ctx, "operation-name")
defer span.End()

// 4. Log
// 5. Log.
logger.InfoContext(ctx, "Hello world!", "locale", "en_US")
```

Expand Down
21 changes: 21 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@ Package slogotel provides a custom handler for `log/slog` to ensures strong corr

# Usage

import (
"context"
"log/slog"

"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
slogotel "github.com/remychantenay/slog-otel"
)

// 1. Configure slog.
slog.SetDefault(slog.New(slogotel.OtelHandler{
Next: slog.NewJSONHandler(os.Stdout, nil),
}))

// 2. Set up your logger.
logger := slog.Default()
logger = logger.With("component", "server")

// 3. (Optional) Add baggage to your context.
m1, _ := baggage.NewMember("key_1", "value_1")
m2, _ := baggage.NewMember("key_2", "value_2")
bag, _ := baggage.New(m1, m2)
ctx := baggage.ContextWithBaggage(context.Background(), bag)

// 4. Start your span.
tracer := sdktrace.NewTracerProvider().Tracer("server")
ctx, span := tracer.Start(ctx, "operation-name")
defer span.End()

// 5. Log.
logger.InfoContext(ctx, "Hello world!", "locale", "en_US")
*/
package slogotel
Loading