Skip to content

Conversation

@ZPascal
Copy link
Contributor

@ZPascal ZPascal commented Jan 29, 2026

Description

This PR adds the Aliyun common library to Telegraf.

Checklist

  • No AI generated code was used in this PR
  • I have read the contributing guidelines
  • I have signed the CLA
  • I have added tests to cover my changes
  • All new and existing tests pass

Related issues

related to #18253

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Jan 29, 2026
@ZPascal
Copy link
Contributor Author

ZPascal commented Jan 29, 2026

FYI: @srebhan This PR integrates the Aliyun common library.

@telegraf-tiger
Copy link
Contributor

@srebhan srebhan changed the title feat: Add the aliyun common lib feat(inputs.aliyuncms): Refactor common parts to plugins/common Jan 30, 2026
@telegraf-tiger telegraf-tiger bot added the plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins label Jan 30, 2026
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @ZPascal! I have some comments in the code. Furthermore, I would love to see inputs.aliyuncms to be migrated to this package in this PR without change of functionality.

Comment on lines +32 to +35
var (
roleSessionExpiration = 3600
sessionExpiration = 3600
)
Copy link
Member

Choose a reason for hiding this comment

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

Please use

Suggested change
var (
roleSessionExpiration = 3600
sessionExpiration = 3600
)
roleSessionExpiration := 3600
sessionExpiration := 3600

Comment on lines +9 to +73
func TestGetCredentials(t *testing.T) {
tests := []struct {
name string
config CredentialConfig
expectError bool
}{
{
name: "with access key credentials",
config: CredentialConfig{
AccessKeyID: "test-key-id",
AccessKeySecret: "test-key-secret",
},
expectError: false,
},
{
name: "with access key and STS token",
config: CredentialConfig{
AccessKeyID: "test-key-id",
AccessKeySecret: "test-key-secret",
AccessKeyStsToken: "test-sts-token",
},
expectError: false,
},
{
name: "with role ARN",
config: CredentialConfig{
AccessKeyID: "test-key-id",
AccessKeySecret: "test-key-secret",
RoleArn: "acs:ram::123456:role/test-role",
RoleSessionName: "test-session",
},
expectError: false,
},
{
name: "with RSA keypair",
config: CredentialConfig{
PublicKeyID: "test-public-key-id",
PrivateKey: "-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----",
},
expectError: true,
},
{
name: "with ECS RAM role name",
config: CredentialConfig{
RoleName: "test-ecs-role",
},
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cred, err := GetCredentials(tt.config)

if tt.expectError {
if err == nil {
require.NotNil(t, cred)
}
} else {
require.NoError(t, err)
require.NotNil(t, cred)
}
})
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Please split the tests into TestGetCredentialsSuccess and TestGetCredentialsFail to simplify the code and to provide context on what to expect for the reader.

Comment on lines +78 to +82
if err != nil {
require.Contains(t, err.Error(), "failed to retrieve credential")
} else {
require.NotNil(t, cred)
}
Copy link
Member

Choose a reason for hiding this comment

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

No! You should know what you expect from that call! Either this should return an error, then check for it, or it doesn't!

Comment on lines +225 to +229
var (
err error
data map[string]interface{}
lastData map[string]interface{}
)
Copy link
Member

Choose a reason for hiding this comment

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

None of these is required to be declared outside of the go-routine, is it? Please use implicit declarations where possible and limit the scope of variables to a minimum to avoid unintended reuse!

Comment on lines +245 to +246
case <-dt.done:
return
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't using a cancel-able context be better here?

Comment on lines +96 to +100
req = "string"
require.NotNil(t, req)

req = 123
require.NotNil(t, req)
Copy link
Member

Choose a reason for hiding this comment

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

What does this test?

Comment on lines +76 to +87
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rpcReq, err := getRPCReqFromDiscoveryRequest(tt.req)
if tt.expectError {
require.Error(t, err)
require.Nil(t, rpcReq)
} else {
require.NoError(t, err)
require.NotNil(t, rpcReq)
}
})
}
Copy link
Member

Choose a reason for hiding this comment

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

Please split into a test that should succeed and one that should fail to simplify code and to provide context to the reader.

Same for other cases where you have if expectedError else constructs in tests!

Comment on lines +91 to +93
var req DiscoveryRequest

req = &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}}
Copy link
Member

Choose a reason for hiding this comment

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

Please use implicit declarations where possible

Suggested change
var req DiscoveryRequest
req = &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}}
req := &mockDiscoveryRequest{RpcRequest: &requests.RpcRequest{}}

Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to get the regions from the SDK? I don't want to maintain that list in telegraf as it is likely to be changed without anyone from Aliyun notifying us...

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this test does anything useful...

@srebhan srebhan self-assigned this Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants