Skip to content

Commit af2b169

Browse files
committed
1.0: Update usage of new service discovery helper API
Since v1.13.0 new helper API is introduced. It makes supporting service_discovery easy. ref. fluent/fluentd#3299 Signed-off-by: Kentaro Hayashi <[email protected]>
1 parent 98888aa commit af2b169

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

plugin-helper-overview/api-plugin-helper-service_discovery.md

+73-7
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ module Fluent::Plugin
1717
def configure(conf)
1818
super
1919

20-
config = conf.elements(name: 'service_discovery').map do |s|
21-
{ type: :static, conf: s }
22-
end
23-
2420
# 2. Create and start service discovery manager
25-
service_discovery_create_manager(
21+
service_discovery_configure(
2622
:out_example_service_discovery_watcher,
27-
configurations: config,
23+
static_default_service_directive: 'server'
2824
)
2925
end
3026

3127
def write(chunk)
3228
# 3. Select service to send data
33-
discovery_manager.select_service do |node|
29+
service_discovery_select_service do |node|
3430
send_data(node, chunk)
3531
end
3632
end
@@ -46,6 +42,18 @@ end
4642

4743
## Methods
4844

45+
### `service_discovery_configure(title, static_default_service_directive: nil, load_balancer: nil, custom_build_method: nil, interval: 3)`
46+
47+
Since v1.13.0, `discovery_manager` is almost automatically configured only by calling this method.
48+
49+
#### Parameters
50+
51+
* `title`: Thread name. Must be unique. \(required\)
52+
* `static_default_service_directive`: The directive name of each service when "static" service discovery is enabled in default.
53+
* `load_balancer`: object which has two methods #rebalance and #select_service.
54+
* `custom_build_method`: The custom method to build the service.
55+
* `interval`: Time interval for updating target service.
56+
4957
### `service_discovery_create_manager(title, configurations:, load_balancer: nil, custom_build_method: nil, interval: 3)`
5058

5159
This method creates `service_discovery_manager`.
@@ -66,5 +74,63 @@ It manages service discovery functionalities such as updating target services an
6674

6775
* [`out_forward`](../output/forward.md)
6876

77+
## Migration guide from `service_discovery_create_manager` to more simpler helper method
78+
79+
Here is the guide to migrate to newer API which is available since v1.13.0.
80+
81+
Example:
82+
83+
```ruby
84+
require 'fluent/plugin/output'
85+
86+
module Fluent::Plugin
87+
class ExampleOutput < Output
88+
Fluent::Plugin.register_output('example', self)
89+
90+
helpers :service_discovery
91+
92+
def configure(conf)
93+
super
94+
95+
# 1. Remove the following code which parse 'service_discovery' section by yourself
96+
#
97+
# config = conf.elements(name: 'service_discovery').map do |s|
98+
# { type: :static, conf: s }
99+
# end
100+
101+
# 2. Remove the following code and use service_discovery_configure
102+
#
103+
# service_discovery_create_manager(
104+
# :out_example_service_discovery_watcher,
105+
# configurations: config,
106+
# )
107+
service_discovery_configure(
108+
:out_example_service_discovery_watcher,
109+
static_default_service_directive: 'server'
110+
)
111+
end
112+
113+
def write(chunk)
114+
# 3. Remove the following code and use service_discovery_select_service to select service
115+
#
116+
# @discovery_manager.select_service do |node|
117+
# send_data(node, chunk)
118+
# end
119+
service_discovery_select_service do |node|
120+
send_data(node, chunk)
121+
end
122+
end
123+
124+
def send_data(node, chunk)
125+
# Send data
126+
end
127+
end
128+
end
129+
```
130+
131+
You can also use helper methods such as `service_discovery_services` or `service_discovery_rebalance` instead of
132+
`@discovery_manager.services` or `@discovery_manager.rebalance`.
133+
134+
69135
If this article is incorrect or outdated, or omits critical information, please [let us know](https://github.com/fluent/fluentd-docs-gitbook/issues?state=open). [Fluentd](http://www.fluentd.org/) is an open-source project under [Cloud Native Computing Foundation \(CNCF\)](https://cncf.io/). All components are available under the Apache 2 License.
70136

0 commit comments

Comments
 (0)