Skip to content

Commit a98d1db

Browse files
authored
Improve docs (#8)
1 parent b45ae8f commit a98d1db

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[![Sourcegraph](https://sourcegraph.com/github.com/labstack/echo-opentelemetry/-/badge.svg?style=flat-square)](https://sourcegraph.com/github.com/labstack/echo-opentelemetry?badge)
22
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/labstack/echo-opentelemetry)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/labstack/echo-opentelemetry?style=flat-square)](https://goreportcard.com/report/github.com/labstack/echo-opentelemetry)
4-
[![Codecov](https://img.shields.io/codecov/c/github/labstack/echo-opentelemetry.svg?style=flat-square)](https://codecov.io/gh/labstack/echo-opentelemetry)
54
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/labstack/echo-opentelemetry/main/LICENSE)
65

76
# Echo OpenTelemetry (OTel) middleware

extrator.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,36 @@ import (
1212

1313
"go.opentelemetry.io/otel/attribute"
1414
"go.opentelemetry.io/otel/metric"
15+
)
16+
17+
import (
1518
semconv "go.opentelemetry.io/otel/semconv/v1.39.0"
1619
"go.opentelemetry.io/otel/semconv/v1.39.0/httpconv"
1720
)
1821

22+
/*
23+
Working principles for tracing/metering HTTP requests/responses with extractor.
24+
25+
1. Create a Values struct per request served to hold extracted values from Request and Response `v := Values{}`.
26+
1.1. If your middleware needs to support default/custom values (server name/port, client addr etc) this is good place
27+
to set them.
28+
2. Extract request values from the HTTP request to Values struct `err := v.ExtractRequest(request)`. These extracted values
29+
are used to start then span and when metrics are recorded at the end of the request (response side)
30+
3. Create the new span.
31+
3.1. Attributes for span can be acquired from extracted values with `attr := v.SpanStartAttributes()`
32+
3.2. If your middleware needs to support additional start attributes, add them to attributes from previous step.
33+
4. Execute next handler in chain.
34+
5. Determine if the request / response ended with an error
35+
6. Extract response values from HTTP response to Values struct
36+
6.1. Determine the response status that was sent to the client `v.HTTPResponseStatusCode = ?`
37+
6.2. Extract response body size `v.HTTPResponseBodySize = ?`
38+
7. Attributes from response can be acquired from extracted values with `attr := v.SpanEndAttributes()`
39+
7.1. If your middleware needs to support additional end attributes, add them to attributes.
40+
8. Create a RecordValues struct for metrics recording `iv := RecordValues{...}`.
41+
9. Record metrics with `m.Record(ctx, iv)`
42+
10. End the span with `span.End(ctx)`
43+
*/
44+
1945
// Metrics holds a standard set of OpenTelemetry global request/response metrics
2046
type Metrics struct {
2147
// requestDurationHistogram (`http.server.request.duration`) is the duration of HTTP server requests.

otel.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ type AttributesFunc func(c *echo.Context, v *Values, attr []attribute.KeyValue)
8989
// MetricAttributesFunc is used to compose attributes for Metrics.Record.
9090
type MetricAttributesFunc func(c *echo.Context, v *Values) []attribute.KeyValue
9191

92-
// MetricsRecorder is used to record metrics.
92+
// MetricsRecorder is used to record metrics. This interface is used to allow custom metrics recording with access to
93+
// the Echo context, so additional attributes can be extracted from it.
9394
type MetricsRecorder interface {
9495
Record(c *echo.Context, v RecordValues)
9596
}
@@ -239,6 +240,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
239240
RequestDuration: time.Since(requestStartTime),
240241
ExtractedValues: ev,
241242
Attributes: nil,
243+
// when Attributes are left nil, they are extracted with `iv.ExtractedValues.MetricAttributes()` inside `metrics.Record()` call
242244
}
243245
if config.MetricAttributes != nil {
244246
iv.Attributes = config.MetricAttributes(c, &ev)
@@ -250,6 +252,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
250252
}, nil
251253
}
252254

255+
// echoMetricsRecorder is the default implementation for Echo metric recording interface
253256
type echoMetricsRecorder struct {
254257
*Metrics
255258
}

0 commit comments

Comments
 (0)