diff --git a/.changeset/config.json b/.changeset/config.json index 66c1704..2be13d4 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -1,10 +1,10 @@ { "$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json", "changelog": "@changesets/cli/changelog", - "commit": true, + "commit": false, "fixed": [], "linked": [], - "access": "restricted", + "access": "public", "baseBranch": "main", "updateInternalDependencies": "patch", "ignore": [] diff --git a/.changeset/v0-tracing-rewrite.md b/.changeset/v0-tracing-rewrite.md new file mode 100644 index 0000000..4123664 --- /dev/null +++ b/.changeset/v0-tracing-rewrite.md @@ -0,0 +1,8 @@ +--- +"react-native-open-telemetry": minor +--- + +v0: a rewrite around one trace across JS and native. Native-first init (`RNOtel.start` on Android and iOS) with a JS +fallback (`startReactNativeSdk`), a context patch that carries the active span through `await`, promises and timers, +and a `withOtel` helper for Metro. Removed (breaking): metrics, fetch instrumentation, session tagging and +the `openTelemetrySDK` init. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c234bc4..38e4a2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,20 +40,15 @@ jobs: java-version: '17' - run: bun install --frozen-lockfile - run: bun run prepare - - run: bun verify # every non-e2e stage (R17) + - run: bun verify - uses: actions/upload-artifact@v4 if: always() with: name: verify-artifacts-static-fast path: verify/artifacts/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true - # Release builds with the JS bundle embedded (no Metro), test mode on; the bare jobs also build test mode off for - # smoke-device (per PR on bare only, X11 interim; Expo smoke goes to the nightly). perf-budget runs on android-bare. - # Every Task 27 failure scenario runs here except fast-refresh-idempotent: it needs dev builds + Metro, so it is - # local only (verify/VERIFY.md), no per-PR jobs; a nightly can add it. - # Native outputs are cached on native inputs only, so a JS-only change re-bundles but reuses the native build. e2e-android-bare: runs-on: ubuntu-latest timeout-minutes: 45 @@ -69,7 +64,7 @@ jobs: - run: bun install --frozen-lockfile - run: bun install --frozen-lockfile working-directory: example - - run: bun run prepare # the example bundles the package through exports -> lib/ (R4) + - run: bun run prepare - name: Enable KVM run: | echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules @@ -89,19 +84,10 @@ jobs: example/node_modules/react-native-nitro-modules/android/build example/node_modules/react-native-nitro-modules/android/.cxx key: android-native-${{ hashFiles('android/**', 'nitrogen/**', 'patches/**', 'example/android/**', 'example/modules/**', 'bun.lock', 'example/bun.lock') }} - # smoke-device (R20, X11): the production pipeline needs a test-mode-off build. Built first, so the test-mode - # build is what the native outputs cache keeps; the switch recompiles only Kotlin and the bundle. - - name: Build release APK (smoke, test mode off) - working-directory: example/android - run: | - ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64 || - ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64 - cp app/build/outputs/apk/release/app-release.apk '${{ runner.temp }}/smoke.apk' - name: Build release APK (test mode) working-directory: example/android env: OTEL_TEST_MODE: '1' - # one retry: plugin and NDK downloads fail transiently on hosted runners run: | ./gradlew :app:assembleRelease -PotelTestMode=true -PreactNativeArchitectures=x86_64 || ./gradlew :app:assembleRelease -PotelTestMode=true -PreactNativeArchitectures=x86_64 @@ -125,7 +111,7 @@ jobs: disable-animations: false script: echo "AVD snapshot created" - name: e2e (android-bare) - timeout-minutes: 25 # a hung device run fails here, not at the job limit + timeout-minutes: 25 uses: reactivecircus/android-emulator-runner@v2 env: E2E_TARGET: android-bare @@ -138,18 +124,16 @@ jobs: disable-animations: true script: | adb shell settings put global hide_error_dialogs 1 - # one line (the runner runs each line in its own shell and stops at the first failure): smoke runs even - # when the main e2e failed, and the step exits with the first non-zero code - bun verify --e2e e2e; a=$?; E2E_SMOKE=1 E2E_ANDROID_APP='${{ runner.temp }}/smoke.apk' bun verify --e2e e2e; b=$?; exit $(( a ? a : b )) + bun verify --e2e e2e - uses: actions/upload-artifact@v4 - if: always() # e2e retries once; keep traces of retried and failed attempts + if: always() with: name: verify-artifacts-e2e-android-bare path: | verify/artifacts/ verify/e2e/.e2e/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true e2e-ios-bare: runs-on: xcode-27 @@ -165,7 +149,7 @@ jobs: - run: bun install --frozen-lockfile - run: bun install --frozen-lockfile working-directory: example - - run: bun run prepare # the example bundles the package through exports -> lib/ (R4) + - run: bun run prepare - name: Cache Swift packages uses: actions/cache@v4 with: @@ -178,19 +162,6 @@ jobs: example/ios/Pods example/ios/build/Build key: ios-native-${{ hashFiles('ios/**', 'nitrogen/**', 'ReactNativeOpenTelemetry.podspec', 'ReactNativeOpenTelemetryCore.podspec', 'example/ios/Podfile', 'example/ios/Podfile.lock', 'example/ios/OpenTelemetryExample/**', 'example/ios/OpenTelemetryExample.xcodeproj/**', 'example/modules/**', 'bun.lock', 'example/bun.lock') }} - # smoke-device (R20, X11): a test-mode-off build, first, so the cache keeps the test-mode one; the switch - # recompiles only the core pod, the adapter and the app. - - name: pod install (smoke, test mode off) - working-directory: example/ios - run: pod install - - name: Build release app (smoke, test mode off) - id: ios-smoke-build - working-directory: example/ios - run: | - xcodebuild -workspace OpenTelemetryExample.xcworkspace -scheme OpenTelemetryExample \ - -configuration Release -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' \ - -derivedDataPath build ARCHS=arm64 build - cp -R build/Build/Products/Release-iphonesimulator/OpenTelemetryExample.app '${{ runner.temp }}/' - name: pod install (test mode) working-directory: example/ios env: @@ -210,23 +181,14 @@ jobs: xcrun simctl boot "$udid" xcrun simctl bootstatus "$udid" -b - name: e2e (ios-bare) - timeout-minutes: 30 # a hung device run fails here, not at the job limit + timeout-minutes: 30 env: E2E_TARGET: ios-bare - AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS: '600000' # the first daemon start on a cold runner outlasts the 120 s default - run: bun verify --e2e e2e - - name: e2e smoke (ios-bare, test mode off) - timeout-minutes: 10 # a hung device run fails here, not at the job limit - # also after a failed main e2e (that step already fails the job) - if: ${{ !cancelled() && steps.ios-smoke-build.outcome == 'success' }} - env: - E2E_TARGET: ios-bare - E2E_SMOKE: '1' AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS: '600000' - run: E2E_IOS_APP='${{ runner.temp }}/OpenTelemetryExample.app' bun verify --e2e e2e + run: bun verify --e2e e2e - name: Diagnose the simulator log if: failure() - timeout-minutes: 4 # `log show` scans the whole store: ~1 min per call on a runner + timeout-minutes: 4 run: | echo "--- RNOtel lines"; xcrun simctl spawn booted log show --last 10m --info --style compact --predicate 'eventMessage CONTAINS "RNOtel"' | tail -20 echo "--- app process lines"; xcrun simctl spawn booted log show --last 10m --info --style compact --predicate 'process == "OpenTelemetryExample"' | tail -20 @@ -239,18 +201,15 @@ jobs: if-no-files-found: ignore include-hidden-files: true - uses: actions/upload-artifact@v4 - if: always() # e2e retries once; keep traces of retried and failed attempts + if: always() with: name: verify-artifacts-e2e-ios-bare path: | verify/artifacts/ verify/e2e/.e2e/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true - # The Expo app (Task 28): JS-fallback init, CNG (`expo prebuild` every run; android/ and ios/ are not committed), - # same scenarios and the same android/ios expected files as the bare app. Test mode: OTEL_TEST_MODE=1 at prebuild - # (the local config plugin writes otelTestMode=true to gradle.properties) and at pod install. e2e-android-expo: runs-on: ubuntu-latest timeout-minutes: 45 @@ -266,7 +225,7 @@ jobs: - run: bun install --frozen-lockfile - run: bun install --frozen-lockfile working-directory: example-expo - - run: bun run prepare # the app bundles the package through exports -> lib/ (R4) + - run: bun run prepare - name: expo prebuild (test mode) working-directory: example-expo env: @@ -280,7 +239,7 @@ jobs: - uses: gradle/actions/setup-gradle@v4 - name: Enable the Gradle build cache (setup-gradle persists it) run: printf '\norg.gradle.caching=true\n' >> example-expo/android/gradle.properties - - name: Cache native build outputs # after prebuild, which regenerates android/ + - name: Cache native build outputs uses: actions/cache@v4 with: path: | @@ -295,7 +254,6 @@ jobs: working-directory: example-expo/android env: OTEL_TEST_MODE: '1' - # one retry: plugin and NDK downloads fail transiently on hosted runners run: | ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64 || ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64 @@ -319,7 +277,7 @@ jobs: disable-animations: false script: echo "AVD snapshot created" - name: e2e (android-expo) - timeout-minutes: 25 # a hung device run fails here, not at the job limit + timeout-minutes: 25 uses: reactivecircus/android-emulator-runner@v2 env: E2E_TARGET: android-expo @@ -334,14 +292,14 @@ jobs: adb shell settings put global hide_error_dialogs 1 bun verify --e2e e2e - uses: actions/upload-artifact@v4 - if: always() # e2e retries once; keep traces of retried and failed attempts + if: always() with: name: verify-artifacts-e2e-android-expo path: | verify/artifacts/ verify/e2e/.e2e/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true e2e-ios-expo: runs-on: xcode-27 @@ -357,11 +315,11 @@ jobs: - run: bun install --frozen-lockfile - run: bun install --frozen-lockfile working-directory: example-expo - - run: bun run prepare # the app bundles the package through exports -> lib/ (R4) + - run: bun run prepare - name: expo prebuild working-directory: example-expo run: bunx expo prebuild --clean --no-install --platform ios - - name: Cache native build outputs # after prebuild, which regenerates ios/ + - name: Cache native build outputs uses: actions/cache@v4 with: path: | @@ -388,10 +346,10 @@ jobs: xcrun simctl boot "$udid" xcrun simctl bootstatus "$udid" -b - name: e2e (ios-expo) - timeout-minutes: 30 # a hung device run fails here, not at the job limit + timeout-minutes: 30 env: E2E_TARGET: ios-expo - AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS: '600000' # the first daemon start on a cold runner outlasts the 120 s default + AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS: '600000' run: bun verify --e2e e2e - uses: actions/upload-artifact@v4 if: failure() @@ -401,17 +359,15 @@ jobs: if-no-files-found: ignore include-hidden-files: true - uses: actions/upload-artifact@v4 - if: always() # e2e retries once; keep traces of retried and failed attempts + if: always() with: name: verify-artifacts-e2e-ios-expo path: | verify/artifacts/ verify/e2e/.e2e/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true - # The Expo app's Metro web export (react-native-web) and the web SDK, headless Chromium. - # The runner exports the app in test mode and serves it (e2e.config.ts `command`). e2e-web: runs-on: ubuntu-latest timeout-minutes: 15 @@ -423,7 +379,7 @@ jobs: - run: bun install --frozen-lockfile - run: bun install --frozen-lockfile working-directory: example-expo - - run: bun run prepare # the web app resolves the package through exports -> lib/ (browser condition) + - run: bun run prepare - run: bunx e2e-web install chromium --with-deps - run: bun verify --e2e e2e env: @@ -436,10 +392,10 @@ jobs: verify/artifacts/ verify/e2e/.e2e/ if-no-files-found: ignore - include-hidden-files: true # verify/e2e/.e2e is a dot dir + include-hidden-files: true budget: - needs: [static-fast, e2e-android-bare, e2e-ios-bare, e2e-android-expo, e2e-ios-expo, e2e-web] # every other job; extend when jobs are added + needs: [static-fast, e2e-android-bare, e2e-ios-bare, e2e-android-expo, e2e-ios-expo, e2e-web] if: always() runs-on: ubuntu-latest timeout-minutes: 5 diff --git a/.gitignore b/.gitignore index 84d2f30..3c7d885 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,5 @@ android/generated .superpowers/ verify/artifacts/ verify/e2e/.e2e/ + +ios/RNOtel/PackageVersion.swift diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 32303fa..5573533 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -77,9 +77,14 @@ We follow the [conventional commits specification](https://www.conventionalcommi We use [TypeScript](https://www.typescriptlang.org/) for type checking and [OXLint](https://oxc.rs/docs/guide/usage/linter.html) for linting and formatting the code. -### Publishing to npm 🚧 +### Publishing to npm -We use [changesets](https://github.com/changesets/changesets) for managing versions, though this is a manual process for now in the experimental phase. +We use [changesets](https://github.com/changesets/changesets). Releases are manual: + +1. Add a changeset (`bunx changeset`) in your PR. +2. To release, check that CI is green on `main`, then run `bun run version` on `main` + (bumps `package.json` and writes `CHANGELOG.md`), review and commit it. +3. `bun run publish` (`changeset publish`) builds through `prepare` and publishes to npm. ### Scripts diff --git a/README.md b/README.md index 12c0062..86c8c5d 100644 --- a/README.md +++ b/README.md @@ -9,96 +9,171 @@ Best in class observability brought to React Native. * **Telemetry without lock-in**. Send to **any OTLP compliant backend** including [vendors](https://opentelemetry.io/ecosystem/vendors/) * [**Tracing** API](https://opentelemetry.io/docs/specs/otel/trace/api/) -* [**Metrics** API](https://opentelemetry.io/docs/specs/otel/metrics/api/) -* **Web**, **Android** (experimental) support with **iOS** coming soon -* Automatic **fetch instrumentation** -* Automatic **session tagging** +* **One trace across JS and Native**, exported together +* Context carried through `await`, promises and timers +* **Web**, **Android** and **iOS** support ## Platform support | Platform | Support | | --- | --- | | **web** | β˜‘οΈ Stable | -| **android** | β˜‘οΈ Stable JS with experimental Native πŸ—οΈ | -| **ios** | β˜‘οΈ Stable JS with planned Native πŸ‘€ | +| **android** | β˜‘οΈ Stable JS and Native | +| **ios** | β˜‘οΈ Stable JS and Native | + +Requires React Native **0.88+** (New Architecture, Hermes), Xcode **27+** and Android minSdk **26**. ## Installation ```sh -npm install react-native-open-telemetry +npm install react-native-open-telemetry react-native-nitro-modules +``` + +**iOS** β€” add the core pod to your app target in `ios/Podfile`, then run `pod install`: + +```ruby +pod 'ReactNativeOpenTelemetryCore', :path => '../node_modules/react-native-open-telemetry' +``` + +**Expo** β€” use the config plugin instead, it adds that line for you: + +```json +{ "expo": { "plugins": ["react-native-open-telemetry"] } } +``` + +**Metro** β€” wrap the merged config, so context tracking starts before any of your code: + +```js +const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config'); // Expo: expo/metro-config +const {withOtel} = require('react-native-open-telemetry/metro'); + +module.exports = withOtel(mergeConfig(getDefaultConfig(__dirname), {})); +``` + +On **Expo web**, also lower `async` in `babel.config.js`, otherwise spans lose their parent after `await`: + +```js +module.exports = api => { + const web = api.caller(c => c?.platform === 'web'); + return { + presets: ['babel-preset-expo'], + plugins: web ? ['@babel/plugin-transform-async-generator-functions', '@babel/plugin-transform-async-to-generator'] : [], + }; +}; ``` ## Usage -### 1. Start the JS SDK +### 1. Start the native SDK (recommended) + +Start in native code, before React Native, so you can trace from the very start of the app. It never throws: on bad config it logs an error and telemetry stays off. + +See OpenTelemetry's [Java](https://opentelemetry.io/docs/languages/java/api/#tracerprovider) and [Swift](https://opentelemetry.io/docs/languages/swift/) documentation for more information on available APIs. + +**Android** + +```kt +import com.callstack.otel.RNOtel + +class MainApplication : Application(), ReactApplication { + override fun onCreate() { + super.onCreate() -See OpenTelemetry's JS documentation on [Traces](https://opentelemetry.io/docs/languages/js/instrumentation/#acquiring-a-tracer) and [Metrics](https://opentelemetry.io/docs/languages/js/instrumentation/#acquiring-a-meter) for more information on available APIs. + // Start the SDK, before loadReactNative(this) + RNOtel.start(this) { + serviceName = "my-app" + serviceVersion = "1.0.0" + resourceAttributes = mapOf("deployment.environment.name" to "production") + export { + url = "https://my-collector.com:4318/v1/traces" + headers = mapOf("x-api-key" to KEY) + } + } + + // Use available APIs + val span = RNOtel.tracer("native-scope-name").spanBuilder("native-work").startSpan() + span.end() + } +} +``` + +**iOS** + +```swift +import ReactNativeOpenTelemetryCore + +// AppDelegate, application(_:didFinishLaunchingWithOptions:), before React Native starts +RNOtel.start { c in + c.serviceName = "my-app" + c.serviceVersion = "1.0.0" + c.export = .init(url: "https://my-collector.com:4318/v1/traces", headers: ["x-api-key": KEY]) +} +``` + +### 2. Start the JS SDK + +Call it in `index.js`, before the app is registered. When native already started, it attaches to it and `exportConfig` is ignored. Otherwise (web, or an Expo app without native code) it starts everything itself. + +See OpenTelemetry's JS documentation on [Traces](https://opentelemetry.io/docs/languages/js/instrumentation/#acquiring-a-tracer) for more information on available APIs. ```js -import { openTelemetrySDK } from 'react-native-open-telemetry'; +import { trace } from '@opentelemetry/api'; +import { startReactNativeSdk } from 'react-native-open-telemetry'; // Start the SDK -const sdk = openTelemetrySDK({ - debug: true, - url: "https://my-collector.com:4317", - name: "my-app", - version: "1.0.0-alpha", - environment: "development", +startReactNativeSdk({ + serviceName: 'my-app', + serviceVersion: '1.0.0', + resourceAttributes: { 'deployment.environment.name': 'production' }, + exportConfig: { url: 'https://my-collector.com:4318/v1/traces', headers: { 'x-api-key': KEY } }, }); // Use available APIs -const meter = sdk.metrics.getMeter("my-js-meter", "1.0"); - -const promoCounter = meter.createCounter("my-promo-counter", { - description: "A counter metric for my promo section" -}); +const tracer = trace.getTracer('my-js-tracer', '1.0'); function App() { - function onPress() { - promoCounter.add(1); + async function onPress() { + await tracer.startActiveSpan('promo-press', async span => { + await fetchPromo(); // spans started in here keep their parent + span.end(); + }); } return