@@ -232,26 +232,85 @@ static ResourceAttributes GetMetadataResourceAttributes(const json& info) {
232232 return attrs;
233233}
234234
235- static void InheritServiceNameIfMissing (ResourceAttributes* attrs,
236- const Resource& resource) {
237- auto resource_attributes = resource.GetAttributes ();
235+ static std::shared_ptr<Resource> MergeResourceAttributes (
236+ const std::shared_ptr<Resource>& base,
237+ ResourceAttributes attrs) {
238+ auto resource_attributes = base->GetAttributes ();
238239 if (resource_attributes.find (kServiceName ) != resource_attributes.end () &&
239- attrs-> find (kServiceName ) == attrs-> end ()) {
240- attrs-> SetAttribute (
240+ attrs. find (kServiceName ) == attrs. end ()) {
241+ attrs. SetAttribute (
241242 kServiceName ,
242243 opentelemetry::nostd::get<std::string>(
243244 resource_attributes[kServiceName ]));
244245 }
245- }
246-
247- static std::shared_ptr<Resource> MergeResourceAttributes (
248- const std::shared_ptr<Resource>& base,
249- ResourceAttributes attrs) {
250- InheritServiceNameIfMissing (&attrs, *base);
251246 auto overlay = std::make_shared<Resource>(Resource::Create (attrs));
252247 return std::make_shared<Resource>(base->Merge (*overlay));
253248}
254249
250+ InstrumentationScope* GetScope () {
251+ static std::unique_ptr<InstrumentationScope> scope =
252+ InstrumentationScope::Create (" nsolid" , NODE_VERSION " +ns" NSOLID_VERSION );
253+ return scope.get ();
254+ }
255+
256+ static void EnsureResourceInitializedLocked () {
257+ if (resource_g != nullptr ) return ;
258+
259+ json config = json::parse (nsolid::GetConfig (), nullptr , false );
260+ // assert because the runtime should never send me an invalid JSON config
261+ ASSERT (!config.is_discarded ());
262+ auto it = config.find (" app" );
263+ ASSERT (it != config.end ());
264+ ResourceAttributes attrs ({
265+ {kServiceName , it->get <std::string>()},
266+ {kServiceInstanceId , nsolid::GetAgentId ()}
267+ });
268+
269+ it = config.find (" appVersion" );
270+ if (it != config.end ()) {
271+ attrs.SetAttribute (kServiceVersion , it->get <std::string>());
272+ }
273+
274+ resource_g = std::make_shared<Resource>(Resource::Create (attrs));
275+ }
276+
277+ std::shared_ptr<Resource> GetResource () {
278+ nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
279+ EnsureResourceInitializedLocked ();
280+ return resource_g;
281+ }
282+
283+ static void EnsureMetricsResourceInitializedLocked () {
284+ if (metrics_resource_g != nullptr ) return ;
285+
286+ EnsureResourceInitializedLocked ();
287+
288+ json info = json::parse (nsolid::GetProcessInfo (), nullptr , false );
289+ ResourceAttributes attrs = GetMetadataResourceAttributes (info);
290+ metrics_resource_g = MergeResourceAttributes (resource_g, std::move (attrs));
291+ }
292+
293+ std::shared_ptr<Resource> GetMetricsResource () {
294+ nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
295+ EnsureMetricsResourceInitializedLocked ();
296+ return metrics_resource_g;
297+ }
298+
299+ std::shared_ptr<Resource> UpdateResource (ResourceAttributes&& attrs) {
300+ nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
301+ EnsureResourceInitializedLocked ();
302+
303+ ResourceAttributes metrics_attrs (attrs);
304+ resource_g = MergeResourceAttributes (resource_g, std::move (attrs));
305+
306+ if (metrics_resource_g != nullptr ) {
307+ metrics_resource_g = MergeResourceAttributes (metrics_resource_g,
308+ std::move (metrics_attrs));
309+ }
310+
311+ return resource_g;
312+ }
313+
255314// NOLINTNEXTLINE(runtime/references)
256315static void add_counter (std::vector<MetricData>& metrics,
257316 const time_point& start,
@@ -317,70 +376,6 @@ static void add_summary(std::vector<MetricData>& metrics,
317376 metrics.push_back (metric_data);
318377}
319378
320- InstrumentationScope* GetScope () {
321- static std::unique_ptr<InstrumentationScope> scope =
322- InstrumentationScope::Create (" nsolid" , NODE_VERSION " +ns" NSOLID_VERSION );
323- return scope.get ();
324- }
325-
326- static void EnsureResourceInitializedLocked () {
327- if (resource_g != nullptr ) return ;
328-
329- json config = json::parse (nsolid::GetConfig (), nullptr , false );
330- // assert because the runtime should never send me an invalid JSON config
331- ASSERT (!config.is_discarded ());
332- auto it = config.find (" app" );
333- ASSERT (it != config.end ());
334- ResourceAttributes attrs ({
335- {kServiceName , it->get <std::string>()},
336- {kServiceInstanceId , nsolid::GetAgentId ()}
337- });
338-
339- it = config.find (" appVersion" );
340- if (it != config.end ()) {
341- attrs.SetAttribute (kServiceVersion , it->get <std::string>());
342- }
343-
344- resource_g = std::make_shared<Resource>(Resource::Create (attrs));
345- }
346-
347- std::shared_ptr<Resource> GetResource () {
348- nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
349- EnsureResourceInitializedLocked ();
350- return resource_g;
351- }
352-
353- static void EnsureMetricsResourceInitializedLocked () {
354- if (metrics_resource_g != nullptr ) return ;
355-
356- EnsureResourceInitializedLocked ();
357-
358- json info = json::parse (nsolid::GetProcessInfo (), nullptr , false );
359- ResourceAttributes attrs = GetMetadataResourceAttributes (info);
360- metrics_resource_g = MergeResourceAttributes (resource_g, std::move (attrs));
361- }
362-
363- std::shared_ptr<Resource> GetMetricsResource () {
364- nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
365- EnsureMetricsResourceInitializedLocked ();
366- return metrics_resource_g;
367- }
368-
369- std::shared_ptr<Resource> UpdateResource (ResourceAttributes&& attrs) {
370- nsuv::ns_mutex::scoped_lock lock (ResourceMutex ());
371- EnsureResourceInitializedLocked ();
372-
373- ResourceAttributes metrics_attrs (attrs);
374- resource_g = MergeResourceAttributes (resource_g, std::move (attrs));
375-
376- if (metrics_resource_g != nullptr ) {
377- metrics_resource_g = MergeResourceAttributes (metrics_resource_g,
378- std::move (metrics_attrs));
379- }
380-
381- return resource_g;
382- }
383-
384379// NOLINTNEXTLINE(runtime/references)
385380void fill_proc_metrics (std::vector<MetricData>& metrics,
386381 const ProcessMetrics::MetricsStor& stor,
0 commit comments