github.com/polarismesh/polaris@v1.17.8/common/model/instance_event.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package model 19 20 import ( 21 "context" 22 "fmt" 23 "time" 24 25 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 26 ) 27 28 // InstanceEventType 探测事件类型 29 type InstanceEventType string 30 31 const ( 32 // EventDiscoverNone empty discover event 33 EventDiscoverNone InstanceEventType = "EventDiscoverNone" 34 // EventInstanceOnline instance becoming online 35 EventInstanceOnline InstanceEventType = "InstanceOnline" 36 // EventInstanceTurnUnHealth Instance becomes unhealthy 37 EventInstanceTurnUnHealth InstanceEventType = "InstanceTurnUnHealth" 38 // EventInstanceTurnHealth Instance becomes healthy 39 EventInstanceTurnHealth InstanceEventType = "InstanceTurnHealth" 40 // EventInstanceOpenIsolate Instance is in isolation 41 EventInstanceOpenIsolate InstanceEventType = "InstanceOpenIsolate" 42 // EventInstanceCloseIsolate Instance shutdown isolation state 43 EventInstanceCloseIsolate InstanceEventType = "InstanceCloseIsolate" 44 // EventInstanceOffline Instance offline 45 EventInstanceOffline InstanceEventType = "InstanceOffline" 46 // EventInstanceSendHeartbeat Instance send heartbeat package to server 47 EventInstanceSendHeartbeat InstanceEventType = "InstanceSendHeartbeat" 48 // EventInstanceUpdate Instance metadata and info update event 49 EventInstanceUpdate InstanceEventType = "InstanceUpdate" 50 ) 51 52 // CtxEventKeyMetadata 用于将metadata从Context中传入并取出 53 const CtxEventKeyMetadata = "ctx_event_metadata" 54 55 // InstanceEvent 服务实例事件 56 type InstanceEvent struct { 57 Id string 58 SvcId string 59 Namespace string 60 Service string 61 Instance *apiservice.Instance 62 EType InstanceEventType 63 CreateTime time.Time 64 MetaData map[string]string 65 } 66 67 // InjectMetadata 从context中获取metadata并注入到事件对象 68 func (i *InstanceEvent) InjectMetadata(ctx context.Context) { 69 value := ctx.Value(CtxEventKeyMetadata) 70 if nil == value { 71 return 72 } 73 i.MetaData = value.(map[string]string) 74 } 75 76 func (i *InstanceEvent) String() string { 77 if nil == i { 78 return "nil" 79 } 80 hostPortStr := fmt.Sprintf("%s:%d", i.Instance.GetHost().GetValue(), i.Instance.GetPort().GetValue()) 81 return fmt.Sprintf("InstanceEvent(id=%s, namespace=%s, svcId=%s, service=%s, type=%v, instance=%s, healthy=%v)", 82 i.Id, i.Namespace, i.SvcId, i.Service, i.EType, hostPortStr, i.Instance.GetHealthy().GetValue()) 83 }