Skip to content

Commit

Permalink
add status (#12) to spans, have nested spans, use fortio with json lo…
Browse files Browse the repository at this point in the history
…gs (#98)

* use prerelease with slog

* fix linter complaints but also fix #12 : set status of span

* indicate where prom metrics are

* use fortio base that starts to use structured logging

* fix missing nesting of spans open-telemetry/opentelemetry-go-contrib#399 open-telemetry/opentelemetry-go-contrib#3046

* fully address open-telemetry/opentelemetry-go-contrib#399 open-telemetry/opentelemetry-go-contrib#3046 - don't have duplicate trace, no need for setting ClientTrace now

* fix header of linter conf

* use release version of fortio and update other deps while at it
  • Loading branch information
ldemailly authored Jun 14, 2023
1 parent f25e490 commit a6586cd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 443 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/gochecks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: go-checks

on:
push:
branches: [main]
pull_request:
# The branches below must be a subset of the branches above
branches: [main]

jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # pin@v3
- name: Setup Go environment
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # pin@v4
with:
go-version: '1.20'
check-latest: true
- name: Run Vulncheck
run: |
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
- name: Run golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # pin@v3
2 changes: 1 addition & 1 deletion .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: docker/setup-qemu-action@2b82ce82d56a2a04d2637cd93a637ae1b359c0a7 # pin@v1
- uses: docker/setup-buildx-action@6a58db7e0d21ca03e6c44877909e80e45217eed2 # pin@v1
- name: Set up Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # pin@v3
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # pin@v4
with:
go-version: '1.20'
check-latest: true
Expand Down
14 changes: 5 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Config for golanglint-ci
# Config for golangci-lint

# output configuration options

Expand Down Expand Up @@ -42,16 +42,9 @@ linters-settings:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Printf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).FErrf
enable-all: true
disable-all: false
depguard:
list-type: blacklist
include-go-root: false
packages:
- github.com/sirupsen/logrus
packages-with-error-message:
# specify an error message to output when a blacklisted package is used
- github.com/sirupsen/logrus: "logging is allowed only by fortio.log"
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
Expand Down Expand Up @@ -81,6 +74,8 @@ linters-settings:

linters:
disable:
# bad ones:
- musttag
# Deprecated ones:
- scopelint
- golint
Expand Down Expand Up @@ -112,6 +107,7 @@ linters:
- cyclop
- forcetypeassert
- ireturn
- depguard
enable-all: true
disable-all: false
# Must not use fast: true in newer golangci-lint or it'll just skip a bunch of linter instead of doing caching like before (!)
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ RUN apk update && apk add ca-certificates
FROM scratch
COPY fortiotel /usr/bin/fortio
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# TODO: Prom metrics exports/scrape
# EXPOSE 9102
# Prom metrics are on the main port (8080) under /debug/metrics
ENV OTEL_SERVICE_NAME "fortio"
# Assumes you added --collector.otlp.enabled=true to your Jaeger deployment
ENV OTEL_EXPORTER_OTLP_ENDPOINT http://jaeger-collector.istio-system.svc.cluster.local:4317
Expand Down
27 changes: 18 additions & 9 deletions fortio_with_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -84,16 +85,23 @@ type OtelLogger struct {
type OtelSpan struct{}

// Before each Run().
func (o *OtelLogger) Start(ctx context.Context, threadID periodic.ThreadID, iter int64, startTime time.Time) context.Context {
func (o *OtelLogger) Start(ctx context.Context, threadID periodic.ThreadID, _ int64, _ time.Time) context.Context {
ctx, span := o.tracer.Start(ctx, fmt.Sprintf("run %d", threadID))
return context.WithValue(ctx, OtelSpan{}, span)
}

// Report logs a single request to a file.
func (o *OtelLogger) Report(ctx context.Context, thread periodic.ThreadID, iter int64,
startTime time.Time, latency float64, status bool, details string,
func (o *OtelLogger) Report(ctx context.Context, _ periodic.ThreadID, _ int64,
_ time.Time, _ float64, status bool, details string,
) {
span := ctx.Value(OtelSpan{}).(trace.Span)
if status {
// Success
span.SetStatus(codes.Ok, details)
} else {
// Error
span.SetStatus(codes.Error, details)
}
span.End()
}

Expand All @@ -102,12 +110,14 @@ func (o *OtelLogger) Info() string {
return "otel"
}

func CreateTrace(ctx context.Context) *httptrace.ClientTrace {
return otelhttptrace.NewClientTrace(ctx)
}

func transportChain(t http.RoundTripper) http.RoundTripper {
return otelhttp.NewTransport(t)
return otelhttp.NewTransport(
t,
otelhttp.WithClientTrace(func(ctx context.Context) *httptrace.ClientTrace {
// So it is nested. replaces ho.ClientTrace = otelhttptrace.NewClientTrace(ctx)
return otelhttptrace.NewClientTrace(ctx)
}),
)
}

func hook(ho *fhttp.HTTPOptions, ro *periodic.RunnerOptions) {
Expand All @@ -116,7 +126,6 @@ func hook(ho *fhttp.HTTPOptions, ro *periodic.RunnerOptions) {
tracer: otel.Tracer("fortio.org/fortio"),
}
ro.AccessLogger = &o
ho.ClientTrace = CreateTrace
ho.Transport = transportChain
// Registers a tracer Provider globally.
var err error
Expand Down
19 changes: 10 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.18
require (
fortio.org/cli v1.1.0
fortio.org/dflag v1.5.2
fortio.org/fortio v1.54.3
fortio.org/log v1.3.0
fortio.org/fortio v1.55.0
fortio.org/log v1.4.0
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0
go.opentelemetry.io/contrib/propagators/b3 v1.17.0
Expand All @@ -21,7 +21,7 @@ require (
// replace fortio.org/fortio => ../fortio

require (
fortio.org/scli v1.4.0 // indirect
fortio.org/scli v1.5.0 // indirect
fortio.org/sets v1.0.3 // indirect
fortio.org/version v1.0.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
Expand All @@ -31,15 +31,16 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.opentelemetry.io/proto/otlp v0.20.0 // indirect
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
Loading

0 comments on commit a6586cd

Please sign in to comment.