func afterNewConfigClient()

in pkg/rules/nacos/config/nacos_go_client_config_setup.go [58:105]


func afterNewConfigClient(call api.CallContext, client *config_client.ConfigClient, err error) {
	if !experimental.NacosEnabler.Enable() {
		return
	}
	if client == nil {
		return
	}
	// get reference for cache map
	t := reflect.ValueOf(client)
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	} else {
		return
	}
	cacheMapField := t.FieldByName("cacheMap")
	if cacheMapField.IsValid() {
		cf := reflect.NewAt(cacheMapField.Type(), unsafe.Pointer(cacheMapField.UnsafeAddr())).Elem()
		cacheMap, ok := cf.Interface().(cache.ConcurrentMap)
		if !ok {
			return
		}
		attrSet := attribute.NewSet(attribute.KeyValue{
			Key:   "namespace",
			Value: attribute.StringValue(call.GetKeyData("namespace").(string)),
		}, attribute.KeyValue{
			Key:   "region",
			Value: attribute.StringValue(call.GetKeyData("region").(string)),
		}, attribute.KeyValue{
			Key:   "appName",
			Value: attribute.StringValue(call.GetKeyData("appName").(string)),
		}, attribute.KeyValue{
			Key:   "appKey",
			Value: attribute.StringValue(call.GetKeyData("appKey").(string)),
		}, attribute.KeyValue{
			Key:   "userName",
			Value: attribute.StringValue(call.GetKeyData("userName").(string)),
		})
		reg, err := experimental.GlobalMeter.RegisterCallback(func(ctx context.Context, observer metric.Observer) error {
			observer.ObserveInt64(experimental.ClientConfigCacheMapSize, int64(cacheMap.Count()), metric.WithAttributeSet(attrSet))
			return nil
		}, experimental.ClientConfigCacheMapSize)
		if err != nil {
			log.Printf("[otel nacos] failed to register metrics for config info holder, %v\n", err)
		} else {
			client.OtelReg = reg
		}
	}
}