Debian Codegraph @

instrumentation/net/http/otelhttp/internal/request/resp_writer_wrapper.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/request/resp_writer_wrapper.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package request // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/request"
8
9 import (
10 "net/http"
11 "sync"
12 )
13
14 var _ http.ResponseWriter = &RespWriterWrapper{}
15
16 // RespWriterWrapper wraps a http.ResponseWriter in order to track the number of
17 // bytes written, the last error, and to catch the first written statusCode.
18 // TODO: The wrapped http.ResponseWriter doesn't implement any of the optional
19 // types (http.Hijacker, http.Pusher, http.CloseNotifier, etc)
20 // that may be useful when using it in real life situations.
21 type RespWriterWrapper struct {
22 http.ResponseWriter
23 OnWrite func(n int64) // must not be nil
24
25 mu sync.RWMutex
26 written int64
27 statusCode int
28 err error
29 wroteHeader bool
30 }
31
32 // NewRespWriterWrapper creates a new RespWriterWrapper.
33 //
34 // The onWrite attribute is a callback that will be called every time the data
35 // is written, with the number of bytes that were written.
36 func NewRespWriterWrapper(w http.ResponseWriter, onWrite func(int64)) *RespWriterWrapper {
37 return &RespWriterWrapper{
38 ResponseWriter: w,
39 OnWrite: onWrite,
40 statusCode: http.StatusOK, // default status code in case the Handler doesn't write anything
41 }
42 }
43
44 // Write writes the bytes array into the [ResponseWriter], and tracks the
45 // number of bytes written and last error.
46 func (w *RespWriterWrapper) Write(p []byte) (int, error) {
47 w.mu.Lock()
48 defer w.mu.Unlock()
49
50 if !w.wroteHeader {
51 w.writeHeader(http.StatusOK)
52 }
53
54 n, err := w.ResponseWriter.Write(p)
55 n1 := int64(n)
56 w.OnWrite(n1)
57 w.written += n1
58 w.err = err
59 return n, err
60 }
61
62 // WriteHeader persists initial statusCode for span attribution.
63 // All calls to WriteHeader will be propagated to the underlying ResponseWriter
64 // and will persist the statusCode from the first call (except for informational response status codes).
65 // Blocking consecutive calls to WriteHeader alters expected behavior and will
66 // remove warning logs from net/http where developers will notice incorrect handler implementations.
67 func (w *RespWriterWrapper) WriteHeader(statusCode int) {
68 w.mu.Lock()
69 defer w.mu.Unlock()
70
71 w.writeHeader(statusCode)
72 }
73
74 // writeHeader persists the status code for span attribution, and propagates
75 // the call to the underlying ResponseWriter.
76 // It does not acquire a lock, and therefore assumes that is being handled by a
77 // parent method.
78 func (w *RespWriterWrapper) writeHeader(statusCode int) {
79 if !w.wroteHeader {
80 // Ignore informational response status codes.
81 // Based on https://github.com/golang/go/blob/go1.24.1/src/net/http/server.go#L1216
82 if statusCode >= 100 && statusCode <= 199 && statusCode != http.StatusSwitchingProtocols {
83 w.ResponseWriter.WriteHeader(statusCode)
84 return
85 }
86
87 w.wroteHeader = true
88 w.statusCode = statusCode
89 }
90 w.ResponseWriter.WriteHeader(statusCode)
91 }
92
93 // Flush implements [http.Flusher].
94 func (w *RespWriterWrapper) Flush() {
95 w.mu.Lock()
96 defer w.mu.Unlock()
97
98 if !w.wroteHeader {
99 w.writeHeader(http.StatusOK)
100 }
101
102 if f, ok := w.ResponseWriter.(http.Flusher); ok {
103 f.Flush()
104 }
105 }
106
107 // BytesWritten returns the number of bytes written.
108 func (w *RespWriterWrapper) BytesWritten() int64 {
109 w.mu.RLock()
110 defer w.mu.RUnlock()
111
112 return w.written
113 }
114
115 // StatusCode returns the HTTP status code that was sent.
116 func (w *RespWriterWrapper) StatusCode() int {
117 w.mu.RLock()
118 defer w.mu.RUnlock()
119
120 return w.statusCode
121 }
122
123 // Error returns the last error.
124 func (w *RespWriterWrapper) Error() error {
125 w.mu.RLock()
126 defer w.mu.RUnlock()
127
128 return w.err
129 }