-
Notifications
You must be signed in to change notification settings - Fork 2
feat(android): context sync #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adhorodyski
wants to merge
3
commits into
main
Choose a base branch
from
feat/rn-context-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 23 additions & 7 deletions
30
android/src/main/java/com/opentelemetry/OpenTelemetryModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,33 @@ | ||
| package com.opentelemetry | ||
|
|
||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReadableMap | ||
| import com.facebook.react.module.annotations.ReactModule | ||
| import io.opentelemetry.context.Context | ||
|
|
||
| @ReactModule(name = OpenTelemetryModule.NAME) | ||
| class OpenTelemetryModule(reactContext: ReactApplicationContext) : | ||
| NativeOpenTelemetrySpec(reactContext) { | ||
| NativeOpenTelemetrySpec(reactContext) { | ||
|
|
||
| override fun getName(): String { | ||
| return NAME | ||
| } | ||
| override fun getName(): String { | ||
| return NAME | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "OpenTelemetry" | ||
| } | ||
| override fun setContext(carrier: ReadableMap) { | ||
| if (carrier.toHashMap().isEmpty()) { | ||
| Context.root().makeCurrent() | ||
| return | ||
| } | ||
|
|
||
| val sdk = OpenTelemetry.get() | ||
| val getter = RNTextMapGetter() | ||
| val currentContext = Context.current() | ||
| val extractedContext = | ||
| sdk.propagators.textMapPropagator.extract(currentContext, carrier, getter) | ||
| extractedContext.makeCurrent() | ||
| } | ||
|
|
||
| companion object { | ||
| const val NAME = "OpenTelemetry" | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
android/src/main/java/com/opentelemetry/RNTextMapGetter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.opentelemetry | ||
|
|
||
| import com.facebook.react.bridge.ReadableMap | ||
| import com.facebook.react.bridge.ReadableMapKeySetIterator | ||
| import io.opentelemetry.context.propagation.TextMapGetter | ||
|
|
||
| class RNTextMapGetter : TextMapGetter<ReadableMap> { | ||
| override fun keys(carrier: ReadableMap): MutableIterable<String> { | ||
| val iterator: ReadableMapKeySetIterator = carrier.keySetIterator() | ||
| val keys = mutableListOf<String>() | ||
| while (iterator.hasNextKey()) { | ||
| keys.add(iterator.nextKey()) | ||
| } | ||
| return keys | ||
| } | ||
|
|
||
| override fun get(carrier: ReadableMap?, key: String): String? { | ||
| return carrier?.getString(key) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| import { TurboModuleRegistry, type TurboModule } from "react-native"; | ||
|
|
||
| export interface Spec extends TurboModule {} | ||
| type Carrier = { | ||
| [key: string]: string; | ||
| }; | ||
|
|
||
| export interface Spec extends TurboModule { | ||
| setContext: (carrier: Carrier) => void; | ||
| } | ||
|
|
||
| export default TurboModuleRegistry.getEnforcing<Spec>("OpenTelemetry"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { ROOT_CONTEXT, propagation } from '@opentelemetry/api'; | ||
| import type {Context, ContextManager} from '@opentelemetry/api'; | ||
| import NATIVE from './NativeOpenTelemetry'; | ||
|
|
||
| /** | ||
| * Stack Context Manager for managing the state in JS, | ||
| * enriched with native-sync capabilities. | ||
| */ | ||
| export class RNContextManager implements ContextManager { | ||
| private _enabled = false; | ||
| public _currentContext = ROOT_CONTEXT; | ||
|
|
||
| // Bind a function to a given context. | ||
| // This is the same as the default helper. | ||
| private _bindFunction<T extends Function>( | ||
| context = ROOT_CONTEXT, | ||
| target: T | ||
| ): T { | ||
| const manager = this; | ||
| const contextWrapper = function (this: unknown, ...args: unknown[]) { | ||
| return manager.with(context, () => target.apply(this, args)); | ||
| }; | ||
| Object.defineProperty(contextWrapper, 'length', { | ||
| enumerable: false, | ||
| configurable: true, | ||
| writable: false, | ||
| value: target.length, | ||
| }); | ||
| return contextWrapper as unknown as T; | ||
| } | ||
|
|
||
| private _syncToNative() { | ||
| const carrier = {}; | ||
| propagation.inject(this._currentContext, carrier); | ||
| console.log({ carrier }); | ||
| NATIVE.setContext(carrier); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the active (current) context. | ||
| */ | ||
| active(): Context { | ||
| return this._currentContext; | ||
| } | ||
|
|
||
| /** | ||
| * Binds the provided context to a target function (or object) so that when that target is called, | ||
| * the provided context is active during its execution. | ||
| */ | ||
| bind<T>(context: Context, target: T): T { | ||
| if (context === undefined) { | ||
| context = this.active(); | ||
| } | ||
| if (typeof target === 'function') { | ||
| return this._bindFunction(context, target); | ||
| } | ||
| return target; | ||
| } | ||
|
|
||
| /** | ||
| * Disables the context manager and resets the current context to ROOT_CONTEXT. | ||
| * You could also choose to sync this state to Native if desired. | ||
| */ | ||
| disable(): this { | ||
| this._currentContext = ROOT_CONTEXT; | ||
| this._enabled = false; | ||
| // Optionally, notify Native with the ROOT_CONTEXT: | ||
| this._syncToNative(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Enables the context manager and initializes the current context. | ||
| * Synchronizes the initial state from Native. | ||
| */ | ||
| enable(): this { | ||
| if (this._enabled) { | ||
| return this; | ||
| } | ||
| this._enabled = true; | ||
| // Load any native context into the JS side | ||
| // this._currentContext = NATIVE.getContext() ?? ROOT_CONTEXT; | ||
| this._currentContext = ROOT_CONTEXT; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Executes the function [fn] with the provided [context] as the active context. | ||
| * Ensures that the updated context is sent to Native before and after the call. | ||
| */ | ||
| with<A extends unknown[], F extends (...args: A) => ReturnType<F>>( | ||
| context: Context | null, | ||
| fn: F, | ||
| thisArg?: ThisParameterType<F>, | ||
| ...args: A | ||
| ): ReturnType<F> { | ||
| const previousContext = this._currentContext; | ||
| // Set new active context (or fallback to ROOT_CONTEXT) | ||
| this._currentContext = context || ROOT_CONTEXT; | ||
|
|
||
| // Sync the new active context to Native | ||
| this._syncToNative(); | ||
|
|
||
| try { | ||
| return fn.call(thisArg, ...args); | ||
| } finally { | ||
| // Restore previous context | ||
| this._currentContext = previousContext; | ||
| // Re-sync the restored context back to Native | ||
| this._syncToNative(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { StackContextManager as RNContextManager } from "@opentelemetry/sdk-trace-web"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.