3838import com .tencent .polaris .api .utils .ThreadPoolUtils ;
3939import com .tencent .polaris .client .util .NamedThreadFactory ;
4040import com .tencent .polaris .logging .LoggerFactory ;
41+ import com .tencent .polaris .metadata .core .constant .MetadataConstants ;
42+ import com .tencent .polaris .metadata .core .manager .CalleeMetadataContainerGroup ;
4143import com .tencent .polaris .plugins .event .tsf .report .CloudEvent ;
4244import com .tencent .polaris .plugins .event .tsf .report .Event ;
4345import com .tencent .polaris .plugins .event .tsf .report .EventResponse ;
@@ -83,6 +85,8 @@ public class TsfEventReporter implements EventReporter, PluginConfigProvider {
8385
8486 private volatile boolean init = true ;
8587
88+ private volatile boolean eventUriInit = false ;
89+
8690 private TsfEventReporterConfig tsfEventReporterConfig ;
8791
8892 private URI v1EventUri ;
@@ -117,6 +121,7 @@ public boolean reportEvent(BaseEvent baseEvent) {
117121
118122 private boolean reportV1Event (FlowEvent flowEvent ) {
119123 try {
124+ initEventUri ();
120125 if (v1EventUri == null ) {
121126 LOG .warn ("build v1 event request url fail, can not sent event." );
122127 return false ;
@@ -134,11 +139,11 @@ private boolean reportV1Event(FlowEvent flowEvent) {
134139 List <TsfEventDataPair > dimensions = new ArrayList <>();
135140 dimensions .add (new TsfEventDataPair (APP_ID_KEY , tsfEventReporterConfig .getAppId ()));
136141 dimensions .add (new TsfEventDataPair (NAMESPACE_ID_KEY , tsfEventReporterConfig .getTsfNamespaceId ()));
137- dimensions .add (new TsfEventDataPair (SERVICE_NAME , tsfEventReporterConfig . getServiceName ()));
142+ dimensions .add (new TsfEventDataPair (SERVICE_NAME , getLocalServiceName ()));
138143 eventData .setDimensions (dimensions );
139144
140145 List <TsfEventDataPair > additionalMsg = new ArrayList <>();
141- additionalMsg .add (new TsfEventDataPair (UPSTREAM_SERVICE_KEY , tsfEventReporterConfig . getServiceName ()));
146+ additionalMsg .add (new TsfEventDataPair (UPSTREAM_SERVICE_KEY , getLocalServiceName ()));
142147 additionalMsg .add (new TsfEventDataPair (UPSTREAM_NAMESPACE_ID_KEY , tsfEventReporterConfig .getTsfNamespaceId ()));
143148 additionalMsg .add (new TsfEventDataPair (DOWNSTREAM_SERVICE_KEY , flowEvent .getService ()));
144149 additionalMsg .add (new TsfEventDataPair (DOWNSTREAM_NAMESPACE_ID_KEY , flowEvent .getNamespace ()));
@@ -193,7 +198,7 @@ private boolean reportReportEvent(FlowEvent flowEvent) {
193198
194199 cloudEvent .putDimension (APP_ID_KEY , tsfEventReporterConfig .getAppId ());
195200 cloudEvent .putDimension (NAMESPACE_ID_KEY , tsfEventReporterConfig .getTsfNamespaceId ());
196- cloudEvent .putDimension (SERVICE_NAME , tsfEventReporterConfig . getServiceName ());
201+ cloudEvent .putDimension (SERVICE_NAME , getLocalServiceName ());
197202 cloudEvent .putDimension (APPLICATION_ID , tsfEventReporterConfig .getApplicationId ());
198203
199204 cloudEvent .putExtensionMsg (UPSTREAM_SERVICE_KEY , flowEvent .getSourceService ());
@@ -208,7 +213,7 @@ private boolean reportReportEvent(FlowEvent flowEvent) {
208213
209214 String uniqueId = tsfEventReporterConfig .getInstanceId ()
210215 + "#" + tsfEventReporterConfig .getTsfNamespaceId ()
211- + "#" + tsfEventReporterConfig . getServiceName () + "#" + ruleId ;
216+ + "#" + getLocalServiceName () + "#" + ruleId ;
212217 cloudEvent .setId (uniqueId );
213218 cloudEvent .setObject (uniqueId );
214219
@@ -269,18 +274,7 @@ public void postContextInit(Extensions ctx) throws PolarisException {
269274 if (!init ) {
270275 init = true ;
271276 try {
272- String v1Path = String .format ("/v1/event/%s/%s" ,
273- URLEncoder .encode (tsfEventReporterConfig .getServiceName (), "UTF-8" ),
274- URLEncoder .encode (tsfEventReporterConfig .getInstanceId (), "UTF-8" ));
275- v1EventUri = new URIBuilder ()
276- .setScheme ("http" )
277- .setHost (IPAddressUtils .getIpCompatible (tsfEventReporterConfig .getEventMasterIp ()))
278- .setPort (tsfEventReporterConfig .getEventMasterPort ())
279- .setPath (v1Path )
280- .setParameter ("token" , tsfEventReporterConfig .getToken ())
281- .build ();
282277 v1EventExecutors .scheduleWithFixedDelay (new TsfV1EventTask (), 1000 , 1000 , TimeUnit .MILLISECONDS );
283- LOG .info ("Tsf v1 event reporter init with uri: {}" , v1EventUri );
284278
285279 this .reportEventUri = new URIBuilder ()
286280 .setScheme ("http" )
@@ -292,9 +286,41 @@ public void postContextInit(Extensions ctx) throws PolarisException {
292286 reportEventExecutors .scheduleWithFixedDelay (new TsfReportEventTask (), 1000 , 1000 , TimeUnit .MILLISECONDS );
293287 LOG .info ("Tsf report event reporter init with uri: {}" , reportEventUri );
294288 LOG .info ("Tsf event reporter starts reporting task." );
289+ } catch (URISyntaxException e ) {
290+ LOG .error ("Build event request url fail." , e );
291+ }
292+ }
293+ }
294+ }
295+ }
296+
297+ private String getLocalServiceName () {
298+ return CalleeMetadataContainerGroup .getStaticApplicationMetadataContainer ().getRawMetadataStringValue (MetadataConstants .LOCAL_SERVICE );
299+ }
300+
301+ /**
302+ * delay init event uri, as service name may not be ready at bootstrap stage.
303+ */
304+ private void initEventUri () {
305+ if (!eventUriInit ) {
306+ synchronized (this ) {
307+ if (!eventUriInit ) {
308+ try {
309+ String v1Path = String .format ("/v1/event/%s/%s" ,
310+ URLEncoder .encode (getLocalServiceName (), "UTF-8" ),
311+ URLEncoder .encode (tsfEventReporterConfig .getInstanceId (), "UTF-8" ));
312+ v1EventUri = new URIBuilder ()
313+ .setScheme ("http" )
314+ .setHost (IPAddressUtils .getIpCompatible (tsfEventReporterConfig .getEventMasterIp ()))
315+ .setPort (tsfEventReporterConfig .getEventMasterPort ())
316+ .setPath (v1Path )
317+ .setParameter ("token" , tsfEventReporterConfig .getToken ())
318+ .build ();
319+ LOG .info ("Tsf v1 event reporter init with uri: {}" , v1EventUri );
295320 } catch (URISyntaxException | UnsupportedEncodingException e ) {
296321 LOG .error ("Build event request url fail." , e );
297322 }
323+ eventUriInit = true ;
298324 }
299325 }
300326 }
@@ -331,6 +357,7 @@ private void postV1Event(TsfGenericEvent genericEvent) {
331357 StringEntity postBody = null ;
332358 RequestConfig config = RequestConfig .custom ().setConnectTimeout (2000 ).setConnectionRequestTimeout (10000 ).setSocketTimeout (10000 ).build ();
333359 try (CloseableHttpClient httpClient = HttpClientBuilder .create ().setDefaultRequestConfig (config ).build ()) {
360+ initEventUri ();
334361 HttpPost httpPost = new HttpPost (v1EventUri );
335362 postBody = new StringEntity (gson .toJson (genericEvent ));
336363 httpPost .setEntity (postBody );
0 commit comments