skywalking

Description

The skywalking Plugin is used to integrate with Apache SkyWalking.

SkyWalking uses its native Nginx Lua tracer to provide tracing, topology analysis, and metrics from both service and URI perspectives.

APISIX supports HTTP protocol to interact with the SkyWalking server.

Attributes

NameTypeRequiredDefaultValid valuesDescription
sample_rationumberTrue1[0.00001, 1]How often to sample the requests. Setting to 1 will sample all requests.

Configuring the endpoint

You can configure these attributes on your configuration file (conf/config.yaml):

NameTypeDefaultDescription
service_namestring“APISIX”Service name for SkyWalking reporter.
service_instance_namestring“APISIX Instance Name”Service instance name for SkyWalking reporter. Set to $hostname to get the local hostname.
endpoint_addrstringhttp://127.0.0.1:12800”SkyWalking HTTP endpoint. For example, http://127.0.0.1:12800.
report_intervalintegerValue in the SkyWalking client libraryReporting interval time in seconds.

You can configure these as shown below:

conf/config.yaml

  1. plugin_attr:
  2. skywalking:
  3. service_name: APISIX
  4. service_instance_name: "APISIX Instance Name"
  5. endpoint_addr: http://127.0.0.1:12800

Sample code for upstream configuration

Java with Spring Boot

  1. import org.springframework.web.bind.annotation.PathVariable;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import javax.servlet.http.HttpServletRequest;
  5. @RestController
  6. public class TestController {
  7. @RequestMapping("/uid/{count}")
  8. public String getUidList(@PathVariable("count") String countStr, HttpServletRequest request) {
  9. return "OK";
  10. }
  11. }

You can configure the SkyWalking agent while starting your service:

agent/config/agent.config

  1. agent.service_name=yourservername
  2. collector.backend_service=10.110.149.175:11800

Then, run the script:

  1. nohup java -javaagent:/root/skywalking/app/agent/skywalking-agent.jar \
  2. -jar /root/skywalking/app/app.jar \
  3. --server.port=8089 \
  4. 2>&1 > /root/skywalking/app/logs/nohup.log &

Enabling the Plugin

To enable the Plugin, you have to add it to your configuration file (conf/config.yaml):

conf/config.yaml

  1. plugins:
  2. - ...
  3. - skywalking

Once you do this, a background timer will be created to report data to the SkyWalking OAP server.

Now, you can enable the Plugin on a specific Route:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uris": [
  5. "/uid/*"
  6. ],
  7. "plugins": {
  8. "skywalking": {
  9. "sample_ratio": 1
  10. }
  11. },
  12. "upstream": {
  13. "type": "roundrobin",
  14. "nodes": {
  15. "10.110.149.175:8089": 1
  16. }
  17. }
  18. }'

Example usage

First, you need to have your SkyWalking server running.

You can run it on Docker by:

  1. sudo docker run --name skywalking -d -p 1234:1234 -p 12800:12800 --restart always apache/skywalking-oap-server:8.7.0-es6
skywalking - 图1tip

By default, SkyWalking uses H2 storage. You can also use Elasticsearch storage instead.

First, install Elasticsearch. You can do it on Docker by running:

  1. sudo docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 --restart always -e "discovery.type=single-node" elasticsearch:6.7.2

Optionally, you can install Elasticsearch management page, elasticsearch-hq:

  1. sudo docker run -d --name elastic-hq -p 5000:5000 --restart always elastichq/elasticsearch-hq

Once you have Elasticsearch installed, you can start a SkyWalking server by running:

  1. sudo docker run --name skywalking -d -p 1234:1234 -p 12800:12800 --restart always --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server:8.7.0-es6

You can also install SkyWalking UI to view the data. To run it on Docker:

  1. sudo docker run --name skywalking-ui -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 --restart always apache/skywalking-ui

You should be able to access the UI from your browser:

Now if you make requests to APISIX:

  1. curl -v http://10.110.149.192:9080/uid/12
  1. HTTP/1.1 200 OK
  2. OK
  3. ...

You should be able to see the topology of all services in your SkyWalking UI:

You should also be able to see traces from all services:

Disable Plugin

To disable the skywalking Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uris": [
  5. "/uid/*"
  6. ],
  7. "plugins": {
  8. },
  9. "upstream": {
  10. "type": "roundrobin",
  11. "nodes": {
  12. "10.110.149.175:8089": 1
  13. }
  14. }
  15. }'

To completely disable the skywalking Plugin, i.e to stop the background report timer, you need to remove the Plugin from your configuration file (conf/config.yaml):

  1. plugins:
  2. - ...
  3. # - skywalking