Skip to content

Commit 8d99cc3

Browse files
authored
Merge pull request #77 from sc-source/main
update fluent-plugin-oceanbase-logs
2 parents b0644c5 + cbaca51 commit 8d99cc3

File tree

4 files changed

+128
-3
lines changed

4 files changed

+128
-3
lines changed

fluent-plugin-oceanbase-logs/README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Every record includes **`ob_log_type`** (`slow_sql` or `top_sql`). With `include
1313

1414
| Gem | Fluentd | Ruby |
1515
| --- | --- | --- |
16-
| >= 0.1.2 | >= 1.8.0 | >= 2.4 |
16+
| >= 0.1.5 | >= 1.8.0 | >= 2.4 |
1717

1818
For **Grafana Loki** output you additionally need [fluent-plugin-grafana-loki](https://github.com/grafana/fluent-plugin-grafana-loki).
1919

@@ -44,6 +44,42 @@ tenant_id "#{ENV['OCEANBASE_TENANT_ID']}"
4444

4545
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.
4646

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
56+
<source>
57+
@type oceanbase_logs
58+
tag oceanbase.slow_sql
59+
log_type slow_sql
60+
access_key_id "#{ENV['OCEANBASE_ACCESS_KEY_ID']}"
61+
access_key_secret "#{ENV['OCEANBASE_ACCESS_KEY_SECRET']}"
62+
endpoint "#{ENV['OCEANBASE_ACCESS_KEY_SECRET']}"
63+
fetch_interval 60
64+
lookback_seconds 600
65+
deduplicate true
66+
include_metadata true
67+
<target>
68+
instance_id "OCEANBASE_INSTANCE_1"
69+
tenant_id "OCEANBASE_TENANT_A"
70+
</target>
71+
<target>
72+
instance_id "OCEANBASE_INSTANCE_2"
73+
tenant_id "OCEANBASE_TENANT_B"
74+
</target>
75+
<storage>
76+
@type local
77+
persistent true
78+
path /var/log/fluentd/slow_sql_seen
79+
</storage>
80+
</source>
81+
```
82+
4783
### Example: Slow SQL → JSON file
4884

4985
Full sample: [`example/fluentd.conf`](example/fluentd.conf).

fluent-plugin-oceanbase-logs/example/oceanbase2loki-docker/docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ services:
2626

2727
fluentd-oceanbase-demo:
2828
image: fluent/fluentd:v1.16-1
29+
platform: linux/amd64
2930
container_name: fluentd-oceanbase-demo
3031
user: root
3132
command:
@@ -46,7 +47,7 @@ services:
4647
OCEANBASE_ENDPOINT: "${OCEANBASE_ENDPOINT:-api-cloud-cn.oceanbase.com}"
4748
OCEANBASE_FETCH_INTERVAL: "${OCEANBASE_FETCH_INTERVAL:-60}"
4849
OCEANBASE_LOOKBACK_SECONDS: "${OCEANBASE_LOOKBACK_SECONDS:-600}"
49-
OCEANBASE_DB_NAME: "${OCEANBASE_DB_NAME:-test}"
50+
OCEANBASE_DB_NAME: "${OCEANBASE_DB_NAME:-}"
5051
OCEANBASE_SEARCH_KEYWORD: "${OCEANBASE_SEARCH_KEYWORD:-}"
5152
OCEANBASE_PROJECT_ID: "${OCEANBASE_PROJECT_ID:-}"
5253
networks:

fluent-plugin-oceanbase-logs/fluent-plugin-oceanbase-logs.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
1010
spec.summary = "Fluentd input plugin for OceanBase Cloud Logs"
1111
spec.description = "Fetches Slow SQL and Top SQL per-execution samples from OceanBase Cloud " \
1212
"and emits them as Fluentd events (one record per trace, dedup by traceId)."
13-
spec.homepage = "https://github.com/sc-source/fluent-plugin-oceanbase-logs"
13+
spec.homepage = "https://github.com/oceanbase/ecology-plugins/tree/main/fluent-plugin-oceanbase-logs"
1414
spec.license = "Apache-2.0"
1515

1616
spec.files = Dir['lib/**/*', 'README.md', 'LICENSE']

fluent-plugin-oceanbase-logs/test/plugin/test_in_oceanbase_logs.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,50 @@ def stub_samples_auth(samples_response, times: 2)
174174
assert_raise(Fluent::ConfigError) { create_driver(conf) }
175175
end
176176

177+
test 'multiple targets: top-level instance_id and tenant_id may be empty' do
178+
conf = %(
179+
tag oceanbase.logs
180+
access_key_id test_access_key_id
181+
access_key_secret test_access_key_secret
182+
fetch_interval 60
183+
lookback_seconds 300
184+
deduplicate false
185+
<target>
186+
instance_id ob_a
187+
tenant_id t_a
188+
</target>
189+
<target>
190+
instance_id ob_b
191+
tenant_id t_b
192+
db_name analytics_db
193+
</target>
194+
)
195+
d = create_driver(conf)
196+
assert_equal 2, d.instance.instance_variable_get(:@fetch_scopes).size
197+
scopes = d.instance.instance_variable_get(:@fetch_scopes)
198+
assert_equal 'ob_a', scopes[0].instance_id
199+
assert_equal 't_a', scopes[0].tenant_id
200+
assert_nil scopes[0].db_name
201+
assert_equal 'ob_b', scopes[1].instance_id
202+
assert_equal 'analytics_db', scopes[1].db_name
203+
end
204+
205+
test 'target section missing tenant_id raises error' do
206+
conf = %(
207+
tag oceanbase.logs
208+
access_key_id test_access_key_id
209+
access_key_secret test_access_key_secret
210+
fetch_interval 60
211+
lookback_seconds 300
212+
deduplicate false
213+
<target>
214+
instance_id ob_a
215+
tenant_id ""
216+
</target>
217+
)
218+
assert_raise(Fluent::ConfigError) { create_driver(conf) }
219+
end
220+
177221
end
178222

179223
# ----------------------------------------------------------- fetching
@@ -267,6 +311,50 @@ def stub_samples_auth(samples_response, times: 2)
267311
d.run(timeout: 3)
268312
assert_equal 0, d.events.length
269313
end
314+
315+
test 'slow_sql with two targets calls both APIs and emits scoped metadata' do
316+
list_one = {
317+
'success' => true,
318+
'data' => { 'dataList' => [{ 'sqlId' => 'SQL_MULTI_A' }], 'total' => 1 },
319+
}
320+
list_two = {
321+
'success' => true,
322+
'data' => { 'dataList' => [{ 'sqlId' => 'SQL_MULTI_B' }], 'total' => 1 },
323+
}
324+
stub_digest_auth(
325+
%r{instances/ob_a/tenants/t_a/slowSql},
326+
list_one
327+
)
328+
stub_digest_auth(
329+
%r{instances/ob_b/tenants/t_b/slowSql},
330+
list_two
331+
)
332+
stub_samples_auth(SAMPLES_RESPONSE, times: 2)
333+
334+
conf = %(
335+
tag oceanbase.logs
336+
access_key_id test_access_key_id
337+
access_key_secret test_access_key_secret
338+
fetch_interval 60
339+
lookback_seconds 300
340+
deduplicate false
341+
<target>
342+
instance_id ob_a
343+
tenant_id t_a
344+
</target>
345+
<target>
346+
instance_id ob_b
347+
tenant_id t_b
348+
</target>
349+
)
350+
d = create_driver(conf)
351+
d.run(timeout: 3)
352+
353+
events = d.events
354+
assert events.length >= 2, "Expected at least 2 events, got #{events.length}"
355+
insts = events.map { |e| e[2]['ob_instance_id'] }.uniq.sort
356+
assert_equal %w[ob_a ob_b], insts
357+
end
270358
end
271359

272360
# ---------------------------------------------------------- deduplication

0 commit comments

Comments
 (0)