or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cache-monitoring.mddatabase-monitoring.mdhttp-monitoring.mdindex.mdmetrics-export.mdmigration-monitoring.mdmodel-monitoring.mdtesting-utilities.md

http-monitoring.mddocs/

0

# HTTP Monitoring

1

2

Comprehensive monitoring of HTTP requests, responses, exceptions, and latencies through Django middleware. The middleware system provides detailed metrics on web application performance and usage patterns.

3

4

## Capabilities

5

6

### Before Middleware

7

8

Monitors early request/response lifecycle events and measures total latency including middleware processing time.

9

10

```python { .api }

11

class PrometheusBeforeMiddleware(MiddlewareMixin):

12

"""

13

Monitoring middleware that should run before other middlewares.

14

Measures total request latency including middleware processing time.

15

"""

16

17

metrics_cls = Metrics

18

19

def __init__(self, *args, **kwargs):

20

"""

21

Initialize middleware with metrics instance.

22

23

Parameters:

24

- *args, **kwargs: Arguments passed to MiddlewareMixin

25

"""

26

27

def process_request(self, request):

28

"""

29

Records request start time and increments request counter.

30

31

Parameters:

32

- request: Django HttpRequest object

33

"""

34

35

def process_response(self, request, response):

36

"""

37

Records response and measures total latency including middlewares.

38

39

Parameters:

40

- request: Django HttpRequest object

41

- response: Django HttpResponse object

42

43

Returns:

44

HttpResponse object (passed through)

45

"""

46

```

47

48

### After Middleware

49

50

Provides detailed monitoring of request characteristics, view processing, template usage, response details, and exception handling.

51

52

```python { .api }

53

class PrometheusAfterMiddleware(MiddlewareMixin):

54

"""

55

Monitoring middleware that should run after other middlewares.

56

Provides detailed metrics on request/response characteristics.

57

"""

58

59

metrics_cls = Metrics

60

61

def __init__(self, *args, **kwargs):

62

"""

63

Initialize middleware with metrics instance.

64

65

Parameters:

66

- *args, **kwargs: Arguments passed to MiddlewareMixin

67

"""

68

69

def process_request(self, request):

70

"""

71

Records request method, transport, AJAX status, and body size.

72

73

Parameters:

74

- request: Django HttpRequest object

75

"""

76

77

def process_view(self, request, view_func, *view_args, **view_kwargs):

78

"""

79

Records view-specific metrics including view name and routing info.

80

81

Parameters:

82

- request: Django HttpRequest object

83

- view_func: View function being called

84

- view_args: Positional arguments to view

85

- view_kwargs: Keyword arguments to view

86

"""

87

88

def process_template_response(self, request, response):

89

"""

90

Records template name usage for template-based responses.

91

92

Parameters:

93

- request: Django HttpRequest object

94

- response: Django TemplateResponse object

95

96

Returns:

97

TemplateResponse object (passed through)

98

"""

99

100

def process_response(self, request, response):

101

"""

102

Records response status, charset, streaming status, body size, and view latency.

103

104

Parameters:

105

- request: Django HttpRequest object

106

- response: Django HttpResponse object

107

108

Returns:

109

HttpResponse object (passed through)

110

"""

111

112

def process_exception(self, request, exception):

113

"""

114

Records exception metrics by type and view when exceptions occur.

115

116

Parameters:

117

- request: Django HttpRequest object

118

- exception: Exception instance that was raised

119

"""

120

121

def label_metric(self, metric, request, response=None, **labels):

122

"""

123

Helper method to apply labels to metrics.

124

125

Parameters:

126

- metric: Prometheus metric object

127

- request: Django HttpRequest object

128

- response: Optional Django HttpResponse object

129

- **labels: Label key-value pairs

130

131

Returns:

132

Labeled metric object

133

"""

134

```

135

136

### Metrics Class

137

138

Singleton class managing all HTTP monitoring metrics with proper metric registration and initialization.

139

140

```python { .api }

141

class Metrics:

142

"""

143

Singleton class that registers and manages all HTTP monitoring metrics.

144

"""

145

146

@classmethod

147

def get_instance(cls):

148

"""

149

Returns singleton instance of Metrics class.

150

151

Returns:

152

Metrics instance

153

"""

154

155

def register_metric(self, metric_cls, name: str, documentation: str, labelnames=(), **kwargs):

156

"""

157

Registers a Prometheus metric with proper naming and labeling.

158

159

Parameters:

160

- metric_cls: Prometheus metric class (Counter, Histogram, etc.)

161

- name: str, metric name

162

- documentation: str, metric documentation

163

- labelnames: tuple, label names for the metric

164

- **kwargs: Additional metric configuration

165

166

Returns:

167

Registered metric instance

168

"""

169

170

def register(self):

171

"""Registers all HTTP monitoring metrics."""

172

```

173

174

## Monitored Metrics

175

176

### Request Metrics

177

- `django_http_requests_before_middlewares_total`: Total requests before middleware processing

178

- `django_http_requests_total_by_method`: Requests by HTTP method (GET, POST, etc.)

179

- `django_http_requests_total_by_transport`: Requests by transport (http, https)

180

- `django_http_requests_total_by_view_transport_method`: Requests by view, transport, and method

181

- `django_http_ajax_requests_total`: AJAX request count

182

- `django_http_requests_body_total_bytes`: Request body size histogram

183

184

### Response Metrics

185

- `django_http_responses_before_middlewares_total`: Total responses before middleware processing

186

- `django_http_responses_total_by_status`: Responses by HTTP status code

187

- `django_http_responses_total_by_status_view_method`: Responses by status, view, and method

188

- `django_http_responses_total_by_charset`: Responses by character encoding

189

- `django_http_responses_total_by_templatename`: Responses by template name

190

- `django_http_responses_streaming_total`: Streaming response count

191

- `django_http_responses_body_total_bytes`: Response body size histogram

192

193

### Latency Metrics

194

- `django_http_requests_latency_including_middlewares_seconds`: Request latency including middleware time

195

- `django_http_requests_latency_seconds_by_view_method`: Request latency by view and method

196

- `django_http_requests_unknown_latency_including_middlewares_total`: Requests with unknown middleware latency

197

- `django_http_requests_unknown_latency_total`: Requests with unknown processing latency

198

199

### Exception Metrics

200

- `django_http_exceptions_total_by_type`: Exceptions by exception class name

201

- `django_http_exceptions_total_by_view`: Exceptions by view name

202

203

## Usage Example

204

205

```python

206

# settings.py

207

MIDDLEWARE = [

208

'django_prometheus.middleware.PrometheusBeforeMiddleware',

209

'django.middleware.security.SecurityMiddleware',

210

'django.contrib.sessions.middleware.SessionMiddleware',

211

# ... other middleware

212

'django_prometheus.middleware.PrometheusAfterMiddleware',

213

]

214

215

# Optional configuration

216

PROMETHEUS_METRIC_NAMESPACE = 'myapp'

217

PROMETHEUS_LATENCY_BUCKETS = (0.1, 0.5, 1.0, 2.5, 5.0, 10.0, float('inf'))

218

```