topfreegames / pitaya

Scalable game server framework with clustering support and client libraries for iOS, Android, Unity and others through the C SDK.
MIT License
2.31k stars 473 forks source link

Context cannot be transferred across services #418

Open Tiper-In-Github opened 1 month ago

Tiper-In-Github commented 1 month ago

description

I tried passing the context in RPC() and RPCTo() in V2, but the receiving end always received an empty context. I checked the context when sending through a breakpoint and confirmed that it contained the information I expected. Is this by design or an exception? (💡which prevents me from using opentelemetry to track links across services)

Code Snippet:

// Send
newCtx = context.WithValue(newCtx, "Test", "119991")
if err := app.RPC(newCtx, "Preview.Remote.sync", rpcResp, rpcReq); err != nil {
  logger.Log.Errorf("%v%v rpcFail:", logPrefix, err.Error())
  span.AddEvent("rpcFail:" + err.Error())
  return false
}

// Receiver
func (r *PreviewRemoteHandler) Sync(c context.Context, req *rpcProtobuf.ReqSync) (*rpcProtobuf.RespSync, error) {
    logger.Log.Infof("test context:%v", c.Value("Test"))
        ......
}

Expected results:

test context:119991

Actual results:

test context:<nil>

Tiper-In-Github commented 1 month ago

@felipejfc Hope to see this issue soon

felipejfc commented 1 month ago

You're right, ctx is currently not propagated. However, there's default integration with Opentelemetry already in master branch. See the docs. Here If you want to propagate more values between calls you can use the "Propagated Context" utility which is the same one we use for default opentelemetry integration. See here https://github.com/topfreegames/pitaya/blob/main/pkg/context/context.go#L30-L44

Tiper-In-Github commented 1 month ago

Thanks,In the future, are there plans to support propagation to enable cross-service link tracking?

felipejfc commented 1 month ago

Can you be more specific about which kind of key/values you will be propagating? Also, does this manual propagation method fits your needs?

I'm thinking about a more transparent way of context propagation that will happen automatically, so gathering some info on use cases would be helpful

Tiper-In-Github commented 1 month ago

We hope to use OpenTelemetry while passing the engine's existing 'request_id' to provide more detailed and convenient tracking and logging.Since we are using V2, we are connecting to opentelemetry ourselves