Repository: mosn/layotto (go)

Issue:
bug: configuration API returns wrong `app_id` and `store_name`
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!

-->


**What happened**:
When I play with [the configuration API demo](https://mosn.io/layotto/#/zh/start/configuration/start), I found that the demo printed some message with wrong `app_id` and `store_name`: 

![image](https://user-images.githubusercontent.com/26001097/170820085-71a0d16e-008d-4b64-b2fb-0688d401c0a2.png)


**What you expected to happen**:
Return correct app_id and store_name

**How to reproduce it (as minimally and precisely as possible)**:
Run the demo in https://mosn.io/layotto/#/zh/start/configuration/start

**Anything else we need to know?**:


Pull request:
fix: configuration API returns wrong `store_name`
<!--  Thanks for sending a pull request!
Read contributing.md before commit pull request.
-->

**What this PR does**:

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #613

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

Patch:
diff --git a/components/configstores/apollo/change_listener.go b/components/configstores/apollo/change_listener.go
index 3334321a..e595a463 100644
--- a/components/configstores/apollo/change_listener.go
+++ b/components/configstores/apollo/change_listener.go
@@ -35,6 +35,7 @@ type RepoForListener interface {
 	splitKey(keyWithLabel string) (key string, label string)
 	getAllTags(group string, keyWithLabel string) (tags map[string]string, err error)
 	GetAppId() string
+	GetStoreName() string
 }
 
 func newChangeListener(c RepoForListener) *changeListener {
@@ -79,7 +80,7 @@ func (lis *changeListener) notify(s *subscriber, keyWithLabel string, change *st
 		}
 	}()
 	// 2 prepare response
-	res := &configstores.SubscribeResp{StoreName: storename, AppId: lis.store.GetAppId()}
+	res := &configstores.SubscribeResp{StoreName: lis.store.GetStoreName(), AppId: lis.store.GetAppId()}
 	item := &configstores.ConfigurationItem{}
 	item.Group = s.group
 	item.Key, item.Label = lis.store.splitKey(keyWithLabel)
diff --git a/components/configstores/apollo/configstore.go b/components/configstores/apollo/configstore.go
index 2c1f3db6..2f1c5f11 100644
--- a/components/configstores/apollo/configstore.go
+++ b/components/configstores/apollo/configstore.go
@@ -173,6 +173,7 @@ func (c *ConfigStore) doInit(config *configstores.StoreConfig) error {
 	kvRepoConfig := &repoConfig{
 		addr:           addr,
 		appId:          appId,
+		storeName:      config.StoreName,
 		env:            c.env,
 		cluster:        metadata["cluster"],
 		namespaceName:  metadata["namespace_name"],
@@ -209,6 +210,13 @@ func (c *ConfigStore) GetAppId() string {
 	return c.kvConfig.appId
 }
 
+func (c *ConfigStore) GetStoreName() string {
+	if c.kvConfig == nil {
+		return ""
+	}
+	return c.kvConfig.storeName
+}
+
 // Get gets configuration from configuration store.
 func (c *ConfigStore) Get(ctx context.Context, req *configstores.GetRequest) ([]*configstores.ConfigurationItem, error) {
 	// TODO forced pagination
diff --git a/components/configstores/apollo/const.go b/components/configstores/apollo/const.go
index 7dfb9906..d2ad0550 100644
--- a/components/configstores/apollo/const.go
+++ b/components/configstores/apollo/const.go
@@ -17,7 +17,6 @@
 package apollo
 
 const (
-	storename                  = "apollo"
 	defaultTagsNamespace       = "sidecar_config_tags"
 	defaultDelimiter           = "@$"
 	defaultNamespace           = "application"
diff --git a/components/configstores/apollo/repository.go b/components/configstores/apollo/repository.go
index 67997f9a..96b5bc19 100644
--- a/components/configstores/apollo/repository.go
+++ b/components/configstores/apollo/repository.go
@@ -39,6 +39,7 @@ type Repository interface {
 type repoConfig struct {
 	addr          string
 	appId         string
+	storeName     string
 	env           string
 	cluster       string
 	namespaceName string
diff --git a/components/configstores/etcdv3/etcdv3.go b/components/configstores/etcdv3/etcdv3.go
index 834fc06e..38a140fa 100644
--- a/components/configstores/etcdv3/etcdv3.go
+++ b/components/configstores/etcdv3/etcdv3.go
@@ -41,6 +41,7 @@ type EtcdV3ConfigStore struct {
 	sync.RWMutex
 	subscribeKey map[string]string
 	appIdKey     string
+	storeName    string
 	// cancel is the func, call cancel will stop watching on the appIdKey
 	cancel       context.CancelFunc
 	watchStarted bool
@@ -70,6 +71,7 @@ func (c *EtcdV3ConfigStore) Init(config *configstores.StoreConfig) error {
 		Endpoints:   config.Address,
 		DialTimeout: time.Duration(t) * time.Second,
 	})
+	c.storeName = config.StoreName
 	return err
 }
 
@@ -172,7 +174,7 @@ func (c *EtcdV3ConfigStore) Delete(ctx context.Context, req *configstores.Delete
 }
 
 func (c *EtcdV3ConfigStore) processWatchResponse(resp *clientv3.WatchResponse) {
-	res := &configstores.SubscribeResp{StoreName: "etcd", AppId: c.appIdKey}
+	res := &configstores.SubscribeResp{StoreName: c.storeName, AppId: c.appIdKey}
 	item := &configstores.ConfigurationItem{}
 	if len(resp.Events) == 0 {
 		return
diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go
index ce82df85..d7bf118d 100644
--- a/pkg/runtime/runtime.go
+++ b/pkg/runtime/runtime.go
@@ -296,6 +296,7 @@ func (m *MosnRuntime) initConfigStores(configStores ...*configstores.StoreFactor
 			m.errInt(err, "create configstore's component %s failed", name)
 			return err
 		}
+		config.StoreName = name
 		if err := c.Init(&config); err != nil {
 			m.errInt(err, "init configstore's component %s failed", name)
 			return err


Test patch:
diff --git a/components/configstores/apollo/change_listener_test.go b/components/configstores/apollo/change_listener_test.go
index 7ec3a8d3..6475b7b7 100644
--- a/components/configstores/apollo/change_listener_test.go
+++ b/components/configstores/apollo/change_listener_test.go
@@ -27,7 +27,10 @@ import (
 	"mosn.io/layotto/components/configstores"
 )
 
-const testAppId = "test_app"
+const (
+	testAppId     = "test_app"
+	testStoreName = "test_storename"
+)
 
 type MockRepo struct {
 	c *ConfigStore
@@ -45,6 +48,10 @@ func (m *MockRepo) GetAppId() string {
 	return testAppId
 }
 
+func (m *MockRepo) GetStoreName() string {
+	return testStoreName
+}
+
 const ns = "application"
 
 func setupChangeListener() *changeListener {
@@ -69,7 +76,7 @@ func Test_changeListener_OnChange(t *testing.T) {
 	go func() {
 		select {
 		case c2 := <-ch:
-			assert.Equal(t, c2.StoreName, "apollo")
+			assert.Equal(t, c2.StoreName, testStoreName)
 			assert.Equal(t, c2.AppId, testAppId)
 			assert.True(t, len(c2.Items) == 1)
 			assert.True(t, c2.Items[0].Key == "key1")
diff --git a/components/configstores/apollo/configstore_test.go b/components/configstores/apollo/configstore_test.go
index 46156765..2271d0b8 100644
--- a/components/configstores/apollo/configstore_test.go
+++ b/components/configstores/apollo/configstore_test.go
@@ -125,6 +125,10 @@ func TestConfigStore_read(t *testing.T) {
 	assert.True(t, store.GetAppId() == "testApplication_yang")
 	assert.True(t, store.GetDefaultGroup() != "")
 	assert.True(t, store.GetDefaultLabel() == "")
+
+	// get storeName
+	assert.True(t, store.GetStoreName() == "config_demo")
+
 	//	get key
 	var req configstores.GetRequest
 	req.AppId = appId
@@ -255,6 +259,7 @@ func setup(t *testing.T) (*ConfigStore, *configstores.StoreConfig) {
     "address": [
         "http://106.54.227.205:8080"
     ],
+	"store_name": "config_demo",
     "metadata": {
         "app_id": "testApplication_yang",
         "cluster": "default",
diff --git a/components/configstores/etcdv3/etcdv3_test.go b/components/configstores/etcdv3/etcdv3_test.go
index aad51e21..ee2d4bf9 100644
--- a/components/configstores/etcdv3/etcdv3_test.go
+++ b/components/configstores/etcdv3/etcdv3_test.go
@@ -191,12 +191,13 @@ func (suite *ClientTestSuite) Subscribe() {
 		if i == 0 {
 			assert.Equal(suite.T(), event.Items[0].Key, "sofa")
 			assert.Equal(suite.T(), event.Items[0].Content, "v1")
+			assert.Equal(suite.T(), event.StoreName, "etcd")
 			i++
 			continue
 		}
 		assert.Equal(suite.T(), event.Items[0].Key, "sofa")
 		assert.Equal(suite.T(), event.Items[0].Content, "")
-
+		assert.Equal(suite.T(), event.StoreName, "etcd")
 	}
 	wg.Done()
 }