@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
22import {
33 AuthOrchestrationOperateScope ,
44 AuthOrchestrationReadScope ,
5+ type AuthEnvironmentScope ,
56 ScheduledTaskError ,
67 ScheduledTaskId ,
78 WS_METHODS ,
@@ -20,7 +21,8 @@ import * as Tracer from "effect/Tracer";
2021import * as RpcTest from "effect/unstable/rpc/RpcTest" ;
2122import * as TestClock from "effect/testing/TestClock" ;
2223
23- import { RPC_REQUIRED_SCOPES , wsRpcGuardLayer } from "../auth/RpcAuthorization.ts" ;
24+ import { RPC_REQUIRED_SCOPES , rpcScopeAuthorizationLayer } from "../auth/RpcAuthorization.ts" ;
25+ import { rpcInstrumentationLayer } from "./RpcInstrumentation.ts" ;
2426
2527type WsRpcMethod = keyof typeof RPC_REQUIRED_SCOPES ;
2628
@@ -33,7 +35,10 @@ const groupOf = <const Tags extends ReadonlyArray<WsRpcMethod>>(...tags: Tags) =
3335 ) ,
3436 ) ;
3537
36- const readOnlyGuard = wsRpcGuardLayer ( [ AuthOrchestrationReadScope ] ) ;
38+ /** The middleware ws.ts installs for a connection with `scopes`. */
39+ const connectionMiddleware = ( scopes : ReadonlyArray < AuthEnvironmentScope > ) =>
40+ Layer . merge ( rpcScopeAuthorizationLayer ( scopes ) , rpcInstrumentationLayer ) ;
41+ const readOnlyConnection = connectionMiddleware ( [ AuthOrchestrationReadScope ] ) ;
3742const taskId = ScheduledTaskId . make ( "scheduled-task:instrumented" ) ;
3843const rpcSpanDefaults = { "rpc.transport" : "websocket" , "rpc.system" : "effect-rpc" } ;
3944
@@ -64,7 +69,7 @@ const rpcSpans = (ended: ReadonlyArray<Tracer.NativeSpan>) =>
6469 ended . filter ( ( span ) => span . name . startsWith ( "ws.rpc." ) ) ;
6570
6671// RpcTest keeps Effect's own RpcServer/RpcClient spans, which ws.ts turns off with
67- // `disableTracing: true`. Everything else comes from the guard or the handlers.
72+ // `disableTracing: true`. Everything else comes from the middleware or the handlers.
6873const appSpans = ( ended : ReadonlyArray < Tracer . NativeSpan > ) =>
6974 ended . filter ( ( span ) => ! / ^ R p c ( S e r v e r | C l i e n t ) \. / . test ( span . name ) ) ;
7075
@@ -95,7 +100,7 @@ const requestDuration = (snapshots: ReadonlyArray<Metric.Metric.Snapshot>, metho
95100 snapshot . attributes ?. [ "method" ] === method ,
96101 ) ?. state ;
97102
98- describe ( "WS RPC guard instrumentation" , ( ) => {
103+ describe ( "WS RPC instrumentation middleware " , ( ) => {
99104 it . effect ( "records one span and request metric per call, including rejected calls" , ( ) =>
100105 withTelemetry ( ( ended ) =>
101106 Effect . gen ( function * ( ) {
@@ -119,7 +124,7 @@ describe("WS RPC guard instrumentation", () => {
119124 ) ,
120125 ) ,
121126 group . toLayerHandler ( WS_METHODS . serverRetryResourceTelemetry , ( ) =>
122- Effect . die ( "the guard let a rejected call through" ) ,
127+ Effect . die ( "authorization let a rejected call through" ) ,
123128 ) ,
124129 group . toLayerHandler ( WS_METHODS . pullRequestsSubscribeRefreshes , ( ) =>
125130 Stream . make ( 1 , 2 ) ,
@@ -131,7 +136,7 @@ describe("WS RPC guard instrumentation", () => {
131136 ) ,
132137 ) ,
133138 group . toLayerHandler ( WS_METHODS . serverGetSettings , ( ) => Effect . die ( "broken" ) ) ,
134- readOnlyGuard ,
139+ readOnlyConnection ,
135140 ) ,
136141 ) ,
137142 ) ;
@@ -259,7 +264,7 @@ describe("WS RPC guard instrumentation", () => {
259264 ) ,
260265 ) ,
261266 ) ,
262- readOnlyGuard ,
267+ readOnlyConnection ,
263268 ) ,
264269 ) ,
265270 ) ;
@@ -307,7 +312,7 @@ describe("WS RPC guard instrumentation", () => {
307312 message : Option . none ( ) ,
308313 } ) . pipe ( Effect . withSpan ( "signalProcess.child" ) ) ,
309314 ) ,
310- wsRpcGuardLayer ( [ AuthOrchestrationReadScope , AuthOrchestrationOperateScope ] ) ,
315+ connectionMiddleware ( [ AuthOrchestrationReadScope , AuthOrchestrationOperateScope ] ) ,
311316 ) ,
312317 ) ,
313318 ) ;
0 commit comments