class JfrScopeDecorator implements ScopeDecorator {

  @Override public Scope decorateScope(@Nullable TraceContext context, Scope scope) {
    if (scope == Scope.NOOP) return scope; // we only scope fields constant in the context

    ScopeEvent event = new ScopeEvent();
    if (!event.isEnabled()) return scope;

    if (context != null) {
      event.traceId = context.traceIdString();
      event.parentId = context.parentIdString();
      event.spanId = context.spanIdString();
    }

    event.begin();

    class JfrCurrentTraceContextScope implements Scope {
      @Override public void close() {
        scope.close();
        event.commit();
      }
    }
    return new JfrCurrentTraceContextScope();
  }

    JfrScopeDecorator();

}