You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/12/11 13:10:30 UTC
[skywalking] branch master updated: Support monitoring mariadb (#10115)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 3229a5c5ec Support monitoring mariadb (#10115)
3229a5c5ec is described below
commit 3229a5c5ec289cf9b9cc3d57eb3df61bc160c2c4
Author: Mahmoud Anwer <ma...@gmail.com>
AuthorDate: Sun Dec 11 15:10:23 2022 +0200
Support monitoring mariadb (#10115)
Co-authored-by: yswdqz <94...@qq.com>
---
.github/workflows/skywalking.yaml | 2 +
.gitignore | 2 +-
docs/en/changes/changes.md | 5 +
docs/en/setup/backend/backend-mysql-monitoring.md | 26 +--
docs/menu.yml | 2 +-
.../ui-initialized-templates/mysql/mysql-root.json | 2 +-
.../e2e-v2/cases/mariadb/expected/db-has-value.yml | 29 ++++
.../cases/mariadb/expected/metrics-has-value.yml | 19 +++
test/e2e-v2/cases/mariadb/expected/service.yml | 24 +++
test/e2e-v2/cases/mariadb/mariadb-cases.yaml | 83 ++++++++++
.../mariadb/mariadb-slowsql/docker-compose.yaml | 121 ++++++++++++++
test/e2e-v2/cases/mariadb/mariadb-slowsql/e2e.yaml | 38 +++++
.../mariadb/mariadb-slowsql/fluent-bit-parser.conf | 25 +++
.../mariadb/mariadb-slowsql/fluent-bit-script.lua | 139 ++++++++++++++++
.../cases/mariadb/mariadb-slowsql/fluent-bit.conf | 52 ++++++
test/e2e-v2/cases/mariadb/mariadb-slowsql/mock.sql | 42 +++++
test/e2e-v2/cases/mariadb/mariadb-slowsql/my.cnf | 37 +++++
.../otel-collector-config.yaml | 54 ++++++
.../mysql/mysql-slowsql/fluent-bit-script.lua | 181 ++++++++++++---------
19 files changed, 787 insertions(+), 96 deletions(-)
diff --git a/.github/workflows/skywalking.yaml b/.github/workflows/skywalking.yaml
index cc3075fff2..f75306d42f 100644
--- a/.github/workflows/skywalking.yaml
+++ b/.github/workflows/skywalking.yaml
@@ -601,6 +601,8 @@ jobs:
config: test/e2e-v2/cases/mysql/mysql-slowsql/e2e.yaml
- name: PostgreSQL Prometheus
config: test/e2e-v2/cases/postgresql/postgres-exporter/e2e.yaml
+ - name: MariaDB Prometheus and slowsql
+ config: test/e2e-v2/cases/mariadb/mariadb-slowsql/e2e.yaml
- name: Rover with Istio Process 1.13.1
config: test/e2e-v2/cases/rover/process/istio/e2e.yaml
diff --git a/.gitignore b/.gitignore
index 492dd61b87..cc26b4f2fd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,4 +21,4 @@ oap-server/oal-grammar/**/gen/
# This serves as a template but will ONLY be updated when building a source release tar,
# so we don't track future updates of this file.
-oap-server/server-starter/src/main/resources/version.properties
+oap-server/server-starter/src/main/resources/version.properties
\ No newline at end of file
diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index d4641ac348..b86459f811 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -35,11 +35,16 @@
* Change `elasticsearch/flushInterval` to 5s(was 15s).
* Optimize `flushInterval` of ElasticSearch BulkProcessor to avoid extra periodical flush in the continuous bulk streams.
+#### OAP-Backend
+
+* Support monitoring MariaDB.
+
#### UI
#### Documentation
* Remove Spring Sleuth docs, and add `Spring MicroMeter Observations Analysis` with the latest Java agent side
enhancement.
+* Update `monitoring MySQL document` to add the `MariaDB` part.
All issues and pull requests are [here](https://github.com/apache/skywalking/milestone/160?closed=1)
diff --git a/docs/en/setup/backend/backend-mysql-monitoring.md b/docs/en/setup/backend/backend-mysql-monitoring.md
index cfb4ffaea3..9cd1724794 100644
--- a/docs/en/setup/backend/backend-mysql-monitoring.md
+++ b/docs/en/setup/backend/backend-mysql-monitoring.md
@@ -1,10 +1,10 @@
-# MySQL monitoring
-## MySQL server performance from `prometheus/mysqld_exporter`
+# MySQL/MariaDB monitoring
+## MySQL/MariaDB server performance from `prometheus/mysqld_exporter`
SkyWalking leverages prometheus/mysqld_exporter for collecting metrics data. It leverages OpenTelemetry Collector to transfer the metrics to
[OpenTelemetry receiver](opentelemetry-receiver.md) and into the [Meter System](./../../concepts-and-designs/meter.md).
### Data flow
-1. mysqld_exporter collect metrics data from MySQL.
+1. mysqld_exporter collect metrics data from MySQL/MariaDB.
2. OpenTelemetry Collector fetches metrics from mysqld_exporter via Prometheus Receiver and pushes metrics to SkyWalking OAP Server via the OpenCensus gRPC Exporter or OpenTelemetry gRPC exporter.
3. The SkyWalking OAP Server parses the expression with [MAL](../../concepts-and-designs/mal.md) to filter/calculate/aggregate and store the results.
@@ -13,9 +13,9 @@ SkyWalking leverages prometheus/mysqld_exporter for collecting metrics data. It
2. Set up [OpenTelemetry Collector ](https://opentelemetry.io/docs/collector/getting-started/#docker). For details on Prometheus Receiver in OpenTelemetry Collector, refer to [here](../../../../test/e2e-v2/cases/mysql/prometheus-mysql-exporter/otel-collector-config.yaml).
3. Config SkyWalking [OpenTelemetry receiver](opentelemetry-receiver.md).
-### MySQL Monitoring
-MySQL monitoring provides monitoring of the status and resources of the MySQL server. MySQL cluster is cataloged as a `Layer: MYSQL` `Service` in OAP.
-Each MySQL server is cataloged as an `Instance` in OAP.
+### MySQL/MariaDB Monitoring
+MySQL/MariaDB monitoring provides monitoring of the status and resources of the MySQL/MariaDB server. MySQL/MariaDB cluster is cataloged as a `Layer: MYSQL` `Service` in OAP.
+Each MySQL/MariaDB server is cataloged as an `Instance` in OAP.
#### Supported Metrics
| Monitoring Panel | Unit | Metric Name | Description | Data Source |
|-----|------|-----|-----|-----|
@@ -37,27 +37,27 @@ The metrics definition and expression rules are found in `/config/otel-rules/mys
The MySQL dashboard panel configurations are found in `/config/ui-initialized-templates/mysql`.
## Collect sampled slow SQLs
-SkyWalking leverages [fluentbit](https://fluentbit.io/) or other log agents for collecting slow SQL statements from MySQL.
+SkyWalking leverages [fluentbit](https://fluentbit.io/) or other log agents for collecting slow SQL statements from MySQL/MariaDB.
### Data flow
-1. fluentbit agent collects slow sql logs from MySQL.
+1. fluentbit agent collects slow sql logs from MySQL/MariaDB.
2. fluentbit agent sends data to SkyWalking OAP Server using native meter APIs via HTTP.
3. The SkyWalking OAP Server parses the expression with [LAL](../../concepts-and-designs/lal.md) to parse/extract and store the results.
### Set up
1. Set up [fluentbit](https://docs.fluentbit.io/manual/installation/docker).
-2. Config [fluentbit](../../../../test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit.conf)
-3. Config MySQL to enable slow log.[example](../../../../test/e2e-v2/cases/mysql/mysql-slowsql/my.cnf).
+2. Config fluentbit from [here](../../../../test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit.conf) for MySQL or [here](../../../../test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit.conf) for MariaDB.
+3. Enable slow log from [here](../../../../test/e2e-v2/cases/mysql/mysql-slowsql/my.cnf) for MySQL or [here](../../../../test/e2e-v2/cases/mariadb/mariadb-slowsql/my.cnf) for MariaDB.
### Slow SQL Monitoring
-Slow SQL monitoring provides monitoring of the slow SQL statements of the MySQL server. MySQL server is cataloged as a `Layer: MYSQL` `Service` in OAP.
+Slow SQL monitoring provides monitoring of the slow SQL statements of the MySQL/MariaDB server. MySQL/MariaDB server is cataloged as a `Layer: MYSQL` `Service` in OAP.
#### Supported Metrics
| Monitoring Panel | Unit | Metric Name | Description | Data Source |
|-----|------|-----|-----|-----|
-|Slow Statements | ms | top_n_database_statement | The latency and statement of MySQL slow SQLs | fluentbit|
+|Slow Statements | ms | top_n_database_statement | The latency and statement of MySQL/MariaDB slow SQLs | fluentbit|
### Customizations
You can customize your own metrics/expression/dashboard panel.
The slowsql expression rules are found in `/config/lal/mysql-slowsql.yaml`
-The MySQL dashboard panel configurations are found in `/config/ui-initialized-templates/mysql`.
+The MySQL/MariaDB dashboard panel configurations are found in `/config/ui-initialized-templates/mysql`.
diff --git a/docs/menu.yml b/docs/menu.yml
index eda1bf216e..021eead392 100644
--- a/docs/menu.yml
+++ b/docs/menu.yml
@@ -187,7 +187,7 @@ catalog:
path: "/en/setup/backend/backend-apisix-monitoring"
- name: "Database Monitoring"
catalog:
- - name: "MySQL Server"
+ - name: "MySQL/MariaDB Server"
path: "/en/setup/backend/backend-mysql-monitoring"
- name: "PostgreSQL Server"
path: "/en/setup/backend/backend-postgresql-monitoring"
diff --git a/oap-server/server-starter/src/main/resources/ui-initialized-templates/mysql/mysql-root.json b/oap-server/server-starter/src/main/resources/ui-initialized-templates/mysql/mysql-root.json
index 3f353fa64a..b2cf86ab0a 100644
--- a/oap-server/server-starter/src/main/resources/ui-initialized-templates/mysql/mysql-root.json
+++ b/oap-server/server-starter/src/main/resources/ui-initialized-templates/mysql/mysql-root.json
@@ -46,7 +46,7 @@
"graph":{
"fontColor":"blue",
"backgroundColor":"white",
- "content":"Provide MySQL Server monitoring through OpenTelemetry's Prometheus Receiver",
+ "content":"Provide MySQL and MariaDB Server monitoring through OpenTelemetry's Prometheus Receiver",
"fontSize":14,
"textAlign":"left",
"url":"https://skywalking.apache.org/docs/main/next/en/setup/backend/backend-mysql-monitoring/"
diff --git a/test/e2e-v2/cases/mariadb/expected/db-has-value.yml b/test/e2e-v2/cases/mariadb/expected/db-has-value.yml
new file mode 100644
index 0000000000..80e0a6dac9
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/expected/db-has-value.yml
@@ -0,0 +1,29 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+{{- contains . }}
+- key: 0
+ value:
+ {{- contains .value }}
+ - key: name
+ value: {{ notEmpty .value }}
+ - key: value
+ value: {{ notEmpty .value }}
+ - key: id
+ value: ""
+ - key: refid
+ value: ""
+ {{- end }}
+{{- end }}
diff --git a/test/e2e-v2/cases/mariadb/expected/metrics-has-value.yml b/test/e2e-v2/cases/mariadb/expected/metrics-has-value.yml
new file mode 100644
index 0000000000..d9c49854c9
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/expected/metrics-has-value.yml
@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+{{- contains . }}
+- key: {{ notEmpty .key }}
+ value: {{ ge .value 1 }}
+{{- end }}
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/expected/service.yml b/test/e2e-v2/cases/mariadb/expected/service.yml
new file mode 100644
index 0000000000..ec661a68a6
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/expected/service.yml
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+{{- contains . }}
+- id: {{ b64enc "mysql::db" }}.1
+ name: mysql::db
+ shortname: db
+ normal: true
+ group: "mysql"
+ layers:
+ - MYSQL
+{{- end }}
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/mariadb-cases.yaml b/test/e2e-v2/cases/mariadb/mariadb-cases.yaml
new file mode 100644
index 0000000000..a3ba087158
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-cases.yaml
@@ -0,0 +1,83 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file is used to show how to write configuration files and can be used to test.
+
+cases:
+ # service list
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql service ls
+ expected: expected/service.yml
+ # service metrics
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_commands_select_rate --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_commands_insert_rate --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_commands_update_rate --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_commands_delete_rate --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_qps --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_tps --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_threads_connected --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_threads_created --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_threads_running --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_connects_aborted --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_connects_available --service-name=mysql::db |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ # instance metrics
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_uptime --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_innodb_buffer_pool_size --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_max_connections --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_thread_cache_size --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_commands_select_rate --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_commands_insert_rate --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_commands_update_rate --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_commands_delete_rate --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_qps --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_tps --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_threads_connected --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_threads_created --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_threads_running --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_connects_aborted --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql metrics linear --name=meter_mysql_instance_connects_available --service-name=mysql::db --instance-name=mariadb-service:9104 |yq e 'to_entries' -
+ expected: expected/metrics-has-value.yml
+ # slow sql
+ - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql records list --name=top_n_database_statement |yq e 'to_entries | with(.[] ; .value=(.value | to_entries))' -
+ expected: expected/db-has-value.yml
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/docker-compose.yaml b/test/e2e-v2/cases/mariadb/mariadb-slowsql/docker-compose.yaml
new file mode 100644
index 0000000000..822577e120
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/docker-compose.yaml
@@ -0,0 +1,121 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+version: '2.1'
+
+services:
+ oap:
+ extends:
+ file: ../../../script/docker-compose/base-compose.yml
+ service: oap
+ ports:
+ - 12800
+ networks:
+ - e2e
+ environment:
+ - SW_CORE_TOPN_REPORT_PERIOD=1
+ - SW_OTEL_RECEIVER=default
+
+ mariadb:
+ image: mariadb:10.7.3
+ networks:
+ - e2e
+ volumes:
+ - ../mariadb-slowsql/log:/var/lib/mysql
+ - ../mariadb-slowsql/my.cnf:/etc/mysql/my.cnf
+ environment:
+ - "MYSQL_ROOT_PASSWORD=password"
+ healthcheck:
+ test: "/usr/bin/mysql --user=root --password=password --execute \"SHOW DATABASES;\""
+ interval: 5s
+ timeout: 60s
+ retries: 120
+
+ mariadb-load:
+ image: mysql:8.0.13
+ depends_on:
+ oap:
+ condition: service_healthy
+ networks:
+ - e2e
+ entrypoint: bash
+ volumes:
+ - ./mock.sql:/docker/mock.sql
+ command:
+ - -c
+ - "mysql -h mariadb -uroot -ppassword < /docker/mock.sql"
+ depends_on:
+ mariadb:
+ condition: service_healthy
+
+ mariadb-load_2:
+ image: mysql:8.0.13
+ depends_on:
+ oap:
+ condition: service_healthy
+ networks:
+ - e2e
+ entrypoint: bash
+ command:
+ - -c
+ - "mysql -h mariadb -uroot -pwrong-password"
+ depends_on:
+ mariadb:
+ condition: service_healthy
+
+
+ fluentbit:
+ image: fluent/fluent-bit:1.9
+ environment:
+ - TZ=Asia/Shanghai
+ volumes:
+ - ../mariadb-slowsql/log:/tmp/skywalking-logs
+ - ./fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
+ - ./fluent-bit-parser.conf:/fluent-bit/etc/fluent-bit-parser.conf
+ - ./fluent-bit-script.lua:/fluent-bit/etc/fluent-bit-script.lua
+ networks:
+ - e2e
+ depends_on:
+ mariadb:
+ condition: service_healthy
+ oap:
+ condition: service_healthy
+
+ mariadb-service:
+ image: prom/mysqld-exporter:v0.14.0
+ ports:
+ - 9104
+ environment:
+ - DATA_SOURCE_NAME=root:password@(mariadb:3306)/
+ networks:
+ - e2e
+ depends_on:
+ mariadb:
+ condition: service_healthy
+
+ otel-collector:
+ image: otel/opentelemetry-collector:0.50.0
+ networks:
+ - e2e
+ command: [ "--config=/etc/otel-collector-config.yaml" ]
+ volumes:
+ - ../prometheus-mariadb-exporter/otel-collector-config.yaml:/etc/otel-collector-config.yaml
+ expose:
+ - 55678
+ depends_on:
+ oap:
+ condition: service_healthy
+networks:
+ e2e:
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/e2e.yaml b/test/e2e-v2/cases/mariadb/mariadb-slowsql/e2e.yaml
new file mode 100644
index 0000000000..40b8dc2122
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/e2e.yaml
@@ -0,0 +1,38 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file is used to show how to write configuration files and can be used to test.
+
+setup:
+ env: compose
+ file: docker-compose.yaml
+ timeout: 20m
+ init-system-environment: ../../../script/env
+ steps:
+ - name: set PATH
+ command: export PATH=/tmp/skywalking-infra-e2e/bin:$PATH
+ - name: install yq
+ command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh yq
+ - name: install swctl
+ command: bash test/e2e-v2/script/prepare/setup-e2e-shell/install.sh swctl
+
+verify:
+ retry:
+ count: 60
+ interval: 3s
+ cases:
+ - includes:
+ - ../mariadb-cases.yaml
+
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-parser.conf b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-parser.conf
new file mode 100644
index 0000000000..34b8a3c3df
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-parser.conf
@@ -0,0 +1,25 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[MULTILINE_PARSER]
+ name my-log-format
+ type regex
+ flush_timeout 1000
+ rule "start_state" "^(# Time: .*)" "second_state"
+ rule "second_state" "^(# User@Host.*)" "third_state"
+ rule "third_state" "^(# Thread_id: .*)" "fourth_state"
+ rule "fourth_state" "^(# Query_time: .*)" "fifth_state"
+ rule "fifth_state" "^(# Rows_affected: .*)" "statement"
+ rule "statement" "^\w+.*" "statement"
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-script.lua b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-script.lua
new file mode 100644
index 0000000000..e980154ec2
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit-script.lua
@@ -0,0 +1,139 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+-- The following is an example of a simple mariadb slow log
+-- # Time: 200929 15:42:35
+-- # User@Host: root[root] @ [192.168.1.1]
+-- # Thread_id: 2332 Schema: parkcloud_performancetest QC_hit: No
+-- # Query_time: 10.93443 Lock_time: 0.000091 Rows_sent: 5 Rows_examined: 9022
+-- # Rows_affected: 0
+-- SET timestamp=1670339460;
+function rewrite_body(tag, timestamp, record)
+ local log = record.log
+ record.log = nil
+ record.tags = { data = { { key = "LOG_KIND", value = "SLOW_SQL" } } }
+
+ local lines = {}
+ for line in string.gmatch(log, "[^\n]+") do
+ table.insert(lines, line)
+ end
+ inner_record = {}
+
+ inner_record.time = os.time() * 1000
+ inner_record.layer = "MYSQL"
+
+ record.layer = "MYSQL"
+
+ -- Here simply set the service name to `mysql::db`, you can also get the part you want from the second line of the log as the service name
+ record.service = "mysql::db"
+ inner_record.service = "mysql::db"
+
+ local query_time = lines[4]:match("%s(%S+)%s+Lock")
+ local qt = math.floor(query_time * 1000)
+ inner_record.query_time = qt
+ inner_record.statement = ""
+
+ inner_record.id = uuid()
+
+ for i = 6, #lines, 1 do
+ inner_record.statement = inner_record.statement .. lines[i]
+ end
+
+ local jsonstr = table2json(inner_record)
+ record.body = { json = { json = jsonstr } }
+
+ return 1, timestamp, record
+end
+
+local UUID_TEMPLATE = '%s-%s-%s-%s-%s'
+
+local random_hex = {}
+
+function random_hex.generate(n)
+ local seed = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
+ local s = ""
+ for i = 1, n do
+ s = s .. seed[math.random(1, 16)]
+ end
+ return s
+end
+
+function uuid(seed)
+ if seed then
+ math.randomseed(seed)
+ else
+ math.randomseed(os.time())
+ end
+ local sid = string.format(UUID_TEMPLATE,
+ random_hex.generate(4), random_hex.generate(4), random_hex.generate(4),
+ random_hex.generate(4), random_hex.generate(8)
+ )
+ return sid
+end
+
+
+function table2json(t)
+ local function serialize(tbl, structure)
+ local tmp = {}
+ for k, v in pairs(tbl) do
+ local k_type = type(k)
+ local v_type = type(v)
+ local key
+ if k_type == "string" then
+ key = '"' .. k .. '":'
+ elseif k_type == "number" then
+ key = ""
+ if structure == "array" then
+ key = ""
+ else
+ key = '"' .. k .. '":'
+ end
+ else
+ key = '"' .. tostring(k) .. '":'
+ end
+ local value
+ if v_type == "table" then
+ if next(v) == nil then
+ value = "null"
+ else
+ value = serialize(v, structure)
+ end
+ elseif v_type == "boolean" then
+ value = tostring(v)
+ elseif v_type == "string" then
+ value = '"' .. v .. '"'
+ else
+ value = v
+ end
+ tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
+ end
+ if structure == "array" then
+ return "[" .. table.concat(tmp, ",") .. "]"
+ else
+ return "{" .. table.concat(tmp, ",") .. "}"
+ end
+ end
+ assert(type(t) == "table")
+ if next(t) == nil then
+ return "null"
+ elseif next(t, next(t)) == nil and type(next(t)) == "number" then
+ return serialize(t, "array")
+ else
+ return serialize(t, "table")
+ end
+end
+
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit.conf b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit.conf
new file mode 100644
index 0000000000..547d4263e0
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/fluent-bit.conf
@@ -0,0 +1,52 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[SERVICE]
+ flush 1
+ log_level info
+ parsers_File fluent-bit-parser.conf
+[INPUT]
+ name tail
+ path /tmp/skywalking-logs/slow.log
+ read_from_head true
+ multiline.parser my-log-format
+[FILTER]
+ name grep
+ match *
+ exclude log mysqld, Version:.*
+[FILTER]
+ name grep
+ match *
+ exclude log Tcp port: 0.*
+[FILTER]
+ name grep
+ match *
+ exclude log Time .*
+[FILTER]
+ name lua
+ match *
+ script fluent-bit-script.lua
+ call rewrite_body
+[OUTPUT]
+ name stdout
+ match *
+ format json
+[OUTPUT]
+ name http
+ match *
+ host oap
+ port 12800
+ uri /v3/logs
+ format json
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/mock.sql b/test/e2e-v2/cases/mariadb/mariadb-slowsql/mock.sql
new file mode 100644
index 0000000000..ec4c385640
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/mock.sql
@@ -0,0 +1,42 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+CREATE DATABASE IF NOT EXISTS test;
+USE test;
+CREATE TABLE IF NOT EXISTS `t1`(
+ `te1` VARCHAR(100) NOT NULL
+)ENGINE=InnoDB DEFAULT CHARSET=utf8;
+SET GLOBAL event_scheduler = 1;
+
+CREATE EVENT `event_1`
+ON SCHEDULE EVERY 1 SECOND
+DO INSERT INTO t1 values('test');
+
+CREATE EVENT `event_2`
+ON SCHEDULE EVERY 1 SECOND
+DO UPDATE t1 SET `te1` = 1;
+
+CREATE EVENT `event_3`
+ON SCHEDULE EVERY 1 SECOND
+DO DELETE FROM t1;
+
+CREATE EVENT `event_4`
+ON SCHEDULE EVERY 1 SECOND
+DO COMMIT;
+
+use mysql;
+select sleep(3);
+select sleep(4);
+select sleep(5);
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/mariadb-slowsql/my.cnf b/test/e2e-v2/cases/mariadb/mariadb-slowsql/my.cnf
new file mode 100644
index 0000000000..f51b27ba55
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/mariadb-slowsql/my.cnf
@@ -0,0 +1,37 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[client-server]
+# Port or socket location where to connect
+# port = 3306
+socket = /run/mysqld/mysqld.sock
+
+# Import all .cnf files from configuration directory
+!includedir /etc/mysql/mariadb.conf.d/
+!includedir /etc/mysql/conf.d/
+
+# Slow SQL configuration
+[mysqld]
+init_connect='SET NAMES utf8'
+slow_query_log=ON
+event_scheduler=ON
+long_query_time=1
+slow_query_log_file=/var/lib/mysql/slow.log
+
+[client]
+default-character-set=utf8
+
+[mysql]
+default-character-set=utf8
\ No newline at end of file
diff --git a/test/e2e-v2/cases/mariadb/prometheus-mariadb-exporter/otel-collector-config.yaml b/test/e2e-v2/cases/mariadb/prometheus-mariadb-exporter/otel-collector-config.yaml
new file mode 100644
index 0000000000..792fe0f621
--- /dev/null
+++ b/test/e2e-v2/cases/mariadb/prometheus-mariadb-exporter/otel-collector-config.yaml
@@ -0,0 +1,54 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+receivers:
+ prometheus:
+ config:
+ scrape_configs:
+ - job_name: 'mysql-monitoring'
+ scrape_interval: 5s
+ static_configs:
+ - targets: ['mariadb-service:9104']
+ labels:
+ host_name: db
+processors:
+ batch:
+
+exporters:
+ ####################################################################################
+ # If you want to use otlp exporter please ensure that your OAP version is >= 9.2.0 #
+ # or you can use oc exporter, The config format should be: #
+ # opencensus: #
+ # endpoint: "oap:11800" #
+ # tls: #
+ # insecure: true #
+ ####################################################################################
+ otlp:
+ endpoint: oap:11800
+ tls:
+ insecure: true
+service:
+ pipelines:
+ metrics:
+ receivers:
+ - prometheus
+ processors:
+ - batch
+ exporters:
+ #########################################
+ # oc exporter config format: #
+ # - opencensus #
+ #########################################
+ - otlp
diff --git a/test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit-script.lua b/test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit-script.lua
index abf6cd7058..ef8890ffed 100644
--- a/test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit-script.lua
+++ b/test/e2e-v2/cases/mysql/mysql-slowsql/fluent-bit-script.lua
@@ -15,102 +15,123 @@
-- limitations under the License.
--
+-- The following is an example of a simple MySQL slow log
+-- # Time: 2022-12-06T15:11:00.449826Z
+-- # User@Host: root[root] @ [127.0.0.1] Id: 29
+-- # Query_time: 11.000312 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
+-- SET timestamp=1670339460;
+-- SELECT SLEEP(11);
function rewrite_body(tag, timestamp, record)
- log = record["log"]
- record["log"] = nil
- record["date"] = nil
- record["tags"] = {data={{key="LOG_KIND", value="SLOW_SQL"}}}
- arr = split(log,"\n")
- re1 = {}
-
- time = string.sub(arr[1], 9)
- time = string.sub(time,1,19)
- time = string.gsub(time,"-","");
- time = string.gsub(time,"T","");
- time = string.gsub(time,":","");
- y1 = string.sub(time,1,4)
- m1 = string.sub(time,5,6)
- d1 = string.sub(time,7,8)
- h1 = string.sub(time,9,10)
- min1 = string.sub(time,11,12)
- s1 = string.sub(time,13,14)
- re1["time"] = os.time() * 1000
-
- re1["layer"] = "MYSQL"
- record["layer"] = "MYSQL"
- id1,_ = string.find(arr[2],"Id:")
- service = string.sub(arr[2],14,id1-1)
- service = string.gsub(service," ","");
- service = string.sub(service,1,10)
- service = "mysql::"..service
- record["service"]=service
- re1["service"]= service
-
- f1,_ = string.find(arr[3],"Lock")
- query_time = string.sub(arr[3],15,f1-3)
- local qt,_ = math.modf(query_time*1000)
- re1["query_time"] = qt
- re1["statement"] = ""
-
- re1["id"] = uuid()
-
- for i=4,#arr,1 do
- re1["statement"] = re1["statement"]..arr[i]
- end
- jsonstr = table2json(re1)
- record["body"]={json={}}
- record["body"]["json"]["json"] = jsonstr
- return 1, timestamp, record
+ local log = record.log
+ record.log = nil
+ record.tags = { data = { { key = "LOG_KIND", value = "SLOW_SQL" } } }
+
+ local lines = {}
+ for line in string.gmatch(log, "[^\n]+") do
+ table.insert(lines, line)
+ end
+ inner_record = {}
+
+ inner_record.time = os.time() * 1000
+ inner_record.layer = "MYSQL"
+
+ record.layer = "MYSQL"
+
+ -- Here simply set the service name to `root[root]`, you can also get the part you want from the second line of the log as the service name
+ record.service = "mysql::" .. "root[root]"
+ inner_record.service = "mysql::" .. "root[root]"
+
+ local query_time = lines[3]:match("%s(%S+)%s+Lock")
+ local qt = math.floor(query_time * 1000)
+ inner_record.query_time = qt
+ inner_record.statement = ""
+
+ inner_record.id = uuid()
+
+ for i = 4, #lines, 1 do
+ inner_record.statement = inner_record.statement .. lines[i]
+ end
+
+ local jsonstr = table2json(inner_record)
+ record.body = { json = { json = jsonstr } }
+
+ return 1, timestamp, record
end
-function split(input, delimiter)
- input = tostring(input)
- delimiter = tostring(delimiter)
- if (delimiter == "") then return false end
- local pos, arr = 0, {}
- for st, sp in function() return string.find(input, delimiter, pos, true) end do
- table.insert(arr, string.sub(input, pos, st - 1))
- pos = sp + 1
- end
- table.insert(arr, string.sub(input, pos))
- return arr
+
+local UUID_TEMPLATE = '%s-%s-%s-%s-%s'
+
+local random_hex = {}
+
+function random_hex.generate(n)
+ local seed = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
+ local s = ""
+ for i = 1, n do
+ s = s .. seed[math.random(1, 16)]
+ end
+ return s
end
-function uuid()
- local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
- local tb={}
- for i=1,32 do
- table.insert(tb,seed[math.random(1,16)])
- end
- local sid=table.concat(tb)
- return string.format('%s-%s-%s-%s-%s',
- string.sub(sid,1,8),
- string.sub(sid,9,12),
- string.sub(sid,13,16),
- string.sub(sid,17,20),
- string.sub(sid,21,32)
- )
+function uuid(seed)
+ if seed then
+ math.randomseed(seed)
+ else
+ math.randomseed(os.time())
+ end
+ local sid = string.format(UUID_TEMPLATE,
+ random_hex.generate(4), random_hex.generate(4), random_hex.generate(4),
+ random_hex.generate(4), random_hex.generate(8)
+ )
+ return sid
end
function table2json(t)
- local function serialize(tbl)
+ local function serialize(tbl, structure)
local tmp = {}
for k, v in pairs(tbl) do
local k_type = type(k)
local v_type = type(v)
- local key = (k_type == "string" and '"' .. k .. '":') or (k_type == "number" and "")
- local value =
- (v_type == "table" and serialize(v)) or (v_type == "boolean" and tostring(v)) or
- (v_type == "string" and '"' .. v .. '"') or
- (v_type == "number" and v)
+ local key
+ if k_type == "string" then
+ key = '"' .. k .. '":'
+ elseif k_type == "number" then
+ key = ""
+ if structure == "array" then
+ key = ""
+ else
+ key = '"' .. k .. '":'
+ end
+ else
+ key = '"' .. tostring(k) .. '":'
+ end
+ local value
+ if v_type == "table" then
+ if next(v) == nil then
+ value = "null"
+ else
+ value = serialize(v, structure)
+ end
+ elseif v_type == "boolean" then
+ value = tostring(v)
+ elseif v_type == "string" then
+ value = '"' .. v .. '"'
+ else
+ value = v
+ end
tmp[#tmp + 1] = key and value and tostring(key) .. tostring(value) or nil
end
- if table.maxn(tbl) == 0 then
- return "{" .. table.concat(tmp, ",") .. "}"
- else
+ if structure == "array" then
return "[" .. table.concat(tmp, ",") .. "]"
+ else
+ return "{" .. table.concat(tmp, ",") .. "}"
end
end
assert(type(t) == "table")
- return serialize(t)
+ if next(t) == nil then
+ return "null"
+ elseif next(t, next(t)) == nil and type(next(t)) == "number" then
+ return serialize(t, "array")
+ else
+ return serialize(t, "table")
+ end
end