Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Alibaba Cloud provider for add_cloud_metadata proccessor #4111

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ https://github.com/elastic/beats/compare/v5.0.2...v5.1.1[View commits]
- Add add_cloud_metadata processor for collecting cloud provider metadata. {pull}2728[2728]
- Added decode_json_fields processor for decoding fields containing JSON strings. {pull}2605[2605]
- Add Tencent Cloud provider for add_cloud_metadata processor. {pull}4023[4023]
- Add Alibaba Cloud provider for add_cloud_metadata processor. {pull}4111[4111]

*Metricbeat*

Expand Down
17 changes: 17 additions & 0 deletions libbeat/docs/processors-config.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ The following cloud providers are supported:
- Digital Ocean
- Google Compute Engine (GCE)
- https://www.qcloud.com/?lang=en[Tencent Cloud] (QCloud)
- Alibaba Cloud (ECS)

The simple configuration below enables the processor.

Expand Down Expand Up @@ -359,6 +360,22 @@ _Tencent Cloud_
}
-------------------------------------------------------------------------------

_Alibaba Cloud_

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that this is supposed to just be examples of the output, but it would be good to mention something like, "Only when VPC is selected as the network type of the instance, you can get the metadata."

@dedemorton Maybe you have wording or formatting suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the wording because we don't want to take the text verbatim from the Alibaba docs. How about adding this before the example:

This metadata is only available when VPC is selected as the network type of the instance.

Or, assuming we are talking about an ECS instance here (apologies for my ignorance), this would be even better and less confusing for users:

This metadata is only available when VPC is selected as the network type of the ECS instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added on the position above the example output.

[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"availability_zone": "cn-shenzhen",
"instance_id": "i-wz9g2hqiikg0aliyun2b",
"provider": "ecs",
"region": "cn-shenzhen-a"
}
}
}
-------------------------------------------------------------------------------

[[add-locale]]
=== add_locale

Expand Down
5 changes: 5 additions & 0 deletions libbeat/processors/add_cloud_metadata/add_cloud_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,17 @@ func setupFetchers(c common.Config) ([]*metadataFetcher, error) {
if err != nil {
return fetchers, err
}
ecsFetcher, err := newAlibabaCloudMetadataFetcher(c)
if err != nil {
return fetchers, err
}

fetchers = []*metadataFetcher{
doFetcher,
ec2Fetcher,
gceFetcher,
qcloudFetcher,
ecsFetcher,
}
return fetchers, nil
}
Expand Down
41 changes: 41 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package add_cloud_metadata

import "github.com/elastic/beats/libbeat/common"

// Alibaba Cloud Metadata Service
// Document https://help.aliyun.com/knowledge_detail/49122.html
func newAlibabaCloudMetadataFetcher(c common.Config) (*metadataFetcher, error) {
ecsMetadataHost := "100.100.100.200"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that this is a fixed IP and not a domain name. Couldn't make much sense of the docs above unfortunately ;-)

Copy link
Contributor Author

@athom athom Apr 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, maybe metadata is not a really important feature for Chinese users, the design looks not so serious in both Tencent Cloud and Alibaba Cloud :-)

Couldn't make much sense of the docs above unfortunately ;-)

You mean remove the document comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is fine.

ecsMetadataInstanceIDURI := "/latest/meta-data/instance-id"
ecsMetadataRegionURI := "/latest/meta-data/region-id"
ecsMetadataZoneURI := "/latest/meta-data/zone-id"

ecsSchema := func(m map[string]interface{}) common.MapStr {
return common.MapStr(m)
}

urls, err := getMetadataURLs(c, ecsMetadataHost, []string{
ecsMetadataInstanceIDURI,
ecsMetadataRegionURI,
ecsMetadataZoneURI,
})
if err != nil {
return nil, err
}
responseHandlers := map[string]responseHandler{
urls[0]: func(all []byte, result *result) error {
result.metadata["instance_id"] = string(all)
return nil
},
urls[1]: func(all []byte, result *result) error {
result.metadata["region"] = string(all)
return nil
},
urls[2]: func(all []byte, result *result) error {
result.metadata["availability_zone"] = string(all)
return nil
},
}
fetcher := &metadataFetcher{"ecs", nil, responseHandlers, ecsSchema}
return fetcher, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package add_cloud_metadata

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/stretchr/testify/assert"
)

func initECSTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/latest/meta-data/instance-id" {
w.Write([]byte("i-wz9g2hqiikg0aliyun2b"))
return
}
if r.RequestURI == "/latest/meta-data/region-id" {
w.Write([]byte("cn-shenzhen"))
return
}
if r.RequestURI == "/latest/meta-data/zone-id" {
w.Write([]byte("cn-shenzhen-a"))
return
}

http.Error(w, "not found", http.StatusNotFound)
}))
}

func TestRetrieveAlibabaCloudMetadata(t *testing.T) {
if testing.Verbose() {
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"})
}

server := initECSTestServer()
defer server.Close()

config, err := common.NewConfigFrom(map[string]interface{}{
"host": server.Listener.Addr().String(),
})

if err != nil {
t.Fatal(err)
}

p, err := newCloudMetadata(*config)
if err != nil {
t.Fatal(err)
}

actual, err := p.Run(common.MapStr{})
if err != nil {
t.Fatal(err)
}

expected := common.MapStr{
"meta": common.MapStr{
"cloud": common.MapStr{
"provider": "ecs",
"instance_id": "i-wz9g2hqiikg0aliyun2b",
"region": "cn-shenzhen",
"availability_zone": "cn-shenzhen-a",
},
},
}
assert.Equal(t, expected, actual)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package add_cloud_metadata

import "github.com/elastic/beats/libbeat/common"

// Tenccent Cloud Metadata Service
// Tencent Cloud Metadata Service
// Document https://www.qcloud.com/document/product/213/4934
func newQcloudMetadataFetcher(c common.Config) (*metadataFetcher, error) {
qcloudMetadataHost := "metadata.tencentyun.com"
qcloudMetadataInstanceIDURI := "/meta-data/instance-id"
Expand Down