fluent-plugin-oceanbase-logs 0.1.1 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de2cfb03d01cbc70a6ad726b2f7e6da5aef25497662a52bff813b641ac36f069
4
- data.tar.gz: b1187d6647a6391749566d16995ede8abaf4017a78550831976a9a372a52f7a5
3
+ metadata.gz: 8b21a8c2fad6676c5f9b5b8f00cd849091b8c4077c745b2fa7a462995f9b7b07
4
+ data.tar.gz: 0f1a70d45968dc28815fb3c507e85b7115cc6b3682f0a5a762a0bdc5467f9019
5
5
  SHA512:
6
- metadata.gz: 70275780b1a4aa356e0a64aa0bb5f0c0ec5a335c8844027ae1485b646c343dafcc8f5e020adda59304df65c21f5404d1c425fd0a1c1cb1ac6f98ae0079a3b092
7
- data.tar.gz: 82256ca93641fb8ca2b5b0497c459640b480473569d7c87b986dcbebabb82f61300f202387bf5eeaf79366582ae86cad7a1c0b659d215e7870a019be6bf781ab
6
+ metadata.gz: 02b0aaf81f118afdcc215f377f7c13ca2ca63d2a1cd28008f778b902e9277b614f28464973fbec4d5ce4a7550fbc3eca9a9f43833367379f58a7f967398f1a05
7
+ data.tar.gz: 2dfdd6d0bbe2ac20532ef99e1ef1e764d7d8fa9244043865370e9b1c77378766503ae19680f50266085e39a0ad8af0784ee00eeb19f904e4ee6523de902692f8
data/LICENSE CHANGED
@@ -1,21 +1,201 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 OceanBase Integrations
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,17 +1,21 @@
1
1
  # fluent-plugin-oceanbase-logs
2
2
 
3
- Fluentd input plugin that periodically fetches SQL diagnostics data from [OceanBase Cloud](https://www.oceanbase.com/). Each event is one SQL execution (sample), with deduplication by `traceId`.
3
+ Fluentd **input** plugin: periodically pulls SQL diagnostics from [OceanBase Cloud](https://www.oceanbase.com/) . Each event is **one execution sample**
4
4
 
5
- | `log_type` | API Path | Description |
5
+ | `log_type` | API | Meaning |
6
6
  | --- | --- | --- |
7
- | `slow_sql` (default) | `/api/v2/.../slowSql` + samples | Slow SQL (per-execution) |
8
- | `top_sql` | `/api/v2/.../topSql` + samples | Top SQL (per-execution) |
7
+ | `slow_sql` (default) | `…/slowSql` + per-`sqlId` samples | Slow SQL |
8
+ | `top_sql` | `…/topSql` + samples | Top SQL |
9
+
10
+ Every record includes **`ob_log_type`** (`slow_sql` or `top_sql`). With `include_metadata true` (default), records also get `ob_instance_id`, `ob_tenant_id`, and the query time window.
9
11
 
10
12
  ## Requirements
11
13
 
12
- | fluent-plugin-oceanbase-logs | fluentd | ruby |
13
- | ---------------------------- | --------- | ------ |
14
- | >= 0.1.0 | >= 1.8.0 | >= 2.4 |
14
+ | Gem | Fluentd | Ruby |
15
+ | --- | --- | --- |
16
+ | >= 0.1.2 | >= 1.8.0 | >= 2.4 |
17
+
18
+ For **Grafana Loki** output you additionally need [fluent-plugin-grafana-loki](https://github.com/grafana/fluent-plugin-grafana-loki).
15
19
 
16
20
  ## Installation
17
21
 
@@ -21,32 +25,84 @@ gem install fluent-plugin-oceanbase-logs
21
25
 
22
26
  ## Preparation
23
27
 
24
- 1. Create an AccessKey pair at [OceanBase Cloud AccessKey Management](https://console-cn.oceanbase.com/account/accessKey)
25
- 2. Find your **Instance ID** and **Tenant ID** from the OceanBase Cloud console
28
+ 1. Create an AccessKey in [OceanBase Cloud AccessKey](https://console-cn.oceanbase.com/account/accessKey).
29
+ 2. In the console, copy **Instance ID** and **Tenant ID**
30
+
26
31
 
27
32
  ## Configuration
28
33
 
29
- ### Slow SQL → file (one record per execution)
34
+ ### Environment variables
35
+
36
+ Typical pattern in Fluentd:
37
+
38
+ ```text
39
+ access_key_id "#{ENV['OCEANBASE_ACCESS_KEY_ID']}"
40
+ access_key_secret "#{ENV['OCEANBASE_ACCESS_KEY_SECRET']}"
41
+ instance_id "#{ENV['OCEANBASE_INSTANCE_ID']}"
42
+ tenant_id "#{ENV['OCEANBASE_TENANT_ID']}"
43
+ ```
30
44
 
31
- ```xml
45
+ Optional: `OCEANBASE_ENDPOINT`, `OCEANBASE_FETCH_INTERVAL`, `OCEANBASE_LOOKBACK_SECONDS`, `OCEANBASE_DB_NAME`, `OCEANBASE_SEARCH_KEYWORD`, `OCEANBASE_PROJECT_ID` — see `.env.example` and the Docker table below.
46
+
47
+ ### Multiple instances, tenants, and databases
48
+
49
+ Use one or more `<target>` blocks. Each block sets **`instance_id`** and **`tenant_id`** (required). Optional **`db_name`** applies the same `dbName` filter as the top-level parameter, for that scope only.
50
+
51
+ When any `<target>` is present, the plugin **only** uses those scopes; top-level `instance_id` / `tenant_id` / `db_name` are ignored (you may leave them empty). Global options such as `access_key_id`, `log_type`, `search_keyword`, `endpoint`, and `fetch_interval` still apply to every target.
52
+
53
+ Example: two clusters, one tenant each, and a second scope that filters one database on another tenant:
54
+
55
+ ```conf
32
56
  <source>
33
57
  @type oceanbase_logs
34
58
  tag oceanbase.slow_sql
35
59
  log_type slow_sql
60
+ access_key_id "#{ENV['OCEANBASE_ACCESS_KEY_ID']}"
61
+ access_key_secret "#{ENV['OCEANBASE_ACCESS_KEY_SECRET']}"
62
+ instance_id ""
63
+ tenant_id ""
64
+ endpoint api-cloud-cn.oceanbase.com
65
+ fetch_interval 60
66
+ lookback_seconds 600
67
+ deduplicate true
68
+ include_metadata true
69
+ <target>
70
+ instance_id "#{ENV['OCEANBASE_INSTANCE_1']}"
71
+ tenant_id "#{ENV['OCEANBASE_TENANT_A']}"
72
+ </target>
73
+ <target>
74
+ instance_id "#{ENV['OCEANBASE_INSTANCE_2']}"
75
+ tenant_id "#{ENV['OCEANBASE_TENANT_B']}"
76
+ db_name "#{ENV['OCEANBASE_DB_ANALYTICS']}"
77
+ </target>
78
+ <storage>
79
+ @type local
80
+ persistent true
81
+ path /var/log/fluentd/slow_sql_seen
82
+ </storage>
83
+ </source>
84
+ ```
85
+
86
+ The same tenant with multiple databases needs one `<target>` per database (or a target without `db_name` to pull all DBs for that tenant, if your API usage allows omitting `dbName`).
36
87
 
88
+ ### Example: Slow SQL → JSON file
89
+
90
+ Full sample: [`example/fluentd.conf`](example/fluentd.conf).
91
+
92
+ ```conf
93
+ <source>
94
+ @type oceanbase_logs
95
+ tag oceanbase.slow_sql
96
+ log_type slow_sql
37
97
  access_key_id "#{ENV['OCEANBASE_ACCESS_KEY_ID']}"
38
98
  access_key_secret "#{ENV['OCEANBASE_ACCESS_KEY_SECRET']}"
39
99
  instance_id "#{ENV['OCEANBASE_INSTANCE_ID']}"
40
100
  tenant_id "#{ENV['OCEANBASE_TENANT_ID']}"
41
-
42
- # 服务接入点(API 地址)
43
101
  endpoint api-cloud-cn.oceanbase.com
44
- # 采集间隔(秒)与采集时间范围(秒)
45
102
  fetch_interval 60
46
103
  lookback_seconds 600
47
-
48
104
  deduplicate true
49
-
105
+ include_metadata true
50
106
  <storage>
51
107
  @type local
52
108
  persistent true
@@ -61,105 +117,32 @@ gem install fluent-plugin-oceanbase-logs
61
117
  <format>
62
118
  @type json
63
119
  </format>
64
- <buffer>
65
- @type file
66
- path /var/log/fluentd/buffer/slow_sql
67
- flush_mode interval
68
- flush_interval 5s
69
- </buffer>
70
120
  </match>
71
121
  ```
72
122
 
73
- ### Parameters
74
-
75
- #### Core
76
-
77
- | Parameter | Type | Required | Default | Description |
78
- | --- | --- | --- | --- | --- |
79
- | `log_type` | enum | no | `slow_sql` | `slow_sql` or `top_sql` |
80
- | `tag` | string | **yes** | — | Fluentd tag for emitted events |
81
-
82
- #### Authentication
83
-
84
- | Parameter | Type | Required | Default | Description |
85
- | --- | --- | --- | --- | --- |
86
- | `access_key_id` | string | **yes** | — | OceanBase Cloud AccessKey ID |
87
- | `access_key_secret` | string | **yes** | — | OceanBase Cloud AccessKey Secret |
88
-
89
- #### Cluster
90
-
91
- | Parameter | Type | Required | Default | Description |
92
- | --- | --- | --- | --- | --- |
93
- | `instance_id` | string | **yes** | — | OceanBase cluster instance ID |
94
- | `tenant_id` | string | **yes** | — | OceanBase tenant ID |
95
- | `project_id` | string | no | nil | Project ID (`X-Ob-Project-Id` header) |
123
+ ### Example: Loki + Docker Compose
96
124
 
97
- #### Filters
98
-
99
- | Parameter | Type | Required | Default | Description |
100
- | --- | --- | --- | --- | --- |
101
- | `db_name` | string | no | nil | Filter by database name |
102
- | `search_keyword` | string | no | nil | Search keyword in SQL text |
103
- | `node_ip` | string | no | nil | Database node IP |
104
- | `filter_condition` | string | no | nil | Advanced filter (e.g. `@avgCpuTime > 20`) |
105
- | `sql_text_length` | integer | no | 65535 | Max SQL text length returned |
106
-
107
- #### 服务接入点与采集时间(均在配置文件中配置)
108
-
109
- | Parameter | Type | Required | Default | Description |
110
- | --- | --- | --- | --- | --- |
111
- | `endpoint` | string | no | `api-cloud-cn.oceanbase.com` | 服务接入点(API 地址;国际站可用 `api-cloud.oceanbase.com`) |
112
- | `fetch_interval` | time | no | 300 (5 min) | 采集间隔(秒):每隔多久请求一次 API |
113
- | `lookback_seconds` | integer | no | 600 (10 min) | 采集时间范围(秒):每次请求查询最近多长时间的数据 |
114
-
115
- 可通过环境变量覆盖:`OCEANBASE_ENDPOINT`、`OCEANBASE_FETCH_INTERVAL`、`OCEANBASE_LOOKBACK_SECONDS`(参见 `example/` 与 `.env.example`)。
116
-
117
- #### Behaviour
118
-
119
- | Parameter | Type | Required | Default | Description |
120
- | --- | --- | --- | --- | --- |
121
- | `deduplicate` | bool | no | true | Skip already-seen records (by traceId) |
122
- | `include_metadata` | bool | no | true | Attach `ob_instance_id` / `ob_tenant_id` / `ob_log_type` to records |
123
- | `ssl_verify_peer` | bool | no | true | Verify SSL certificates |
124
- | `http_proxy` | string | no | nil | HTTP proxy URL |
125
-
126
- (`endpoint` 见上表「服务接入点与采集时间」。)
127
-
128
- ## Output record fields
129
-
130
- Each record is one SQL execution (sample). Main fields:
131
-
132
- | Field | Type | Description |
133
- | --- | --- | --- |
134
- | `sqlId` | string | SQL identifier |
135
- | `fullSqlText` | string | Complete SQL text |
136
- | `sqlTextShort` | string | SQL text (truncated) |
137
- | `sqlType` | string | `SELECT`, `UPDATE`, etc. |
138
- | `dbName` | string | Database name |
139
- | `userName` | string | User |
140
- | `requestTime` | string | Execution timestamp (UTC) |
141
- | `traceId` | string | Unique trace ID (used for dedup) |
142
- | `elapsedTime` | double | Response time (ms) |
143
- | `cpuTime` | double | CPU time (ms) |
144
- | `executeTime` | double | Plan execution time (ms) |
145
- | `returnRows` | long | Returned rows |
146
- | `affectedRows` | long | Affected rows |
147
-
148
- ## Examples
149
-
150
- See the `example/` directory:
151
-
152
- - `fluentd.conf` — Slow SQL to file
153
- - `fluentd_to_file.conf` — Slow SQL + Top SQL to file
154
- - `fluentd_to_loki.conf` — Slow SQL + Top SQL to Grafana Loki
155
-
156
- ## Test
125
+ Ready-made stack (Loki, Fluentd, Grafana) and a **slow_sql + top_sql**:
157
126
 
158
127
  ```bash
159
- bundle install
160
- bundle exec rake test
128
+ cp .env.example .env # fill in secrets
129
+ cd example/oceanbase2loki-docker && docker compose up -d
161
130
  ```
162
131
 
163
- ## License
132
+ **Compose-related environment** (host `.env` or exports):
164
133
 
165
- MIT
134
+ | Variable | Required | Default |
135
+ | --- | --- | --- |
136
+ | `LOKI_URL` | no | `http://loki:3100` |
137
+ | `OCEANBASE_ACCESS_KEY_ID` | **yes** | — |
138
+ | `OCEANBASE_ACCESS_KEY_SECRET` | **yes** | — |
139
+ | `OCEANBASE_INSTANCE_ID` | **yes** | — |
140
+ | `OCEANBASE_TENANT_ID` | **yes** | — |
141
+ | `OCEANBASE_ENDPOINT` | no | `api-cloud-cn.oceanbase.com` |
142
+ | `OCEANBASE_FETCH_INTERVAL` | no | `60` |
143
+ | `OCEANBASE_LOOKBACK_SECONDS` | no | `600` |
144
+ | `OCEANBASE_DB_NAME` | no | `test` |
145
+ | `OCEANBASE_SEARCH_KEYWORD` | no | `SELECT` |
146
+ | `OCEANBASE_PROJECT_ID` | no | *(unset)* |
147
+
148
+ > The bundled `fluentd-to-loki.conf` uses `record.dig` and avoids `";` / `]` patterns that break Fluentd’s config parser and `record_transformer` — keep that style if you edit it.
@@ -13,6 +13,8 @@ module Fluent::Plugin
13
13
 
14
14
  helpers :thread, :storage
15
15
 
16
+ FetchScope = Struct.new(:instance_id, :tenant_id, :db_name)
17
+
16
18
  DEFAULT_STORAGE_TYPE = 'local'
17
19
 
18
20
  LOG_TYPE_PATHS = {
@@ -28,10 +30,19 @@ module Fluent::Plugin
28
30
  config_param :access_key_secret, :string, secret: true,
29
31
  desc: "OceanBase Cloud AccessKey Secret."
30
32
 
31
- config_param :instance_id, :string,
32
- desc: "OceanBase cluster instance ID."
33
- config_param :tenant_id, :string,
34
- desc: "OceanBase tenant ID."
33
+ config_param :instance_id, :string, default: '',
34
+ desc: "OceanBase cluster instance ID (legacy single scope; ignored when <target> sections are present)."
35
+ config_param :tenant_id, :string, default: '',
36
+ desc: "OceanBase tenant ID (legacy single scope; ignored when <target> sections are present)."
37
+
38
+ config_section :target, param_name: :targets, multi: true, required: false do
39
+ config_param :instance_id, :string,
40
+ desc: "Instance ID for this scope."
41
+ config_param :tenant_id, :string,
42
+ desc: "Tenant ID for this scope."
43
+ config_param :db_name, :string, default: nil,
44
+ desc: "Optional database name filter for this scope."
45
+ end
35
46
  config_param :project_id, :string, default: nil,
36
47
  desc: "OceanBase Cloud project ID (X-Ob-Project-Id header)."
37
48
 
@@ -74,6 +85,22 @@ module Fluent::Plugin
74
85
  super
75
86
  @endpoint = @endpoint.to_s.strip
76
87
  @endpoint = 'api-cloud-cn.oceanbase.com' if @endpoint.empty?
88
+
89
+ %i[@access_key_id @access_key_secret @instance_id @tenant_id].each do |iv|
90
+ v = instance_variable_get(iv)
91
+ next unless v.is_a?(String)
92
+ instance_variable_set(iv, v.strip)
93
+ end
94
+ raise Fluent::ConfigError, 'access_key_id is required and cannot be empty' if @access_key_id.empty?
95
+ raise Fluent::ConfigError, 'access_key_secret is required and cannot be empty' if @access_key_secret.empty?
96
+
97
+ %i[@db_name @search_keyword @node_ip @filter_condition @project_id].each do |iv|
98
+ v = instance_variable_get(iv)
99
+ instance_variable_set(iv, nil) if v.is_a?(String) && v.strip.empty?
100
+ end
101
+
102
+ @fetch_scopes = build_fetch_scopes
103
+
77
104
  @api_path_segment = LOG_TYPE_PATHS[@log_type.to_s]
78
105
  if @deduplicate
79
106
  @seen_storage = storage_create(
@@ -97,6 +124,36 @@ module Fluent::Plugin
97
124
 
98
125
  private
99
126
 
127
+ def build_fetch_scopes
128
+ scopes = []
129
+ if @targets && !@targets.empty?
130
+ @targets.each_with_index do |t, idx|
131
+ iid = t.instance_id.to_s.strip
132
+ tid = t.tenant_id.to_s.strip
133
+ raise Fluent::ConfigError, "target[#{idx}]: instance_id is required and cannot be empty" if iid.empty?
134
+ raise Fluent::ConfigError, "target[#{idx}]: tenant_id is required and cannot be empty" if tid.empty?
135
+ db = normalize_optional_db_name(t.db_name)
136
+ scopes << FetchScope.new(iid, tid, db)
137
+ end
138
+ else
139
+ raise Fluent::ConfigError, 'instance_id is required and cannot be empty (e.g. set OCEANBASE_INSTANCE_ID), or define one or more <target> sections' if @instance_id.empty?
140
+ raise Fluent::ConfigError, 'tenant_id is required and cannot be empty (e.g. set OCEANBASE_TENANT_ID), or define one or more <target> sections' if @tenant_id.empty?
141
+ scopes << FetchScope.new(@instance_id, @tenant_id, @db_name)
142
+ end
143
+ scopes
144
+ end
145
+
146
+ def normalize_optional_db_name(value)
147
+ return nil if value.nil?
148
+ s = value.to_s.strip
149
+ s.empty? ? nil : s
150
+ end
151
+
152
+ def dedup_storage_key(scope, dedup_id)
153
+ db = scope.db_name.to_s
154
+ "trace:#{scope.instance_id}:#{scope.tenant_id}:#{db}:#{dedup_id}"
155
+ end
156
+
100
157
  def run
101
158
  until @finished
102
159
  begin
@@ -121,24 +178,26 @@ module Fluent::Plugin
121
178
  now = Time.now.utc
122
179
  start_time = (now - @lookback_seconds).strftime('%Y-%m-%dT%H:%M:%SZ')
123
180
  end_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
124
- fetch_and_emit_samples(start_time, end_time)
181
+ @fetch_scopes.each do |scope|
182
+ fetch_and_emit_samples(start_time, end_time, scope)
183
+ end
125
184
  end
126
185
 
127
186
  # Fetch list then per-execution samples (one record per trace)
128
- def fetch_and_emit_samples(start_time, end_time)
129
- list_response = call_list_api(start_time, end_time)
187
+ def fetch_and_emit_samples(start_time, end_time, scope)
188
+ list_response = call_list_api(start_time, end_time, scope)
130
189
  return unless list_response
131
190
 
132
191
  sql_records = extract_records(list_response)
133
192
  return if sql_records.nil? || sql_records.empty?
134
193
 
135
194
  sql_ids = sql_records.map { |r| r['sqlId'] }.compact.uniq
136
- log.debug "Found #{sql_ids.size} unique SQL IDs, fetching samples..."
195
+ log.debug "Found #{sql_ids.size} unique SQL IDs for instance=#{scope.instance_id} tenant=#{scope.tenant_id} db=#{scope.db_name.inspect}, fetching samples..."
137
196
 
138
197
  total_emitted = 0
139
198
 
140
199
  sql_ids.each do |sql_id|
141
- samples = fetch_samples_for_sql(sql_id, start_time, end_time)
200
+ samples = fetch_samples_for_sql(sql_id, start_time, end_time, scope)
142
201
  next if samples.nil? || samples.empty?
143
202
 
144
203
  es = Fluent::MultiEventStream.new
@@ -148,12 +207,13 @@ module Fluent::Plugin
148
207
  dedup_id = trace_id || "#{sql_id}_#{sample['requestTime']}"
149
208
 
150
209
  if @deduplicate
151
- dedup_key = :"trace_#{dedup_id}"
210
+ dedup_key = dedup_storage_key(scope, dedup_id)
152
211
  next if @seen_storage.get(dedup_key)
153
212
  @seen_storage.put(dedup_key, Time.now.to_i.to_s)
154
213
  end
155
214
 
156
- sample = attach_metadata(sample, start_time, end_time) if @include_metadata
215
+ sample['ob_log_type'] = @log_type.to_s
216
+ sample = attach_metadata(sample, start_time, end_time, scope) if @include_metadata
157
217
 
158
218
  event_time = if sample['requestTime']
159
219
  begin
@@ -174,50 +234,58 @@ module Fluent::Plugin
174
234
  end
175
235
  end
176
236
 
177
- log.info "Emitted #{total_emitted} #{@log_type} sample events (#{start_time} ~ #{end_time})" if total_emitted > 0
237
+ log.info "Emitted #{total_emitted} #{@log_type} sample events for instance=#{scope.instance_id} tenant=#{scope.tenant_id} (#{start_time} ~ #{end_time})" if total_emitted > 0
178
238
  end
179
239
 
180
- def fetch_samples_for_sql(sql_id, start_time, end_time)
181
- path = "/api/v2/instances/#{@instance_id}/tenants/#{@tenant_id}/sqls/#{sql_id}/samples"
240
+ def fetch_samples_for_sql(sql_id, start_time, end_time, scope)
241
+ path = "/api/v2/instances/#{scope.instance_id}/tenants/#{scope.tenant_id}/sqls/#{sql_id}/samples"
182
242
  params = {
183
243
  'startTime' => start_time,
184
244
  'endTime' => end_time,
185
245
  }
186
- params['dbName'] = @db_name if @db_name
246
+ params['dbName'] = scope.db_name if scope.db_name
187
247
 
188
248
  response = call_api_raw(path, params)
189
249
  return nil unless response
190
250
  extract_records(response)
191
251
  end
192
252
 
193
- def attach_metadata(record, start_time, end_time)
253
+ def attach_metadata(record, start_time, end_time, scope)
194
254
  record.merge(
195
- 'ob_instance_id' => @instance_id,
196
- 'ob_tenant_id' => @tenant_id,
197
- 'ob_log_type' => @log_type.to_s,
255
+ 'ob_instance_id' => scope.instance_id,
256
+ 'ob_tenant_id' => scope.tenant_id,
198
257
  'query_start_time' => start_time,
199
258
  'query_end_time' => end_time
200
259
  )
201
260
  end
202
261
 
203
262
  def extract_records(response)
204
- if response['data'].is_a?(Hash) && response['data']['dataList'].is_a?(Array)
205
- response['data']['dataList']
206
- else
207
- log.warn "Unexpected API response structure", keys: response.keys
208
- nil
263
+ data = response['data']
264
+ return [] if data.nil?
265
+
266
+ if data.is_a?(Hash)
267
+ list = data['dataList']
268
+ return list if list.is_a?(Array)
269
+ return [] if list.nil? || list == []
270
+ elsif data.is_a?(Array)
271
+ return data
209
272
  end
273
+
274
+ log.warn "Unexpected API response structure",
275
+ keys: response.keys, data_class: data.class.name,
276
+ data_keys: (data.respond_to?(:keys) ? data.keys : nil)
277
+ []
210
278
  end
211
279
 
212
280
  # ---- API calls ----
213
281
 
214
- def call_list_api(start_time, end_time)
215
- path = "/api/v2/instances/#{@instance_id}/tenants/#{@tenant_id}/#{@api_path_segment}"
282
+ def call_list_api(start_time, end_time, scope)
283
+ path = "/api/v2/instances/#{scope.instance_id}/tenants/#{scope.tenant_id}/#{@api_path_segment}"
216
284
  params = {
217
285
  'startTime' => start_time,
218
286
  'endTime' => end_time,
219
287
  }
220
- params['dbName'] = @db_name if @db_name
288
+ params['dbName'] = scope.db_name if scope.db_name
221
289
  params['searchKeyWord'] = @search_keyword if @search_keyword
222
290
  params['nodeIp'] = @node_ip if @node_ip
223
291
  params['filterCondition'] = @filter_condition if @filter_condition
@@ -238,7 +306,16 @@ module Fluent::Plugin
238
306
  end
239
307
 
240
308
  unless resp && resp.code.to_i == 200
241
- log.error "OceanBase API HTTP #{resp&.code}", body: resp&.body, path: path
309
+ code = resp&.code
310
+ raw = resp&.body.to_s
311
+ detail = nil
312
+ begin
313
+ j = JSON.parse(raw)
314
+ detail = j['message'] || j['errorMessage'] || j['msg']
315
+ rescue JSON::ParserError
316
+ end
317
+ log.error "OceanBase API HTTP #{code}",
318
+ message: detail, body: (raw.bytesize > 512 ? raw.byteslice(0, 512) + '...' : raw), path: path
242
319
  return nil
243
320
  end
244
321
 
@@ -2,7 +2,7 @@ module Fluent
2
2
  module Plugin
3
3
  module OceanBase
4
4
  module Logs
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.4"
6
6
  end
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-oceanbase-logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - OceanBase Integrations
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-03-09 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: fluentd
@@ -66,20 +65,6 @@ dependencies:
66
65
  - - "~>"
67
66
  - !ruby/object:Gem::Version
68
67
  version: '3.0'
69
- - !ruby/object:Gem::Dependency
70
- name: test-unit-rr
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
68
  - !ruby/object:Gem::Dependency
84
69
  name: webmock
85
70
  requirement: !ruby/object:Gem::Requirement
@@ -106,11 +91,10 @@ files:
106
91
  - README.md
107
92
  - lib/fluent/plugin/in_oceanbase_logs.rb
108
93
  - lib/fluent/plugin/oceanbase/logs/version.rb
109
- homepage: https://github.com/your-org/fluent-plugin-oceanbase-logs
94
+ homepage: https://github.com/sc-source/fluent-plugin-oceanbase-logs
110
95
  licenses:
111
- - MIT
96
+ - Apache-2.0
112
97
  metadata: {}
113
- post_install_message:
114
98
  rdoc_options: []
115
99
  require_paths:
116
100
  - lib
@@ -125,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
109
  - !ruby/object:Gem::Version
126
110
  version: '0'
127
111
  requirements: []
128
- rubygems_version: 3.0.3.1
129
- signing_key:
112
+ rubygems_version: 3.6.9
130
113
  specification_version: 4
131
114
  summary: Fluentd input plugin for OceanBase Cloud Logs
132
115
  test_files: []