Skip to content

Commit

Permalink
metric: propagate OTel parent span for service-call and conversation
Browse files Browse the repository at this point in the history
An execution::context that hold
* current service
* current execution span
* parent service
* parent execution span

During a service call the current service and execution span will be
propagated as parent service and parent execution span.
Service invocation (callee side) will update the execution::context with
the parent information.

Added execution span and parent span to service metric, and appended
these to the event-service-log.

New versions of messages to include execution span:
* service-call-request
* service-call-reply (the same for now)
* conversation-connect-request
  • Loading branch information
lazan committed Jul 2, 2024
1 parent 9a37533 commit 5bda1c0
Show file tree
Hide file tree
Showing 56 changed files with 752 additions and 315 deletions.
4 changes: 2 additions & 2 deletions middleware/cli/include/casual/cli/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace casual
void message( const M& message)
{
if( ! message.execution)
message.execution = common::execution::id();
message.execution = common::execution::context::get().id;

cli::message::to::human< M>::stream( message);
}
Expand All @@ -45,7 +45,7 @@ namespace casual
void message( const M& message)
{
if( ! message.execution)
message.execution = common::execution::id();
message.execution = common::execution::context::get().id;

if( pipe::terminal::out())
cli::message::to::human< M>::stream( message);
Expand Down
2 changes: 1 addition & 1 deletion middleware/common/include/common/communication/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ namespace casual
else
{
if( ! message.execution)
message.execution = execution::id();
message.execution = execution::context::get().id;

return apply(
std::forward< P>( policy),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace casual

inline auto correlation( const Header& header)
{
return strong::correlation::id::emplace( header.correlation);
return strong::correlation::id{ header.correlation};
}

} // detail
Expand Down
54 changes: 0 additions & 54 deletions middleware/common/include/common/execution.h

This file was deleted.

103 changes: 103 additions & 0 deletions middleware/common/include/common/execution/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//!
//! Copyright (c) 2015, The casual project
//!
//! This software is licensed under the MIT license, https://opensource.org/licenses/MIT
//!

#pragma once

#include "common/strong/id.h"
#include <string>

namespace casual
{
namespace common::execution
{

using type = strong::execution::id;

namespace context
{
struct Parent
{
strong::execution::span::id span;
std::string service;

inline friend bool operator == ( const Parent&, const Parent&) = default;

CASUAL_CONST_CORRECT_SERIALIZE(
CASUAL_SERIALIZE( span);
CASUAL_SERIALIZE( service);
)
};

} // context

struct Context
{
strong::execution::id id;
strong::execution::span::id span;
std::string service;
context::Parent parent;

CASUAL_LOG_SERIALIZE(
CASUAL_SERIALIZE( id);
CASUAL_SERIALIZE( span);
CASUAL_SERIALIZE( service);
CASUAL_SERIALIZE( parent);
)
};

namespace context
{
//! @returns the curren execution context
const Context& get();

//! clears all execution context state
void clear();

//! resets/regenerate id, span. clears parent
void reset();

namespace id
{
//! sets a new execution id. @returns the old.
strong::execution::id set( strong::execution::id id);

//! sets a new random execution id.
strong::execution::id reset();
} // id

namespace span
{
strong::execution::span::id set( strong::execution::span::id id);
strong::execution::span::id reset();
} // span

namespace service
{
//! Sets the current service
void set( const std::string& service);

//! clear the service name
void clear();
} // service

namespace parent
{
namespace service
{
void set( const std::string& service);
} // service

namespace span
{
void set( strong::execution::span::id id);
} // span

} // parent

} // context

} // common::execution
} // casual
70 changes: 68 additions & 2 deletions middleware/common/include/common/message/conversation.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,84 @@ namespace casual

namespace connect
{
namespace v1_2
{
using base_type = message::basic_request< Type::conversation_connect_request_v2>;
struct base_request : base_type
{
using base_type::base_type;

service::call::Service service;
std::string parent;

common::transaction::ID trid;
common::service::header::Fields header;

//! pending time, only to be return in the "ACK", to collect
//! metrics
platform::time::unit pending{};

duplex::Type duplex{};

CASUAL_CONST_CORRECT_SERIALIZE(
base_type::serialize( archive);
CASUAL_SERIALIZE( service);
CASUAL_SERIALIZE( parent);
CASUAL_SERIALIZE( trid);
CASUAL_SERIALIZE( header);
CASUAL_SERIALIZE( pending);
CASUAL_SERIALIZE( duplex);
)
};

namespace caller
{
struct Request : base_request
{
template< typename... Args>
Request( common::buffer::payload::Send buffer, Args&&... args)
: base_request( std::forward< Args>( args)...), buffer( std::move( buffer))
{}
common::buffer::payload::Send buffer;

CASUAL_CONST_CORRECT_SERIALIZE(
base_request::serialize( archive);
CASUAL_SERIALIZE( buffer);
)
};

} // caller

namespace callee
{
struct Request : base_request
{
using base_request::base_request;

common::buffer::Payload buffer;

CASUAL_CONST_CORRECT_SERIALIZE(
base_request::serialize( archive);
CASUAL_SERIALIZE( buffer);
)
};

} // callee

} // v1_2


using base_type = message::basic_request< Type::conversation_connect_request>;
struct base_request : base_type
{
using base_type::base_type;

service::call::Service service;
std::string parent;
execution::context::Parent parent;

common::transaction::ID trid;
common::service::header::Fields header;


//! pending time, only to be return in the "ACK", to collect
//! metrics
platform::time::unit pending{};
Expand Down
4 changes: 2 additions & 2 deletions middleware/common/include/common/message/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#pragma once

#include "common/execution.h"
#include "common/execution/context.h"
#include "common/traits.h"
#include "common/serialize/native/complete.h"
#include "common/communication/device.h"
Expand Down Expand Up @@ -149,7 +149,7 @@ namespace casual
message_type message;

serialize::native::complete( complete, message);
execution::id( message.execution);
execution::context::id::set( message.execution);

// We got 4 combinations of signature we need to take care of.
// We could move it out to a few function but that seems to get
Expand Down
6 changes: 5 additions & 1 deletion middleware/common/include/common/message/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,15 @@ namespace casual
}
return "<unknown>";
}

} // metric


struct Metric
{
strong::execution::span::id span;
std::string service;
std::string parent;
execution::context::Parent parent;
metric::Type type = metric::Type::sequential;

common::process::Handle process;
Expand All @@ -255,6 +258,7 @@ namespace casual
auto duration() const noexcept { return end - start;}

CASUAL_CONST_CORRECT_SERIALIZE(
CASUAL_SERIALIZE( span);
CASUAL_SERIALIZE( service);
CASUAL_SERIALIZE( parent);
CASUAL_SERIALIZE( type);
Expand Down
5 changes: 3 additions & 2 deletions middleware/common/include/common/message/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,14 @@ namespace casual
};
} // v1_2


struct base_request : message::basic_request< message::Type::service_call>
{
using base_type = message::basic_request< message::Type::service_call>;
using base_type::base_type;

execution::context::Parent parent;
Service service;
std::string parent;

common::transaction::ID trid;
request::Flag flags{};
Expand All @@ -604,8 +605,8 @@ namespace casual

CASUAL_CONST_CORRECT_SERIALIZE(
base_type::serialize( archive);
CASUAL_SERIALIZE( service);
CASUAL_SERIALIZE( parent);
CASUAL_SERIALIZE( service);
CASUAL_SERIALIZE( trid);
CASUAL_SERIALIZE( flags);
CASUAL_SERIALIZE( header);
Expand Down
12 changes: 7 additions & 5 deletions middleware/common/include/common/message/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "common/strong/type.h"
#include "common/serialize/macro.h"
#include "common/execution.h"
#include "common/execution/context.h"
#include "common/process.h"

#include <type_traits>
Expand Down Expand Up @@ -120,10 +120,12 @@ namespace casual
// 3202
// 3203

conversation_connect_request = 3210,
conversation_connect_reply = 3211,
conversation_send = 3212,
conversation_disconnect = 3213,
conversation_connect_request_v2 = 3210,
conversation_connect_reply = 3211,
conversation_send = 3212,
conversation_disconnect = 3213,

conversation_connect_request = 3220,


// event messages
Expand Down
2 changes: 1 addition & 1 deletion middleware/common/include/common/serialize/native/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "common/traits.h"
#include "common/algorithm.h"
#include "common/memory.h"
#include "common/execution.h"
#include "common/execution/context.h"

#include "casual/assert.h"

Expand Down
Loading

0 comments on commit 5bda1c0

Please sign in to comment.