Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 274 additions & 14 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions example-expo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CNG: generated by `expo prebuild`
/android
/ios
.expo/
10 changes: 10 additions & 0 deletions example-expo/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"expo": {
"name": "OpenTelemetryExpo",
"slug": "opentelemetry-example-expo",
"version": "1.0.0",
"ios": {"bundleIdentifier": "opentelemetry.example.expo"},
"android": {"package": "opentelemetry.example.expo"},
"plugins": ["react-native-open-telemetry", "./plugins/withOtelExample"]
}
}
7 changes: 7 additions & 0 deletions example-expo/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// The bare example's inline OTEL_TEST_MODE plugin (test mode is a bundle-time flag).
const inlineTestMode = require('../example/inlineTestMode');

module.exports = {
presets: ['babel-preset-expo'],
plugins: [inlineTestMode],
};
1,025 changes: 1,025 additions & 0 deletions example-expo/bun.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions example-expo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {registerRootComponent} from 'expo';
import {Platform} from 'react-native';
import {startReactNativeSdk} from 'react-native-open-telemetry';
import {ScenariosScreen} from '../example/src/ScenariosScreen';

// JS-fallback init: no native init in this app, so this call initializes native from the config (spec §4.4).
// No serviceVersion: the test-mode resource is service.name only (R2). Same collector and headers as the bare app.
startReactNativeSdk({
serviceName: 'verify',
exportConfig: {
url: Platform.OS === 'android' ? 'http://10.0.2.2:4318' : 'http://localhost:4318',
headers: {'x-api-key': 'test'}, // asserted by verify/e2e/export-headers.e2e.ts
},
});

registerRootComponent(ScenariosScreen);
25 changes: 25 additions & 0 deletions example-expo/metro.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');
const {getDefaultConfig} = require('expo/metro-config');
const {mergeConfig} = require('@expo/metro/metro-config');
const {withOtel} = require('../metro');

const root = path.resolve(__dirname, '..');
const own = p => path.join(__dirname, 'node_modules', p);
// The repo root and the bare example have their own copies of these; the app must use exactly one.
const single = ['react', 'react-native', 'react-native-nitro-modules', '@react-native'];
const escape = s => s.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
const block = dir => single.map(m => new RegExp(`^${escape(path.join(dir, 'node_modules', m))}[/\\\\].*`));

const config = {
// babel.config.js inlines OTEL_TEST_MODE; keep test-mode and normal transforms in separate caches
cacheVersion: `otel-test-mode=${process.env.OTEL_TEST_MODE ?? ''}`,
// the library (exports -> lib/), the bare example's ScenariosScreen and verify/
watchFolders: [root],
resolver: {
nodeModulesPaths: [path.join(__dirname, 'node_modules'), path.join(root, 'node_modules')],
blockList: [...block(root), ...block(path.join(root, 'example'))],
extraNodeModules: {...Object.fromEntries(single.map(m => [m, own(m)])), 'react-native-open-telemetry': root},
},
};

module.exports = withOtel(mergeConfig(getDefaultConfig(__dirname), config));
16 changes: 16 additions & 0 deletions example-expo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "react-native-open-telemetry-example-expo",
"version": "0.0.1",
"private": true,
"main": "index.js",
"scripts": {
"postinstall": "test -e node_modules/react-native-open-telemetry || ln -sfn ../.. node_modules/react-native-open-telemetry",
"prebuild": "expo prebuild --clean --no-install"
},
"dependencies": {
"expo": "58.0.26",
"react": "19.3.0",
"react-native": "0.88.1",
"react-native-nitro-modules": "0.37.2"
}
}
36 changes: 36 additions & 0 deletions example-expo/plugins/withOtelExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const {withAndroidManifest, withAppBuildGradle, withGradleProperties} = require('expo/config-plugins');

// Same as the bare example's app/build.gradle: the bundle task sees only this app's dir, so a change to the package
// (lib/, src/), the scenarios or OTEL_TEST_MODE would otherwise leave the old bundle in the APK.
const BUNDLE_INPUTS = `
// withOtelExample: rebundle when lib/, src/, verify/ or OTEL_TEST_MODE change
tasks.matching { it.name.startsWith("createBundle") }.configureEach {
inputs.property("otelTestMode", System.getenv("OTEL_TEST_MODE") ?: "")
inputs.files(fileTree("../../..") { include "lib/**", "src/**", "verify/scenarios/**", "verify/harness/*.ts" })
}
`;

/**
* Example-only (Task 28): what the bare example's native projects carry by hand, applied at `expo prebuild`. The Core
* pod (R24) comes from the package's own plugin (`react-native-open-telemetry` in app.json), as in a real Expo app.
*/
module.exports = config => {
config = withAppBuildGradle(config, c => {
if (!c.modResults.contents.includes('withOtelExample: rebundle')) c.modResults.contents += BUNDLE_INPUTS;
return c;
});
// EXAMPLE-ONLY, DO NOT COPY: app-wide cleartext so the e2e collector (plain http, also with test mode off) is reachable.
// ponytail: any host, not only 10.0.2.2 like the bare app's network_security_config. None of this file is what a real
// Expo app needs; it needs only the package's plugin.
config = withAndroidManifest(config, c => {
c.modResults.manifest.application[0].$['android:usesCleartextTraffic'] = 'true';
return c;
});
// Test mode is a build flag: `OTEL_TEST_MODE=1 expo prebuild` (Android); iOS takes it at `OTEL_TEST_MODE=1 pod install`.
if (process.env.OTEL_TEST_MODE === '1')
config = withGradleProperties(config, c => {
c.modResults.push({type: 'property', key: 'otelTestMode', value: 'true'});
return c;
});
return config;
};
9 changes: 9 additions & 0 deletions example-expo/react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require('path');

// Autolink the library from the repo root and the bare example's TestNative module (no npm dependency needed).
module.exports = {
dependencies: {
'react-native-open-telemetry': {root: path.join(__dirname, '..')},
'react-native-test-native': {root: path.join(__dirname, '../example/modules/test-native')},
},
};
9 changes: 9 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ android {
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
// test mode is a build flag (-PotelTestMode=true); MainApplication reads test-only overrides only then
buildConfigField "boolean", "OTEL_TEST_MODE", ((findProperty("otelTestMode") ?: "false").toString() == "true").toString()
}
signingConfigs {
debug {
Expand All @@ -107,6 +109,13 @@ android {
}
}

// The bundle task's inputs are example/ only. It also bundles the package (lib/, src/) and the verify scenarios, and
// babel.config.js inlines OTEL_TEST_MODE: without these, a change there or a test-mode switch reuses the old bundle.
tasks.matching { it.name.startsWith("createBundle") }.configureEach {
inputs.property("otelTestMode", System.getenv("OTEL_TEST_MODE") ?: "")
inputs.files(fileTree("../../..") { include "lib/**", "src/**", "verify/scenarios/**", "verify/harness/*.ts" })
}

dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MainApplication : Application(), ReactApplication {

override fun onCreate() {
super.onCreate()
startOtel() // native-first init, before React Native (OtelInit.kt)
loadReactNative(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package opentelemetry.example

import android.app.Application
import com.callstack.otel.RNOtel
import java.io.File

/**
* Native-first init, called from MainApplication.onCreate before React Native. Its own file so the nightly
* (verify/nightly/app.sh) can add it unchanged to a freshly generated app at another RN version.
* No serviceVersion: the test-mode resource is service.name only (R2).
*/
internal fun Application.startOtel() {
RNOtel.start(this) {
serviceName = "verify"
export {
url = testExportUrl() ?: "http://10.0.2.2:4318" // the e2e collector on the host
headers = mapOf("x-api-key" to "test") // asserted by verify/e2e/export-headers.e2e.ts
}
}
// compile check: the otel API is part of RNOtel's public surface (a no-op tracer is cheap to build)
RNOtel.tracer("opentelemetry.example")
}

/**
* Test-mode builds only: verify/e2e/native-init-failure.e2e.ts overrides the URL. Application.onCreate cannot see
* launch arguments (they reach the Activity), so the test writes this file with adb and removes it afterwards.
*/
private fun Application.testExportUrl(): String? =
if (!BuildConfig.OTEL_TEST_MODE) null
else File(getExternalFilesDir(null), "otel-export-url").takeIf { it.exists() }?.readText()?.trim()
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
buildToolsVersion = "37.0.0"
minSdkVersion = 24
minSdkVersion = 26
compileSdkVersion = 37
targetSdkVersion = 36
ndkVersion = "27.1.12297006"
Expand Down
9 changes: 1 addition & 8 deletions example/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
// Test mode is a build flag: inline process.env.OTEL_TEST_MODE at bundle time (e2e builds set it to 1).
const inlineTestMode = ({types: t}) => {
const visit = p => {
if (p.get('object').matchesPattern('process.env') && t.isIdentifier(p.node.property, {name: 'OTEL_TEST_MODE'}))
p.replaceWith(process.env.OTEL_TEST_MODE == null ? t.identifier('undefined') : t.stringLiteral(process.env.OTEL_TEST_MODE));
};
return {visitor: {MemberExpression: visit, OptionalMemberExpression: visit}};
};
const inlineTestMode = require('./inlineTestMode');

module.exports = {
presets: ['module:@react-native/babel-preset'],
Expand Down
Loading