|
1 | | -# Monitoring Library Configuration Guide |
| 1 | +## Table of Contents |
| 2 | +1. [Overview](#overview) |
| 3 | +2. [Configuration](#configuration) |
| 4 | + - [Readme API Key](#readme-api-key) |
| 5 | + - [User data configuration ](#userdata-configuration) |
| 6 | + - [Custom user data collection config](#customizing-user-data-collection) |
| 7 | + |
| 8 | +--- |
2 | 9 |
|
3 | 10 | ## Overview |
4 | 11 |
|
5 | | -This spring-boot-starter provides possibility to integrate Readme Metrics SDK to a Spring Boot application easily. |
6 | | -It provides a convenient way to extract user-specific information (e.g., api-key, email, label) from |
7 | | -incoming HTTP requests. It allows configuring multiple extraction methods, such as HTTP headers, JWT claims, or JSON body fields. |
| 12 | +This library provides an easy way to integrate the ReadMe Metrics into a Spring Boot application, |
| 13 | +enabling comprehensive monitoring and logging capabilities. |
| 14 | +The SDK is designed to collect detailed information from HTTP requests and responses, as well as user-specific data, |
| 15 | +for better observability and insights into application behavior. |
| 16 | + |
| 17 | +### Key Features: |
| 18 | +1. **Request and Response Data Logging**: |
| 19 | + - Collects HTTP request and response details, including headers, body content, and HTTP status codes. |
| 20 | + - Ensures minimal impact on the application's core business logic by leveraging efficient wrappers for request and response processing. |
| 21 | + |
| 22 | +2. **User Data Extraction**: |
| 23 | + - Logs information about the user making the request, such as `api-key`, `email`, and `label`. |
| 24 | + - Supports multiple configurable data extraction methods: |
| 25 | + - **HTTP headers** |
| 26 | + - **JWT claims** |
| 27 | + - **JSON body fields** |
| 28 | + |
| 29 | +--- |
8 | 30 |
|
9 | 31 | ## Configuration |
10 | 32 |
|
11 | | -Add the following properties to your `application.yaml` or `application.properties` file. |
12 | | -Each field (`apiKey`, `email`, `label`) requires two sub-properties: |
13 | | -- `source`: Defines where to extract the data from. |
| 33 | +To configure the library, you need to define two main aspects: |
| 34 | +1. The `ReadMe API Key`, which is required to send logged data to the ReadMe platform. |
| 35 | +2. The `UserData` fields (`apiKey`, `email`, `label`), which define where to extract user-specific information from incoming requests. |
| 36 | + |
| 37 | +### ReadMe API Key configuration |
| 38 | + |
| 39 | +The `ReadMe API Key` is a unique identifier that you receive from the ReadMe platform. It is used to authenticate and authorize data sent to the ReadMe metrics endpoint. |
| 40 | +You can configure the `ReadMe API Key` in your `application.yaml` or `application.properties` file using environment variables for security. |
| 41 | + |
| 42 | +**application.yaml:** |
| 43 | +```yaml |
| 44 | +readme: |
| 45 | + readmeApiKey: ${README_API_KEY} |
| 46 | +``` |
| 47 | +**application.properties:** |
| 48 | +```properties |
| 49 | +readme.readmeApiKey=${README_API_KEY} |
| 50 | +``` |
| 51 | + |
| 52 | +### UserData configuration |
| 53 | + |
| 54 | +The library allows you to extract user-specific data (`apiKey`, `email`, `label`) from incoming HTTP requests. Each field requires two properties: |
| 55 | +- **`source`**: Specifies where to extract the data from. |
14 | 56 | - Possible values: |
15 | | - - `header`: Extracts data from an HTTP header. |
16 | | - - `jwtClaim`: Extracts data from a JWT token claim. |
17 | | - - `jsonBody`: Extracts data from the JSON body of a request. |
18 | | -- `fieldName`: Specifies the name or key associated with the source. |
| 57 | + - `header`: Extracts data from an HTTP header. |
| 58 | + - `jwtClaim`: Extracts data from a JWT token claim. |
| 59 | + - `jsonBody`: Extracts data from the JSON body of a request. |
| 60 | +- **`fieldName`**: The key or field name corresponding to the specified source. |
19 | 61 |
|
20 | | -### Example Configuration (YAML) |
| 62 | + |
| 63 | +**application.yaml:** |
21 | 64 | ```yaml |
22 | 65 | readme: |
23 | | - readmeApiKey: ${readmeApiKey} |
24 | 66 | userdata: |
25 | 67 | apiKey: |
26 | 68 | source: header |
27 | 69 | fieldName: X-User-Id |
28 | 70 | email: |
29 | | - source: jwt |
| 71 | + source: jwtClaim |
30 | 72 | fieldName: aud |
31 | 73 | label: |
32 | 74 | source: jsonBody |
33 | | - fieldName: user.name |
| 75 | + fieldName: user/name |
34 | 76 | ``` |
35 | 77 |
|
36 | | -### Example Configuration (PROPERTIES) |
| 78 | +**application.properties:** |
37 | 79 | ```properties |
38 | 80 | readme.userdata.apikey.source=header |
39 | 81 | readme.userdata.apikey.fieldname=X-User-Id |
40 | 82 |
|
41 | | -readme.userdata.email.source=jwtClaim |
| 83 | +readme.userdata.email.source=jwt |
42 | 84 | readme.userdata.email.fieldname=aud |
43 | 85 |
|
44 | 86 | readme.userdata.label.source=jsonBody |
45 | | -readme.userdata.label.fieldname=user.name |
46 | | -``` |
| 87 | +readme.userdata.label.fieldname=user/name |
| 88 | +``` |
| 89 | + |
| 90 | +### Customizing user data collection |
| 91 | + |
| 92 | +The library provides a default implementation of `UserDataCollector`, which extracts user data based on the configuration |
| 93 | +in your YAML or properties file. However, some use cases may require custom logic to extract user-specific information. |
| 94 | +For example: |
| 95 | +- The user data comes from a unique header format. |
| 96 | +- Complex logic is needed to determine user-specific fields. |
| 97 | +- Multiple fields need to be combined dynamically. |
| 98 | + |
| 99 | +In such cases, you can configure the library with a custom way of extracting user data information |
| 100 | +by creating your own implementation of `UserDataCollector`. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +#### How to Create a Custom UserDataCollector |
| 105 | + |
| 106 | +To create a custom `UserDataCollector`, define a Spring bean for your implementation. |
| 107 | +The library's configuration will automatically use your custom implementation if it is present in the application context. |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +#### Example: Custom Implementation |
| 112 | + |
| 113 | +Below is an example of a custom `UserDataCollector` that extracts the `apiKey` from an HTTP header and assigns static |
| 114 | +values for `email` and `label`. |
| 115 | + |
| 116 | +```java |
| 117 | +@Configuration |
| 118 | +public class CustomUserDataCollectorConfig { |
| 119 | + |
| 120 | + @Bean |
| 121 | + public UserDataCollector<ServletDataPayloadAdapter> customUserDataCollector() { |
| 122 | + return payloadAdapter -> { |
| 123 | + // Extract the apiKey from the request headers |
| 124 | + String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name"); |
| 125 | + |
| 126 | + // Build the UserData object |
| 127 | + return UserData.builder() |
| 128 | + .apiKey(apiKey) |
| 129 | + |
| 130 | + .label("owl-label") |
| 131 | + .build(); |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
0 commit comments