Skip to content

Commit 0da8b8e

Browse files
authored
fix: set correct global attributes (signalfx#68)
1 parent 18cd25f commit 0da8b8e

9 files changed

Lines changed: 48 additions & 31 deletions

File tree

android/src/main/java/com/splunkotelreactnative/SplunkOtelReactNativeModule.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void initialize(ReadableMap configMap, Promise promise) {
9595
exporter = createExporter(endpointWithAuthentication, getReactApplicationContext(),
9696
mapReader.getEnableDiskBuffering(), mapReader.getMaxStorageUseMb());
9797

98-
crashReporter = new CrashReporter(exporter,
98+
crashReporter = new CrashReporter(exporter, currentNetworkProvider,
9999
attributesFromMap(mapReader.getGlobalAttributes()), getReactApplicationContext());
100100

101101
crashReporter.install();
@@ -134,7 +134,6 @@ public void export(ReadableArray spanMaps, Promise promise) {
134134
CurrentNetwork network = currentNetworkProvider.refreshNetworkStatus();
135135
CurrentNetworkAttributesExtractor networkAttributesExtractor = new CurrentNetworkAttributesExtractor();
136136
Attributes networkAttributes = networkAttributesExtractor.extract(network);
137-
setGlobalAttributes(networkAttributes);
138137

139138
for (int i = 0; i < spanMaps.size(); i++) {
140139
ReadableMap spanMap = spanMaps.getMap(i);

android/src/main/java/com/splunkotelreactnative/crash/CrashReporter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.splunkotelreactnative.crash;
22

33
import android.content.Context;
4+
import android.util.Log;
45

56
import com.splunkotelreactnative.ReactSpanData;
67
import com.splunkotelreactnative.ReactSpanProperties;
8+
import com.splunkotelreactnative.exporter.network.CurrentNetwork;
9+
import com.splunkotelreactnative.exporter.network.CurrentNetworkAttributesExtractor;
10+
import com.splunkotelreactnative.exporter.network.CurrentNetworkProvider;
711

812
import java.util.Collections;
913
import java.util.List;
@@ -27,16 +31,18 @@
2731

2832
public class CrashReporter {
2933
private final SpanExporter exporter;
34+
private final CurrentNetworkProvider currentNetworkProvider;
3035
private volatile Attributes globalAttributes;
3136
private volatile String sessionId;
3237
private final RuntimeDetailsExtractor runtimeDetailsExtractor;
3338
private final AtomicBoolean crashHappened = new AtomicBoolean(false);
3439
private final AnchoredClock clock;
3540
private final IdGenerator idGenerator;
3641

37-
public CrashReporter(SpanExporter exporter, Attributes globalAttributes, Context context) {
42+
public CrashReporter(SpanExporter exporter, CurrentNetworkProvider currentNetworkProvider, Attributes globalAttributes, Context context) {
3843
this.exporter = exporter;
3944
this.globalAttributes = globalAttributes;
45+
this.currentNetworkProvider = currentNetworkProvider;
4046
this.runtimeDetailsExtractor = RuntimeDetailsExtractor.create(context);
4147
this.clock = AnchoredClock.create(Clock.getDefault());
4248
this.idGenerator = IdGenerator.random();
@@ -113,6 +119,11 @@ private Attributes buildAttributes(ReactSpanProperties properties, Thread thread
113119
: CrashReporterAttributes.COMPONENT_ERROR;
114120
attributes.put(CrashReporterAttributes.COMPONENT_KEY, component);
115121

122+
CurrentNetwork network = currentNetworkProvider.refreshNetworkStatus();
123+
CurrentNetworkAttributesExtractor networkAttributesExtractor = new CurrentNetworkAttributesExtractor();
124+
Attributes networkAttributes = networkAttributesExtractor.extract(network);
125+
attributes.putAll(networkAttributes);
126+
116127
return attributes.build();
117128
}
118129

example/__tests__/e2e/specs/navigation.e2e.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ describe('Navigation', () => {
4747

4848
expect(createSpan).toBeDefined();
4949
expect(createSpan.tags['screen.name']).toBe('Details');
50+
expect(createSpan.tags['app']).toBe('RnExample');
51+
expect(createSpan.tags['device.model.name']).toBeDefined();
5052
});
5153
});

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ PODS:
497497
- RCT-Folly (= 2021.07.22.00)
498498
- React-Core
499499
- SocketRocket (0.6.1)
500-
- splunk-otel-react-native (0.2.0):
500+
- splunk-otel-react-native (0.3.0):
501501
- DeviceKit (~> 4.0)
502502
- PLCrashReporter (~> 1.11)
503503
- React-Core
@@ -739,10 +739,10 @@ SPEC CHECKSUMS:
739739
ReactCommon: 91ece8350ebb3dd2be9cef662abd78b6948233c0
740740
RNScreens: 3c2d122f5e08c192e254c510b212306da97d2581
741741
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
742-
splunk-otel-react-native: b82a0b557bfd9518cd9d36b2e79b04227642bb02
742+
splunk-otel-react-native: a9aa98bc64460b78fb470365cc465cdd7feba82b
743743
Yoga: 86fed2e4d425ee4c6eab3813ba1791101ee153c6
744744
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
745745

746-
PODFILE CHECKSUM: 5009b8cc36e00dbdd65a448c2f8e5dfab9669070
746+
PODFILE CHECKSUM: 43525f2fc52f01068fed659b4b70dfe433901dd5
747747

748748
COCOAPODS: 1.11.3

ios/SpanToDiskExporter.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
*/
1717

1818
import Foundation
19+
import DeviceKit
1920

2021
/*
2122
Copyright 2022 Splunk Inc.
@@ -39,13 +40,15 @@ class SpanToDiskExporter : SpanExporter {
3940
let maxFileSizeBytes: Int64
4041
// Count of spans to insert before checking whether truncation is necessary
4142
let truncationCheckpoint: Int64
43+
let deviceModel: String
4244
private var totalSpansInserted: Int64 = 0
4345
private var checkpointCounter: Int64 = 0
4446

4547
init(spanDb: SpanDb, limitDiskUsageMegabytes: Int64, truncationCheckpoint: Int64) {
4648
self.db = spanDb
4749
self.maxFileSizeBytes = limitDiskUsageMegabytes * 1024 * 1024
4850
self.truncationCheckpoint = truncationCheckpoint
51+
self.deviceModel = Device.current.description
4952
}
5053

5154
public func shutdown() {}
@@ -71,6 +74,10 @@ class SpanToDiskExporter : SpanExporter {
7174
span.tags["splunk.rumSessionId"] = sessionId
7275
}
7376

77+
if span.tags["device.model.name"] == nil {
78+
span.tags["device.model.name"] = self.deviceModel
79+
}
80+
7481
if networkInfo.hostConnectionType != nil {
7582
span.tags["net.host.connection.type"] = networkInfo.hostConnectionType!
7683
}

ios/SplunkOtelReactNative.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SplunkOtelReactNative: NSObject {
2424
let enableDiskBuffering = config["enableDiskBuffering"] as? Bool ?? true
2525
let limitDiskUsageMegabytes = config["limitDiskUsageMegabytes"] as? Int64 ?? 25
2626
let truncationCheckpoint = config["truncationCheckpoint"] as? Int64 ?? 512
27-
27+
2828
let db = SpanDb(enableDiskBuffering: enableDiskBuffering)
2929
spanExporter = SpanToDiskExporter(spanDb: db, limitDiskUsageMegabytes: limitDiskUsageMegabytes, truncationCheckpoint: truncationCheckpoint)
3030
initializeCrashReporting(exporter: spanExporter)
@@ -76,10 +76,15 @@ class SplunkOtelReactNative: NSObject {
7676
updateCrashReportSessionId(id)
7777
resolve(true)
7878
}
79-
79+
8080
@objc(setGlobalAttributes:withResolver:withRejecter:)
8181
func setGlobalAttributes(attributes: Dictionary<String, Any>, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
82-
let newAttribs: [String:String] = attributes.compactMapValues { v in
82+
setGlobalAttributesInternally(attributes: attributes)
83+
resolve(true)
84+
}
85+
86+
private func setGlobalAttributesInternally(attributes: Dictionary<String, Any>) {
87+
let newAttribs: [String: String] = attributes.compactMapValues { v in
8388
switch v {
8489
case is String:
8590
return v as! String
@@ -95,7 +100,6 @@ class SplunkOtelReactNative: NSObject {
95100
}
96101

97102
Globals.setGlobalAttributes(newAttribs)
98-
resolve(true)
99103
}
100104

101105
private func processStartTime() throws -> Date {

ios/ZipkinTransform.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616

1717
import Foundation
18-
import DeviceKit
1918

2019
// Zipkin conversion code modified from https://github.com/open-telemetry/opentelemetry-swift/blob/main/Sources/Exporters/Zipkin/Implementation/ZipkinConversionExtension.swift
2120

@@ -130,8 +129,6 @@ struct ZipkinTransform {
130129
}
131130
}
132131

133-
tags["device.model.name"] = Device.current.description
134-
135132
return ZipkinSpan(traceId: traceId,
136133
parentId: parentId,
137134
id: spanId,

src/native.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const SplunkOtelReactNative = NativeModules.SplunkOtelReactNative
3737
export interface NativeSdKConfiguration {
3838
beaconEndpoint?: string;
3939
rumAccessToken?: string;
40-
globalAttributes?: object;
40+
globalAttributes?: Attributes;
4141
enableDiskBuffering?: boolean;
4242
limitDiskUsageMegabytes?: number;
4343
truncationCheckpoint?: number;

src/splunkRum.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,23 @@ export const SplunkRum: SplunkRumType = {
156156
nativeSdkConf.beaconEndpoint = config.beaconEndpoint;
157157
}
158158

159+
const sessionId = getSessionId();
159160
nativeSdkConf.rumAccessToken = config.rumAccessToken;
160161
nativeSdkConf.enableDiskBuffering = config.enableDiskBuffering;
161162
nativeSdkConf.limitDiskUsageMegabytes = config.limitDiskUsageMegabytes;
162-
nativeSdkConf.globalAttributes = { ...getResource() };
163+
nativeSdkConf.globalAttributes = {
164+
...getResource(),
165+
...config.globalAttributes,
166+
'splunk.rumSessionId': sessionId,
167+
app: config.applicationName,
168+
};
169+
170+
if (config.deploymentEnvironment) {
171+
nativeSdkConf.globalAttributes['deployment.environment'] = config.deploymentEnvironment;
172+
}
173+
// make sure native crashreporter has correct attributes
174+
setGlobalAttributes(nativeSdkConf.globalAttributes);
163175

164-
addGlobalAttributesFromConf(config);
165176
const provider = new WebTracerProvider({});
166177
provider.addSpanProcessor(new GlobalAttributeAppender());
167178
provider.addSpanProcessor(
@@ -196,8 +207,7 @@ export const SplunkRum: SplunkRumType = {
196207
appStartInfo.appStart || appStartInfo.moduleStart;
197208
}
198209
setNativeSessionId(getSessionId());
199-
// make sure native crashreporter has correct attributes
200-
setGlobalAttributes({});
210+
201211
if (config.appStartEnabled) {
202212
const tracer = provider.getTracer('AppStart');
203213
const nativeInitEnd = Date.now();
@@ -238,19 +248,6 @@ export const SplunkRum: SplunkRumType = {
238248
updateLocation: updateLocation,
239249
};
240250

241-
function addGlobalAttributesFromConf(config: ReactNativeConfiguration) {
242-
const confAttributes: Attributes = {
243-
...config.globalAttributes,
244-
};
245-
confAttributes.app = config.applicationName;
246-
247-
if (config.deploymentEnvironment) {
248-
confAttributes['deployment.environment'] = config.deploymentEnvironment;
249-
}
250-
251-
setGlobalAttributes(confAttributes);
252-
}
253-
254251
function updateLocation(latitude: number, longitude: number) {
255252
setGlobalAttributes({
256253
[LOCATION_LATITUDE]: latitude,

0 commit comments

Comments
 (0)