ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
otel.h File Reference

Go to the source code of this file.

Data Structures

struct  ascii_otel_scope
 ascii otel scope is intentionally a small, concrete record. Fields such as scope, retained, previous give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed. More...

Macros

#define ASCII_OTEL_OPERATION(id, name, realtime)
#define ASCII_OTEL_METRIC(id, name, type, unit)
#define ASCII_OTEL_ATTR_STRING(key, value)
#define ASCII_OTEL_ATTR_INT(key, value)
#define ASCII_OTEL_PACKET(type, size, action, transport)
#define ASCII_OTEL_SCOPE_PARENT(id, parent)
#define ASCII_OTEL_SCOPE(id)
#define ASCII_OTEL_SCOPE_AS(variable, id)

Typedefs

typedef struct ascii_otel_scope ascii_otel_scope_t
 ascii otel scope is intentionally a small, concrete record. Fields such as scope, retained, previous give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed.

Enumerations

enum  ascii_otel_operation_t { ASCII_OTEL_OPERATION_COUNT }
enum  ascii_otel_metric_t { ASCII_OTEL_METRIC_COUNT }

Functions

asciichat_error_t ascii_otel_init (const char *mode, int argc, char **argv)
void ascii_otel_shutdown (void)
void ascii_otel_request_shutdown (void)
bool ascii_otel_enabled (void)
const char * ascii_otel_current_operation (void)
void ascii_otel_metric (ascii_otel_metric_t metric, double value)
void ascii_otel_stats_attach (stats_scope_t *scope)
ascii_otel_metric_t ascii_otel_stats_metric (stats_record_kind_t kind, unsigned id)
void ascii_otel_ui_usage (const char *screen, const char *event, uint64_t duration_ns)
void ascii_otel_call_session_used (const char *session_string)
void ascii_otel_packet (ascii_otel_scope_t *scope, uint16_t type, size_t payload_size, const char *action, int transport)
void ascii_otel_exit (int code)
void ascii_otel_frame_rejected (const char *validator, const char *reason, size_t bytes, uint32_t width, uint32_t height, asciichat_error_t code)
gotel_context_t ascii_otel_context (void)
void ascii_otel_scope_int (ascii_otel_scope_t *scope, const char *key, int64_t value)
void ascii_otel_scope_string (ascii_otel_scope_t *scope, const char *key, const char *value)
void ascii_otel_scope_parent (ascii_otel_scope_t *scope, ascii_otel_operation_t operation, const gotel_context_t *parent)
void ascii_otel_scope_begin (ascii_otel_scope_t *scope, ascii_otel_operation_t operation)
void ascii_otel_scope_end (ascii_otel_scope_t *scope)
void ascii_otel_log (int level, const char *file, int line, const char *function, const char *format, va_list args)
void ascii_otel_error (const char *message)
void ascii_otel_error_code (asciichat_error_t code, const char *message)
ascii_otel_operation_t ascii_otel_failure_operation (asciichat_error_t code, const char *origin)
double ascii_otel_pipeline_age (uint64_t now, uint64_t capture, uint64_t progress, bool expected)
void ascii_otel_pipeline_progress (unsigned stage, unsigned expected)
void ascii_otel_http (ascii_otel_scope_t *scope, const char *hostname, const char *path)
void ascii_otel_http_status (ascii_otel_scope_t *scope, int status)
void ascii_otel_sqlite (sqlite3 *db, const gotel_query_t *queries, size_t count)
void ascii_otel_sqlite_catalog (sqlite3 *db, const gotel_query_catalog_t *catalog)
int ascii_otel_sqlite_prepare (sqlite3 *db, const char *sql, sqlite3_stmt **statement)

Macro Definition Documentation

◆ ASCII_OTEL_ATTR_INT

◆ ASCII_OTEL_ATTR_STRING

◆ ASCII_OTEL_METRIC

#define ASCII_OTEL_METRIC ( id,
name,
type,
unit )
Value:
ASCII_OTEL_METRIC_##id,

Definition at line 13 of file otel.h.

◆ ASCII_OTEL_OPERATION

#define ASCII_OTEL_OPERATION ( id,
name,
realtime )
Value:
ASCII_OTEL_##id,

Definition at line 7 of file otel.h.

◆ ASCII_OTEL_PACKET

#define ASCII_OTEL_PACKET ( type,
size,
action,
transport )
Value:
ascii_otel_packet(&otel_scope, type, size, action, transport)
deferred_action_t action
void ascii_otel_packet(ascii_otel_scope_t *scope, uint16_t type, size_t payload_size, const char *action, int transport)
Definition otel.c:524

Definition at line 96 of file otel.h.

96#define ASCII_OTEL_PACKET(type, size, action, transport) \
97 ascii_otel_packet(&otel_scope, type, size, action, transport)

Referenced by packet_receive_via_transport(), and packet_send_via_transportv().

◆ ASCII_OTEL_SCOPE

#define ASCII_OTEL_SCOPE ( id)
Value:
ascii_otel_scope_t otel_scope \
__attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
ascii_otel_scope_begin(&otel_scope, ASCII_OTEL_##id)
void ascii_otel_scope_end(ascii_otel_scope_t *s)
Definition otel.c:497
struct ascii_otel_scope ascii_otel_scope_t
ascii otel scope is intentionally a small, concrete record. Fields such as scope, retained,...

Definition at line 102 of file otel.h.

102#define ASCII_OTEL_SCOPE(id) \
103 ascii_otel_scope_t otel_scope \
104 __attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
105 ascii_otel_scope_begin(&otel_scope, ASCII_OTEL_##id)

Referenced by acds_on_host_announcement_ex(), acds_send_current_host(), acds_server_init(), acds_server_shutdown(), acip_client_receive_and_dispatch(), acip_outbound_create(), acip_outbound_destroy(), acip_send_ascii_frame_contract(), acip_send_audio_batch(), acip_send_audio_opus(), acip_send_audio_opus_batch(), acip_send_error(), acip_send_image_frame(), acip_send_image_frame_h265(), acip_send_ping(), acip_send_server_state(), acip_send_stream_start(), acip_send_stream_stop(), acip_server_receive_and_dispatch(), ascii_frame_init(), ascii_half_frame_init(), ascii_legacy_frame_init(), ascii_legacy_half_frame_init(), audio_destroy(), audio_init(), audio_start_duplex(), audio_stop_duplex(), client_audio_render_thread(), client_connection_start(), client_video_render_thread(), compress_data(), compress_workspace(), crypto_decrypt(), crypto_encrypt(), crypto_handshake_client_auth_response(), crypto_handshake_client_complete(), crypto_handshake_client_key_exchange(), crypto_handshake_server_auth_challenge(), crypto_handshake_server_complete(), crypto_handshake_server_start(), database_close(), database_init(), database_session_add_key(), database_session_cleanup_expired(), database_session_clear_host(), database_session_get_keys(), database_session_is_migration_ready(), database_session_join(), database_session_leave(), database_session_lookup(), database_session_revoke_key(), database_session_start_migration(), database_session_update_host(), database_session_verify_key(), database_stats_session_count(), database_stats_snapshot(), database_stats_unique_clients(), decompress_data(), decompress_data_exact(), discovery_session_destroy(), discovery_session_stop(), dns_test_connectivity(), ffmpeg_decoder_create_stdin(), ffmpeg_decoder_create_with_request_options(), ffmpeg_decoder_destroy(), ffmpeg_decoder_poll_video(), ffmpeg_decoder_read_audio_samples(), ffmpeg_decoder_read_video_frame(), ffmpeg_decoder_seek_to_timestamp(), h265_decode(), h265_decoder_create(), h265_encode(), h265_encoder_create(), h265_encoder_flush(), http_server_dispatch_http3(), https_get(), mcp_dispatch(), mcp_error(), media_source_create_interruptible(), media_source_destroy(), media_source_read_audio(), media_source_read_video(), media_source_rewind(), media_source_seek(), media_source_start_video(), mixer_add_source(), mixer_create_channels(), mixer_process(), mixer_process_excluding_source(), mixer_remove_source(), nat_upnp_close(), nat_upnp_open_protocol(), nat_upnp_refresh(), network_connection_open(), opus_codec_create_decoder_channels(), opus_codec_create_encoder_channels(), opus_codec_decode(), opus_codec_encode(), opus_codec_set_bitrate(), opus_codec_set_dtx(), packet_receive_via_transport(), packet_send_via_transportv(), parallel_connect(), rate_limiter_check(), rate_limiter_prune(), server_connection_init(), server_connection_lost(), server_connection_shutdown(), session_capture_create(), session_capture_destroy(), session_capture_open_media(), session_capture_open_playlist(), session_capture_process_for_transmission_into(), session_capture_read_audio(), session_capture_read_frame(), session_capture_skip_media(), session_client_like_run(), session_display_convert_to_ascii_reuse(), session_display_create(), session_display_encode_frame(), session_display_write_ascii_frame(), session_display_write_audio(), session_display_write_raw(), session_host_render_audio(), session_render_loop(), video_encoder_create(), yt_dlp_extract_with_options(), and yt_dlp_is_available().

◆ ASCII_OTEL_SCOPE_AS

#define ASCII_OTEL_SCOPE_AS ( variable,
id )
Value:
ascii_otel_scope_t variable \
__attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
ascii_otel_scope_begin(&variable, ASCII_OTEL_##id)

Definition at line 106 of file otel.h.

106#define ASCII_OTEL_SCOPE_AS(variable, id) \
107 ascii_otel_scope_t variable \
108 __attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
109 ascii_otel_scope_begin(&variable, ASCII_OTEL_##id)

Referenced by https_get(), parallel_connect(), and session_client_like_run().

◆ ASCII_OTEL_SCOPE_PARENT

#define ASCII_OTEL_SCOPE_PARENT ( id,
parent )
Value:
ascii_otel_scope_t otel_scope \
__attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
ascii_otel_scope_parent(&otel_scope, ASCII_OTEL_##id, parent)

Definition at line 98 of file otel.h.

98#define ASCII_OTEL_SCOPE_PARENT(id, parent) \
99 ascii_otel_scope_t otel_scope \
100 __attribute__((cleanup(ascii_otel_scope_end))) = {0}; \
101 ascii_otel_scope_parent(&otel_scope, ASCII_OTEL_##id, parent)

Referenced by ascii_otel_call_session_used(), ascii_otel_ui_usage(), and session_pipeline_run_main().

Typedef Documentation

◆ ascii_otel_scope_t

ascii otel scope is intentionally a small, concrete record. Fields such as scope, retained, previous give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed.

Enumeration Type Documentation

◆ ascii_otel_metric_t

Enumerator
ASCII_OTEL_METRIC_COUNT 

Definition at line 12 of file otel.h.

12 {
13#define ASCII_OTEL_METRIC(id, name, type, unit) ASCII_OTEL_METRIC_##id,
14#include <ascii-chat/otel_metrics.def>
15#undef ASCII_OTEL_METRIC
ascii_otel_metric_t
Definition otel.h:12
@ ASCII_OTEL_METRIC_COUNT
Definition otel.h:16

◆ ascii_otel_operation_t

Enumerator
ASCII_OTEL_OPERATION_COUNT 

Definition at line 6 of file otel.h.

6 {
7#define ASCII_OTEL_OPERATION(id, name, realtime) ASCII_OTEL_##id,
8#include <ascii-chat/otel_operations.def>
9#undef ASCII_OTEL_OPERATION
ascii_otel_operation_t
Definition otel.h:6
@ ASCII_OTEL_OPERATION_COUNT
Definition otel.h:10

Function Documentation

◆ ascii_otel_call_session_used()

void ascii_otel_call_session_used ( const char * session_string)

Definition at line 516 of file otel.c.

516 {
517 if (!session_string || !session_string[0])
518 return;
519 // Call usage must not inherit a realtime packet's sampling decision.
520 gotel_context_t root = {0};
521 ASCII_OTEL_SCOPE_PARENT(CALL_SESSION_USED, &root);
522 ASCII_OTEL_ATTR_STRING("ascii-chat.session.string", session_string);
523}
#define ASCII_OTEL_SCOPE_PARENT(id, parent)
Definition otel.h:98
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:92

References ASCII_OTEL_ATTR_STRING, and ASCII_OTEL_SCOPE_PARENT.

◆ ascii_otel_context()

gotel_context_t ascii_otel_context ( void )

Definition at line 870 of file otel.c.

870{ return gotel_current_context(); }

Referenced by ascii_otel_error_code(), ascii_otel_frame_rejected(), and mcp_dispatch().

◆ ascii_otel_current_operation()

const char * ascii_otel_current_operation ( void )

Definition at line 165 of file otel.c.

165 {
166 return current_scope &&
167 current_scope->scope.operation < ASCII_OTEL_OPERATION_COUNT
168 ? operations[current_scope->scope.operation].name
169 : "";
170}

References ASCII_OTEL_OPERATION_COUNT.

Referenced by ascii_otel_error_code().

◆ ascii_otel_enabled()

bool ascii_otel_enabled ( void )

Definition at line 171 of file otel.c.

171{ return atomic_ptr_load(&host) != NULL; }
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.

References atomic_ptr_load().

Referenced by ascii_otel_error_code(), and ascii_otel_pipeline_progress().

◆ ascii_otel_error()

void ascii_otel_error ( const char * message)

Definition at line 965 of file otel.c.

965 {
966 char redacted[OTEL_BODY_BUFFER_SIZE];
967 int written =
968 safe_snprintf(redacted, sizeof(redacted), "%s", message ? message : "");
969 if (written < 0)
970 return;
971 url_redact_diagnostic(redacted, (size_t)written >= sizeof(redacted));
972 gotel_current_error("ascii_chat.error", redacted);
973}
void url_redact_diagnostic(char *text, bool truncated)
Definition url.c:32
#define OTEL_BODY_BUFFER_SIZE
Definition otel.c:7

References OTEL_BODY_BUFFER_SIZE, and url_redact_diagnostic().

Referenced by mcp_error().

◆ ascii_otel_error_code()

void ascii_otel_error_code ( asciichat_error_t code,
const char * message )

Definition at line 932 of file otel.c.

932 {
933 if (code == ASCIICHAT_OK || code == ERROR_SIGNAL_INTERRUPT ||
935 return;
936 char redacted[OTEL_BODY_BUFFER_SIZE];
937 int written =
938 safe_snprintf(redacted, sizeof(redacted), "%s", message ? message : "");
939 if (written < 0)
940 return;
941 url_redact_diagnostic(redacted, (size_t)written >= sizeof(redacted));
942 gotel_current_error("ascii_chat.error", redacted);
943 if (!ascii_otel_enabled())
944 return;
945 const char *origin = ascii_otel_current_operation();
947 if (operation == ASCII_OTEL_OPERATION_COUNT)
948 return;
949 const char *category = operation == ASCII_OTEL_FAILURE_CRYPTO ? "crypto"
950 : operation == ASCII_OTEL_FAILURE_PROTOCOL
951 ? "protocol"
952 : "discovery";
953 gotel_context_t parent = ascii_otel_context();
954 ascii_otel_scope_t report
955 __attribute__((cleanup(ascii_otel_scope_end))) = {0};
956 if (!scope_retained(&report, operation, &parent))
957 return;
958 gotel_attr_string(&report.scope, "ascii_chat.failure.category", category);
959 gotel_attr_string(&report.scope, "ascii_chat.failure.operation", origin);
960 gotel_attr_int(&report.scope, "asciichat_errno.code", code);
961 gotel_attr_string(&report.scope, "asciichat_errno.code_message",
962 asciichat_error_string(code));
963 gotel_error(&report.scope, category, redacted);
964}
@ ERROR_NETWORK_INCOMPLETE
Definition error_codes.h:97
@ ERROR_SIGNAL_INTERRUPT
@ ASCIICHAT_OK
Definition error_codes.h:50
int32_t vec_t __attribute__((vector_size(4 *4)))
SESSION_CREATED (PACKET_TYPE_ACIP_SESSION_CREATED) - Session created response.
Definition vector.h:42
bool ascii_otel_enabled(void)
Definition otel.c:171
gotel_context_t ascii_otel_context(void)
Definition otel.c:870
ascii_otel_operation_t ascii_otel_failure_operation(asciichat_error_t code, const char *origin)
Definition otel.c:914
const char * ascii_otel_current_operation(void)
Definition otel.c:165

References ascii_otel_context(), ascii_otel_current_operation(), ascii_otel_enabled(), ascii_otel_failure_operation(), ASCII_OTEL_OPERATION_COUNT, ascii_otel_scope_end(), ASCIICHAT_OK, ERROR_NETWORK_INCOMPLETE, ERROR_SIGNAL_INTERRUPT, OTEL_BODY_BUFFER_SIZE, and url_redact_diagnostic().

◆ ascii_otel_exit()

void ascii_otel_exit ( int code)

Definition at line 881 of file otel.c.

881 {
882 gotel_attr_int(&command_scope, "process.exit.code", code);
883 if (code)
884 gotel_error(&command_scope, "ascii_chat.exit", "Command failed");
885 else {
886 command_scope.failed = false;
887 if (command_scope.span)
888 (void)otel_status(
889 otlp_span_set_status(command_scope.span, OTLP_STATUS_CODE_UNSET, ""),
890 "otlp_span_set_status");
891 }
892}

◆ ascii_otel_failure_operation()

ascii_otel_operation_t ascii_otel_failure_operation ( asciichat_error_t code,
const char * origin )

Definition at line 914 of file otel.c.

915 {
916 if (code == ASCIICHAT_OK || code == ERROR_SIGNAL_INTERRUPT ||
919 if ((code >= ERROR_CRYPTO && code <= ERROR_CRYPTO_VERIFICATION) ||
920 code == ERROR_CRYPTO_INIT || code == ERROR_INVALID_PASSWORD ||
922 return ASCII_OTEL_FAILURE_CRYPTO;
923 if (code == ERROR_NETWORK_PROTOCOL || code == ERROR_NETWORK_SIZE ||
924 code == ERROR_UNKNOWN_PACKET)
925 return ASCII_OTEL_FAILURE_PROTOCOL;
926 if ((code >= ERROR_SESSION_NOT_FOUND && code <= ERROR_SESSION_FULL) ||
928 (origin && !strncmp(origin, "ascii-chat.discovery.", 21)))
929 return ASCII_OTEL_FAILURE_DISCOVERY;
931}
@ ERROR_UNKNOWN_PACKET
Definition error_codes.h:95
@ ERROR_INVALID_PASSWORD
Definition error_codes.h:89
@ ERROR_ACDS_STRING_INVALID
Definition error_codes.h:93
@ ERROR_ACDS_STRING_TAKEN
Definition error_codes.h:91
@ ERROR_CRYPTO_VERIFICATION
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_INVALID_SIGNATURE
Definition error_codes.h:90
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_SESSION_FULL
Definition error_codes.h:88
@ ERROR_CRYPTO_INIT
Definition error_codes.h:57
@ ERROR_CRYPTO
@ ERROR_SESSION_NOT_FOUND
Definition error_codes.h:87

References ASCII_OTEL_OPERATION_COUNT, ASCIICHAT_OK, ERROR_ACDS_STRING_INVALID, ERROR_ACDS_STRING_TAKEN, ERROR_CRYPTO, ERROR_CRYPTO_INIT, ERROR_CRYPTO_VERIFICATION, ERROR_INVALID_PASSWORD, ERROR_INVALID_SIGNATURE, ERROR_NETWORK_INCOMPLETE, ERROR_NETWORK_PROTOCOL, ERROR_NETWORK_SIZE, ERROR_SESSION_FULL, ERROR_SESSION_NOT_FOUND, ERROR_SIGNAL_INTERRUPT, and ERROR_UNKNOWN_PACKET.

Referenced by ascii_otel_error_code().

◆ ascii_otel_frame_rejected()

void ascii_otel_frame_rejected ( const char * validator,
const char * reason,
size_t bytes,
uint32_t width,
uint32_t height,
asciichat_error_t code )

Definition at line 893 of file otel.c.

895 {
896 ascii_otel_metric(ASCII_OTEL_METRIC_FRAME_VALIDATION_REJECTED, 1);
897 gotel_context_t parent = ascii_otel_context();
898 ascii_otel_scope_t otel_scope
899 __attribute__((cleanup(ascii_otel_scope_end))) = {0};
900 if (!scope_retained(&otel_scope, ASCII_OTEL_FRAME_VALIDATION_REJECTED,
901 &parent))
902 return;
903 ASCII_OTEL_ATTR_STRING("frame.validation.validator", validator);
904 ASCII_OTEL_ATTR_STRING("frame.validation.reason", reason);
905 ASCII_OTEL_ATTR_INT("frame.size_bytes", (int64_t)bytes);
906 ASCII_OTEL_ATTR_INT("frame.width", width);
907 ASCII_OTEL_ATTR_INT("frame.height", height);
908 ASCII_OTEL_ATTR_INT("asciichat_errno.code", code);
909 ASCII_OTEL_ATTR_STRING("asciichat_errno.code_message",
910 asciichat_error_string(code));
911 if (otel_scope.retained)
912 gotel_error(&otel_scope.scope, "ascii_chat.invalid_frame", reason);
913}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:175
#define ASCII_OTEL_ATTR_INT(key, value)
Definition otel.h:94

References ASCII_OTEL_ATTR_INT, ASCII_OTEL_ATTR_STRING, ascii_otel_context(), ascii_otel_metric(), ascii_otel_scope_end(), height, and width.

Referenced by session_frame_validate_contract().

◆ ascii_otel_http()

void ascii_otel_http ( ascii_otel_scope_t * scope,
const char * hostname,
const char * path )

Definition at line 974 of file otel.c.

975 {
976 if (!s->retained || !hostname || !path)
977 return;
978 gotel_error(&s->scope, "http.transport", "HTTP operation did not complete");
980 int n = safe_snprintf(url, sizeof(url), "https://%s%s", hostname, path);
981 if (n >= 0 && (size_t)n < sizeof(url) &&
982 otel_status(
983 gotel_http_name(name, sizeof(name), "ascii-chat", false, "GET", url),
984 "Name HTTP telemetry span") == ASCIICHAT_OK) {
985 if (s->scope.span)
986 (void)otel_status(otlp_span_set_name(s->scope.span, name),
987 "otlp_span_set_name");
988 gotel_http_attributes(&s->scope, "GET", 0);
989 gotel_attr_string(&s->scope, "server.address", hostname);
990 gotel_attr_string(&s->scope, "url.scheme", "https");
991 }
992}
#define OTEL_NAME_EXTENDED_BUFFER_SIZE
Definition otel.c:9
#define OTEL_URL_BUFFER_SIZE
Definition otel.c:8

References ASCIICHAT_OK, OTEL_NAME_EXTENDED_BUFFER_SIZE, OTEL_URL_BUFFER_SIZE, ascii_otel_scope::retained, and ascii_otel_scope::scope.

Referenced by https_get().

◆ ascii_otel_http_status()

void ascii_otel_http_status ( ascii_otel_scope_t * scope,
int status )

Definition at line 993 of file otel.c.

993 {
994 if (s->retained) {
995 if (status > 0 && status < 400) {
996 s->scope.failed = false;
997 if (s->scope.span) {
998 (void)otel_status(
999 otlp_span_set_status(s->scope.span, OTLP_STATUS_CODE_UNSET, ""),
1000 "otlp_span_set_status");
1001 gotel_attr_string(&s->scope, "error.type", "");
1002 }
1003 }
1004 gotel_http_attributes(&s->scope, "GET", status);
1005 }
1006}

References ascii_otel_scope::retained, and ascii_otel_scope::scope.

Referenced by https_get().

◆ ascii_otel_init()

asciichat_error_t ascii_otel_init ( const char * mode,
int argc,
char ** argv )

Definition at line 648 of file otel.c.

648 {
649 // Test mode is a hard export prohibition, including explicit endpoints and
650 // CLI overrides.
651 if (is_test_environment())
652 return ASCIICHAT_OK;
653 const options_t *o = options_get();
654 if (!o)
655 return ASCIICHAT_OK;
656 const char *endpoint = o->otel_endpoint;
657 if (!endpoint || endpoint[0] == '\0')
658 return ASCIICHAT_OK;
659 bool endpoint_cli = false, timeout_cli = false;
660 for (int i = 1; i < argc; ++i) {
661 endpoint_cli |= !strcmp(argv[i], "--otel-endpoint") ||
662 !strncmp(argv[i], "--otel-endpoint=", 16);
663 timeout_cli |= !strcmp(argv[i], "--otel-timeout") ||
664 !strncmp(argv[i], "--otel-timeout=", 15);
665 }
666 unsigned signals;
667 asciichat_error_t parsed =
668 otel_status(gotel_parse_signals(o->otel_enabled_telemetry, &signals),
669 "Parse otel-enabled-telemetry (traces,logs,metrics or none)");
670 if (parsed != ASCIICHAT_OK)
671 return parsed;
672 if (!isfinite(o->otel_realtime_sample_rate) ||
674 o->otel_queue_capacity < 2 ||
676 o->otel_timeout < 1 || o->otel_metrics_interval < 1 ||
678 return SET_ERRNO(ERROR_CONFIG,
679 "Invalid telemetry sampling, queue capacity, or interval");
680 const char *log_names[] = {"dev", "debug", "info", "warn", "error", "fatal"};
681 const int log_levels[] = {LOG_DEV, LOG_DEBUG, LOG_INFO,
683 minimum_log_level = -1;
684 for (size_t i = 0; i < sizeof(log_levels) / sizeof(log_levels[0]); ++i)
685 if (strcmp(o->otel_log_level, log_names[i]) == 0)
686 minimum_log_level = log_levels[i];
687 if (minimum_log_level < 0)
688 return SET_ERRNO(ERROR_CONFIG, "Invalid otel-log-level");
689 if (!signals)
690 return ASCIICHAT_OK;
691 gotel_options_t config = {0};
692 config.application_name = "ascii-chat";
693 config.disable_span_name_prefix = true;
694 char service_name[OPTIONS_BUFF_SIZE];
695 int n =
696 safe_snprintf(service_name, sizeof(service_name), "ascii-chat-%s", mode);
697 if (n < 0 || (size_t)n >= sizeof(service_name))
698 return SET_ERRNO(ERROR_CONFIG, "Telemetry service name too long");
699 config.exporter.service_name = service_name;
700 // The exporter observes its batch interval during final flush as well.
701 // Keep it short enough to deliver all signals within the shutdown budget.
702 config.exporter.batch_ms = 25;
703 config.exporter.queue_capacity = (size_t)o->otel_queue_capacity;
704 config.exporter.connect_timeout_ms = (uint32_t)o->otel_timeout;
705 config.exporter.read_timeout_ms = (uint32_t)o->otel_timeout;
706 config.exporter.flush_timeout_ms = (uint32_t)o->otel_timeout;
707 otlp_resource_attr_t resources[] = {
708 {.key = "ascii_chat.mode",
709 .value = {.type = OTLP_VALUE_STRING, .v = {.string_val = mode}}},
710 {.key = "service.version",
711 .value = {.type = OTLP_VALUE_STRING,
712 .v = {.string_val = ASCII_CHAT_VERSION_FULL}}},
713 };
714 config.exporter.resource_attributes = resources;
715 config.exporter.n_resource_attributes =
716 sizeof(resources) / sizeof(resources[0]);
717 config.signals = signals;
718 config.realtime_sample_rate = o->otel_realtime_sample_rate;
719 config.realtime_max_per_second = (uint32_t)o->otel_realtime_max_per_second;
720 config.metrics_interval_ms = (uint32_t)o->otel_metrics_interval;
721 config.monotonic_ns = time_get_ns;
722 config.realtime_ns = telemetry_realtime_ns;
723 config.operations = operations;
724 config.operation_count = ASCII_OTEL_OPERATION_COUNT;
725 config.exporter_event = exporter_event;
726 config.metrics = metrics;
727 config.metric_count = ASCII_OTEL_METRIC_COUNT;
728#ifndef NDEBUG
729 config.watchdog.snapshot = watchdog_snapshot;
730 config.watchdog.decorate = watchdog_decorate;
731 config.watchdog.operation = ASCII_OTEL_THREAD_DEADLOCK;
732 config.watchdog.metric = ASCII_OTEL_METRIC_DEADLOCK_THREADS;
733#endif
734 gotel_command_args_t command_args = {0};
735 // Addresses are positional in native client/server modes, with optional
736 // ports.
737 char address_port[OPTIONS_BUFF_SIZE + 32],
738 address6_port[OPTIONS_BUFF_SIZE + 32];
739 char address_brackets[OPTIONS_BUFF_SIZE + 32],
740 address6_brackets[OPTIONS_BUFF_SIZE + 32];
741 char address_brackets_port[OPTIONS_BUFF_SIZE + 32],
742 address6_brackets_port[OPTIONS_BUFF_SIZE + 32];
743 SAFE_SNPRINTF(address_port, sizeof(address_port), "%s:%d", o->address,
744 o->port);
745 SAFE_SNPRINTF(address6_port, sizeof(address6_port), "%s:%d", o->address6,
746 o->port);
747 SAFE_SNPRINTF(address_brackets, sizeof(address_brackets), "[%s]", o->address);
748 SAFE_SNPRINTF(address6_brackets, sizeof(address6_brackets), "[%s]",
749 o->address6);
750 SAFE_SNPRINTF(address_brackets_port, sizeof(address_brackets_port), "[%s]:%d",
751 o->address, o->port);
752 SAFE_SNPRINTF(address6_brackets_port, sizeof(address6_brackets_port),
753 "[%s]:%d", o->address6, o->port);
754 const char *sensitive_values[] = {o->address,
755 o->address6,
756 address_port,
757 address6_port,
758 address_brackets,
759 address6_brackets,
760 address_brackets_port,
761 address6_brackets_port};
762 gotel_command_policy_t argument_policy = {
763#ifdef NDEBUG
764 .mode = GOTEL_COMMAND_REDACT_SELECTED,
765#else
766 .mode = GOTEL_COMMAND_REDACT_NONE,
767#endif
768 .rules = command_argument_rules,
769 .sensitive_values = sensitive_values,
770 .sensitive_value_count =
771 sizeof(sensitive_values) / sizeof(sensitive_values[0]),
772 .rule_count =
773 sizeof(command_argument_rules) / sizeof(command_argument_rules[0]),
774 };
775 asciichat_error_t arguments_status =
776 otel_status(gotel_command_args_init_policy(
777 &command_args, "ascii-chat", mode, argc,
778 (const char *const *)argv, &argument_policy),
779 "Sanitize telemetry command arguments");
780 if (arguments_status != ASCIICHAT_OK)
781 return arguments_status;
782 config.argc = command_args.argc;
783 config.argv = command_args.argv;
784 gotel_env_storage_t env;
785 asciichat_error_t env_status = otel_status(
786 gotel_options_apply_env(&config, &env, OPT_OTEL_ENDPOINT_DEFAULT),
787 "Configure OTEL environment");
788 if (env_status == ASCIICHAT_OK &&
789 (endpoint_cli || SAFE_GETENV("ASCII_CHAT_OTEL_ENDPOINT") ||
790 strcmp(endpoint, OPT_OTEL_ENDPOINT_DEFAULT)))
791 env_status =
792 otel_status(gotel_options_set_endpoint(&config, &env, endpoint),
793 "Configure telemetry endpoint");
794 if (timeout_cli || SAFE_GETENV("ASCII_CHAT_OTEL_TIMEOUT")) {
795 config.exporter.connect_timeout_ms = (uint32_t)o->otel_timeout;
796 config.exporter.read_timeout_ms = (uint32_t)o->otel_timeout;
797 config.exporter.flush_timeout_ms = (uint32_t)o->otel_timeout;
798 }
799 // The final drain runs on the exporter worker. Keep its network waits
800 // interruptible by the shutdown deadline even when the collector is down.
801 const uint32_t shutdown_safe_timeout_ms = 500;
802 if (config.exporter.connect_timeout_ms > shutdown_safe_timeout_ms)
803 config.exporter.connect_timeout_ms = shutdown_safe_timeout_ms;
804 if (config.exporter.read_timeout_ms > shutdown_safe_timeout_ms)
805 config.exporter.read_timeout_ms = shutdown_safe_timeout_ms;
806 if (config.exporter.flush_timeout_ms > shutdown_safe_timeout_ms)
807 config.exporter.flush_timeout_ms = shutdown_safe_timeout_ms;
808 if (*o->otel_ca_file)
809 config.tls_ca_file = o->otel_ca_file;
810 if (env_status != ASCIICHAT_OK || !config.signals ||
811 !config.exporter.endpoint) {
812 gotel_command_args_destroy(&command_args);
813 return env_status;
814 }
815 gotel_host_t *h = NULL;
816 asciichat_error_t created =
817 otel_status(gotel_create(&config, &h), "Initialize telemetry exporter");
818 if (created != ASCIICHAT_OK) {
819 gotel_command_args_destroy(&command_args);
820 return created;
821 }
822 gotel_set_logger(h, exporter_diagnostic, NULL);
823 shutdown_timeout_ns = (uint64_t)o->otel_timeout * NS_PER_MS_INT;
824 atomic_store(&stopping, false);
825 atomic_store(&exporter_queue_capacity, config.exporter.queue_capacity);
826 atomic_store(&worker_done, false);
827 if (platform_thread_create(&worker, "otel_export", export_worker, h) != 0) {
828 gotel_destroy(h);
829 gotel_command_args_destroy(&command_args);
830 return SET_ERRNO(ERROR_THREAD, "Cannot start telemetry exporter");
831 }
832 worker_started = true;
833 (void)atexit(ascii_otel_shutdown);
834 atomic_ptr_store(&host, h);
835 gotel_begin(h, &command_scope, ASCII_OTEL_CLI, NULL);
836 (void)otel_status(gotel_command_args_set_span(&command_scope, &command_args),
837 "Attach telemetry command arguments");
838 gotel_command_args_destroy(&command_args);
839
840 log_info("Telemetry enabled: %s (%s)", config.exporter.endpoint,
842 return ASCIICHAT_OK;
843}
#define SET_ERRNO(code,...)
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
unsigned int uint32_t
#define SAFE_GETENV(name)
#define SAFE_SNPRINTF(buffer, buffer_size,...)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_THREAD
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define NS_PER_MS_INT
Definition time.h:172
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
#define OPT_OTEL_ENDPOINT_DEFAULT
Debug and Dev builds export to the local Spaniel Compose collector.
Definition options.h:349
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:513
#define OPTIONS_BUFF_SIZE
Buffer size for option string values.
Definition options.h:305
#define LOG_DEV
Definition log/types.h:42
#define LOG_DEBUG
Definition log/types.h:43
#define LOG_FATAL
Definition log/types.h:47
#define LOG_ERROR
Definition log/types.h:46
#define LOG_WARN
Definition log/types.h:45
#define LOG_INFO
Definition log/types.h:44
void ascii_otel_shutdown(void)
Definition otel.c:548
char otel_ca_file[256]
Definition options.h:1152
int otel_realtime_max_per_second
Definition options.h:1155
double otel_realtime_sample_rate
Definition options.h:1154
int otel_metrics_interval
Definition options.h:1156
int otel_queue_capacity
Definition options.h:1158
char otel_endpoint[256]
Definition options.h:1151
char otel_log_level[256]
Definition options.h:1150
char address6[256]
IPv6 bind address (server only).
Definition options.h:1003
int port
Server port number.
Definition options.h:1004
char otel_enabled_telemetry[256]
Definition options.h:1153
char address[256]
Server address (client) or bind address (server).
Definition options.h:1002

References options_state::address, options_state::address6, ASCII_OTEL_METRIC_COUNT, ASCII_OTEL_OPERATION_COUNT, ascii_otel_shutdown(), ASCIICHAT_OK, atomic_ptr_store(), ERROR_CONFIG, ERROR_THREAD, LOG_DEBUG, LOG_DEV, LOG_ERROR, LOG_FATAL, LOG_INFO, log_info, LOG_WARN, NS_PER_MS_INT, OPT_OTEL_ENDPOINT_DEFAULT, OPTIONS_BUFF_SIZE, options_get(), options_state::otel_ca_file, options_state::otel_enabled_telemetry, options_state::otel_endpoint, options_state::otel_log_level, options_state::otel_metrics_interval, options_state::otel_queue_capacity, options_state::otel_realtime_max_per_second, options_state::otel_realtime_sample_rate, options_state::otel_timeout, options_state::port, SAFE_GETENV, SAFE_SNPRINTF, and SET_ERRNO.

◆ ascii_otel_log()

void ascii_otel_log ( int level,
const char * file,
int line,
const char * function,
const char * format,
va_list args )

Definition at line 844 of file otel.c.

845 {
846 (void)file;
847 (void)line;
848 (void)function;
849 if (in_log || level < minimum_log_level || !atomic_ptr_load(&host))
850 return;
851 in_log = true;
852 gotel_host_t *h = retain();
853 if (h) {
854 char body[OTEL_BODY_BUFFER_SIZE];
855 int n = safe_vsnprintf(body, sizeof(body), format, args);
856 if (n >= 0) {
857 url_redact_diagnostic(body, (size_t)n >= sizeof(body));
858 otlp_severity_t severity = level >= LOG_FATAL ? OTLP_SEVERITY_FATAL
859 : level >= LOG_ERROR ? OTLP_SEVERITY_ERROR
860 : level >= LOG_WARN ? OTLP_SEVERITY_WARN
861 : level >= LOG_INFO ? OTLP_SEVERITY_INFO
862 : level >= LOG_DEBUG ? OTLP_SEVERITY_DEBUG
863 : OTLP_SEVERITY_TRACE;
864 gotel_log(h, severity, body, file, line, function);
865 }
866 release();
867 }
868 in_log = false;
869}
action_args_t args

References args, atomic_ptr_load(), LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_WARN, OTEL_BODY_BUFFER_SIZE, and url_redact_diagnostic().

Referenced by log_msg().

◆ ascii_otel_metric()

◆ ascii_otel_packet()

void ascii_otel_packet ( ascii_otel_scope_t * scope,
uint16_t type,
size_t payload_size,
const char * action,
int transport )

Definition at line 524 of file otel.c.

525 {
526 if (!scope->retained || !scope->scope.span)
527 return;
528 const char *packet = acip_packet_type_name(type);
530 safe_snprintf(name, sizeof(name), "%s.%s",
531 operations[scope->scope.operation].name, packet);
532 safe_snprintf(version, sizeof(version), "%d.%d.%d", PROTOCOL_VERSION_MAJOR,
534 (void)otel_status(otlp_span_set_name(scope->scope.span, name),
535 "otlp_span_set_name");
536 gotel_attr_string(&scope->scope, "network.protocol.name", "acip");
537 gotel_attr_string(&scope->scope, "network.protocol.version", version);
538 gotel_attr_string(&scope->scope, "network.packet.name", packet);
539 gotel_attr_int(&scope->scope, "network.packet.type", type);
540 gotel_attr_int(&scope->scope, "network.packet.size", (int64_t)payload_size);
541 gotel_attr_string(&scope->scope, "network.packet.operation", action);
542 const char *carrier = transport == ACIP_TRANSPORT_TCP ? "tcp"
543 : transport == ACIP_TRANSPORT_WEBSOCKET ? "websocket"
544 : transport == ACIP_TRANSPORT_WEBRTC ? "webrtc"
545 : "unknown";
546 gotel_attr_string(&scope->scope, "acip.transport", carrier);
547}
#define PROTOCOL_VERSION_MAJOR
Major protocol version number.
#define PROTOCOL_VERSION_MINOR
Minor protocol version number.
#define PROTOCOL_VERSION_PATCH
Patch protocol version number.
const char * acip_packet_type_name(uint16_t type)
Get human-readable name for packet type.
Definition handlers.c:579
@ ACIP_TRANSPORT_TCP
Raw TCP socket.
@ ACIP_TRANSPORT_WEBSOCKET
WebSocket over TCP.
@ ACIP_TRANSPORT_WEBRTC
WebRTC DataChannel (P2P).
#define OTEL_VERSION_BUFFER_SIZE
Definition otel.c:6
#define OTEL_NAME_BUFFER_SIZE
Definition otel.c:5
gotel_scope_t scope
Definition otel.h:32
bool retained
Definition otel.h:33

References acip_packet_type_name(), ACIP_TRANSPORT_TCP, ACIP_TRANSPORT_WEBRTC, ACIP_TRANSPORT_WEBSOCKET, action, OTEL_NAME_BUFFER_SIZE, OTEL_VERSION_BUFFER_SIZE, PROTOCOL_VERSION_MAJOR, PROTOCOL_VERSION_MINOR, PROTOCOL_VERSION_PATCH, ascii_otel_scope::retained, and ascii_otel_scope::scope.

◆ ascii_otel_pipeline_age()

double ascii_otel_pipeline_age ( uint64_t now,
uint64_t capture,
uint64_t progress,
bool expected )

Definition at line 324 of file otel.c.

325 {
326 return expected && capture && progress && now >= capture && now >= progress &&
327 now - capture < 2 * NS_PER_SEC_INT
328 ? (double)(now - progress) / 1e9
329 : 0;
330}
#define NS_PER_SEC_INT
Definition time.h:173

References NS_PER_SEC_INT.

◆ ascii_otel_pipeline_progress()

void ascii_otel_pipeline_progress ( unsigned stage,
unsigned expected )

Definition at line 298 of file otel.c.

298 {
299 if (!ascii_otel_enabled())
300 return;
301 uint64_t now = time_get_ns();
302 if (stage == 0) {
303 if (!atomic_load(&pipeline_capture_ns)) {
304 atomic_store(&pipeline_display_ns, now);
305 atomic_store(&pipeline_encode_ns, now);
306 }
307 if (!(expected & 1))
308 atomic_store(&pipeline_display_ns, now);
309 if (!(expected & 2))
310 atomic_store(&pipeline_encode_ns, now);
311 atomic_store(&pipeline_expected, expected);
312 atomic_store(&pipeline_capture_ns, now);
313 } else if (stage == 1)
314 atomic_store(&pipeline_display_ns, now);
315 else if (stage == 2)
316 atomic_store(&pipeline_encode_ns, now);
317 else {
318 atomic_store(&pipeline_expected, 0);
319 atomic_store(&pipeline_capture_ns, 0);
320 ascii_otel_metric(ASCII_OTEL_METRIC_PIPELINE_DISPLAY_STALL, 0);
321 ascii_otel_metric(ASCII_OTEL_METRIC_PIPELINE_ENCODE_STALL, 0);
322 }
323}

References ascii_otel_enabled(), and ascii_otel_metric().

Referenced by session_pipeline_destroy(), and session_pipeline_run_main().

◆ ascii_otel_request_shutdown()

void ascii_otel_request_shutdown ( void )

Request exporter cancellation without draining or freeing telemetry state.

Definition at line 172 of file otel.c.

172 {
173 atomic_store_explicit(&stopping, true, memory_order_release);
174}

Referenced by signal_exit().

◆ ascii_otel_scope_begin()

void ascii_otel_scope_begin ( ascii_otel_scope_t * scope,
ascii_otel_operation_t operation )

Definition at line 445 of file otel.c.

445 {
446 ascii_otel_scope_parent(s, op, NULL);
447}
void ascii_otel_scope_parent(ascii_otel_scope_t *s, ascii_otel_operation_t op, const gotel_context_t *parent)
Definition otel.c:448

References ascii_otel_scope_parent().

◆ ascii_otel_scope_end()

void ascii_otel_scope_end ( ascii_otel_scope_t * scope)

Definition at line 497 of file otel.c.

497 {
498 if (!s || !s->retained)
499 return;
500 gotel_end(&s->scope);
501 if (current_scope == s)
502 current_scope = s->previous;
503 s->retained = false;
504 release();
505}

References ascii_otel_scope::previous, ascii_otel_scope::retained, and ascii_otel_scope::scope.

Referenced by ascii_otel_error_code(), and ascii_otel_frame_rejected().

◆ ascii_otel_scope_int()

void ascii_otel_scope_int ( ascii_otel_scope_t * scope,
const char * key,
int64_t value )

Definition at line 871 of file otel.c.

872 {
873 if (s->retained)
874 gotel_attr_int(&s->scope, key, value);
875}

References ascii_otel_scope::retained, and ascii_otel_scope::scope.

Referenced by session_pipeline_run_main().

◆ ascii_otel_scope_parent()

void ascii_otel_scope_parent ( ascii_otel_scope_t * scope,
ascii_otel_operation_t operation,
const gotel_context_t * parent )

Definition at line 448 of file otel.c.

449 {
450 gotel_host_t *h = retain();
451 s->retained = h != NULL;
452 if (h) {
453 gotel_context_t context = gotel_current_context();
454 bool lifetime_parent =
455 current_scope &&
456 (current_scope->scope.operation == ASCII_OTEL_SESSION_CLIENT_LIKE_RUN ||
457 current_scope->scope.operation == ASCII_OTEL_SESSION_ATTEMPT);
458 // Lifetime spans finish only at shutdown. Completed operations must remain
459 // inspectable while a server is running, including after abrupt
460 // termination.
461 if (!parent && context.valid) {
462 gotel_parent_mode_t mode =
463 lifetime_parent ||
464 memcmp(context.span_id, command_scope.context.span_id,
465 sizeof(context.span_id)) == 0
466 ? GOTEL_PARENT_INDEPENDENT
467 : GOTEL_PARENT_BOUNDED;
468 context = gotel_operation_parent(context, mode);
469 parent = &context;
470 }
471 gotel_begin(h, &s->scope, (size_t)op, parent);
472 s->previous = current_scope;
473 current_scope = s;
474 }
475}

References ascii_otel_scope::previous, ascii_otel_scope::retained, and ascii_otel_scope::scope.

Referenced by ascii_otel_scope_begin().

◆ ascii_otel_scope_string()

void ascii_otel_scope_string ( ascii_otel_scope_t * scope,
const char * key,
const char * value )

Definition at line 876 of file otel.c.

877 {
878 if (s->retained)
879 gotel_attr_string(&s->scope, key, value);
880}

References ascii_otel_scope::retained, and ascii_otel_scope::scope.

◆ ascii_otel_shutdown()

void ascii_otel_shutdown ( void )

Definition at line 548 of file otel.c.

548 {
549 atomic_store_explicit(&stopping, true, memory_order_release);
550 gotel_host_t *h = atomic_ptr_exchange(&host, NULL);
551 if (!h)
552 return;
553 // The command span may still reference exporter-owned state while a
554 // shutdown signal is being handled on another thread. Do not close it on
555 // the exit thread; the exporter owns the remaining queued data and process
556 // teardown reclaims the span memory.
557 uint64_t started = time_get_ns();
558 // Leave time for the rest of process teardown, even if the collector stalls.
559 // Keep the application-side budget below one second while allowing the
560 // exporter its bounded 500 ms interruptible network drain.
561 uint64_t budget_ns = shutdown_timeout_ns < 900 * NS_PER_MS_INT
562 ? shutdown_timeout_ns
563 : 900 * NS_PER_MS_INT;
564 bool producers_active =
565 atomic_load_explicit(&readers, memory_order_seq_cst) != 0;
566 if (worker_started) {
567 // Interrupt any exporter request before waiting for the worker. Waiting
568 // first makes cancellation unreachable when the worker is blocked in
569 // DNS/TLS/HTTP, which turns a bounded shutdown into a process hang.
570 (void)otel_status(gotel_drain(h), "Drain telemetry exporter");
571 while (!atomic_load_explicit(&worker_done, memory_order_acquire) &&
572 time_get_ns() - started < budget_ns)
573 platform_sleep_ms(1);
574 bool done = atomic_load_explicit(&worker_done, memory_order_acquire);
575 uint64_t elapsed_ns = time_get_ns() - started;
576 int joined = -1;
577 if (done && elapsed_ns < budget_ns) {
578#if !defined(__linux__) && !defined(_WIN32)
579 // The POSIX timed join implementation blocks on these platforms. The
580 // worker has finished using the exporter, so detach its remaining wrapper
581 // cleanup.
582 joined = thread_detach(&worker);
583#else
584 joined = thread_join_timeout(&worker, NULL, budget_ns - elapsed_ns);
585#endif
586 }
587 if (joined != 0) {
588 log_warn("Telemetry worker did not stop within its deadline; exporter "
589 "retained until process exit");
590 return;
591 }
592 worker_started = false;
593 }
594 asciichat_error_t result = drain_result;
595 otlp_exporter_stats_t stats = {0};
596 (void)otel_status(gotel_stats(h, &stats), "gotel_stats");
597 // exit() can bypass scope cleanup in a producer. Do not free its host while
598 // it remains borrowed; the process will reclaim it after atexit handlers.
599 if (!producers_active)
600 gotel_destroy(h);
601 else
602 log_warn("Telemetry producer still active during process shutdown; "
603 "exporter retained");
604 if (result != ASCIICHAT_OK)
605 log_warn("Telemetry flush deadline reached; pending records discarded");
606 log_debug("Telemetry exporter stopped: spans=%llu/%llu logs=%llu/%llu "
607 "metrics=%llu/%llu network_errors=%llu "
608 "http_4xx=%llu dropped=%llu rejected=%llu",
609 (unsigned long long)stats.sent, (unsigned long long)stats.emitted,
610 (unsigned long long)stats.sent_logs,
611 (unsigned long long)stats.emitted_logs,
612 (unsigned long long)stats.sent_metrics,
613 (unsigned long long)stats.emitted_metrics,
614 (unsigned long long)stats.network_err,
615 (unsigned long long)stats.http_4xx,
616 (unsigned long long)(stats.dropped_full + stats.dropped_err),
617 (unsigned long long)stats.rejected_spans);
618}
void * atomic_ptr_exchange(atomic_ptr_t *a, void *new_value)
Atomically exchange a pointer and return the old value.
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565

References ASCIICHAT_OK, atomic_ptr_exchange(), log_debug, log_warn, and NS_PER_MS_INT.

Referenced by ascii_otel_init().

◆ ascii_otel_sqlite()

void ascii_otel_sqlite ( sqlite3 * db,
const gotel_query_t * queries,
size_t count )

Definition at line 1012 of file otel.c.

1013 {
1014 gotel_host_t *h = retain();
1015 if (!h)
1016 return;
1017 gotel_sqlite_t *a = NULL;
1018 sequel_analysis_options_t analysis = {.format = true};
1019 gotel_sqlite_options_t options = {.analysis = &analysis};
1020 if (otel_status(gotel_sqlite_attach_with_options(h, db, "ascii-chat.storage",
1021 queries, count, &options,
1022 &a),
1023 "Attach SQLite telemetry") != ASCIICHAT_OK)
1024 log_warn("Could not attach SQLite telemetry");
1025 release();
1026}

References ASCIICHAT_OK, and log_warn.

◆ ascii_otel_sqlite_catalog()

void ascii_otel_sqlite_catalog ( sqlite3 * db,
const gotel_query_catalog_t * catalog )

The catalog accessor and its context must remain valid until the DB closes.

Definition at line 1028 of file otel.c.

1029 {
1030 gotel_host_t *h = retain();
1031 if (!h)
1032 return;
1033 gotel_sqlite_t *a = NULL;
1034 sequel_analysis_options_t analysis = {.format = true};
1035 gotel_sqlite_options_t options = {.analysis = &analysis};
1036 if (otel_status(gotel_sqlite_attach_catalog_with_options(
1037 h, db, "ascii-chat.storage", catalog, &options, &a),
1038 "Attach SQLite telemetry") != ASCIICHAT_OK)
1039 log_warn("Could not attach SQLite telemetry");
1040 release();
1041}

References ASCIICHAT_OK, and log_warn.

Referenced by database_init().

◆ ascii_otel_sqlite_prepare()

◆ ascii_otel_stats_attach()

void ascii_otel_stats_attach ( stats_scope_t * scope)

Definition at line 237 of file otel.c.

237 {
238 stats_scope_set_observer(scope, stats_metric_record, NULL);
239}
void stats_scope_set_observer(stats_scope_t *scope, stats_observer_fn observer, void *context)

References stats_scope_set_observer().

Referenced by stats_runtime_start().

◆ ascii_otel_stats_metric()

ascii_otel_metric_t ascii_otel_stats_metric ( stats_record_kind_t kind,
unsigned id )

Definition at line 197 of file otel.c.

198 {
199 if (!((kind == STATS_RECORD_COUNTER && id < STATS_COUNTER_COUNT) ||
200 (kind == STATS_RECORD_GAUGE && id < STATS_GAUGE_COUNT) ||
203 ascii_otel_metric_t metric;
204 switch (((unsigned)kind << 16) | id) {
205#define ASCII_STATS_COUNTER(id, metric_id, name, unit) \
206 case (STATS_RECORD_COUNTER << 16) | STATS_COUNTER_##id: \
207 metric = ASCII_OTEL_METRIC_##metric_id; \
208 break;
209#define ASCII_STATS_GAUGE(id, metric_id, name, unit) \
210 case (STATS_RECORD_GAUGE << 16) | STATS_GAUGE_##id: \
211 metric = ASCII_OTEL_METRIC_##metric_id; \
212 break;
213#define ASCII_STATS_DURATION(id, metric_id, name, unit) \
214 case (STATS_RECORD_DURATION << 16) | STATS_DURATION_##id: \
215 metric = ASCII_OTEL_METRIC_##metric_id; \
216 break;
217#include <ascii-chat/stats/telemetry.def>
218#undef ASCII_STATS_COUNTER
219#undef ASCII_STATS_GAUGE
220#undef ASCII_STATS_DURATION
221 default:
223 }
224 return metric;
225}

References ASCII_OTEL_METRIC_COUNT, STATS_COUNTER_COUNT, STATS_DURATION_COUNT, STATS_GAUGE_COUNT, STATS_RECORD_COUNTER, STATS_RECORD_DURATION, and STATS_RECORD_GAUGE.

◆ ascii_otel_ui_usage()

void ascii_otel_ui_usage ( const char * screen,
const char * event,
uint64_t duration_ns )

Definition at line 506 of file otel.c.

507 {
508 // UI transitions must not inherit realtime media sampling decisions.
509 gotel_context_t root = {0};
510 ASCII_OTEL_SCOPE_PARENT(UI_USAGE, &root);
511 ASCII_OTEL_ATTR_STRING("ascii_chat.ui.screen", screen);
512 ASCII_OTEL_ATTR_STRING("ascii_chat.ui.event", event);
513 ASCII_OTEL_ATTR_INT("ascii_chat.ui.duration_ns", (int64_t)duration_ns);
514}

References ASCII_OTEL_ATTR_INT, ASCII_OTEL_ATTR_STRING, and ASCII_OTEL_SCOPE_PARENT.

Referenced by keyboard_help_toggle_terminal(), log_search_enter_mode(), log_search_exit_mode(), terminal_screen_logs_handle_key_sized(), ui_error_popup_key(), ui_input_media_take(), ui_input_read_key(), update_banner_set_result(), and update_banner_toggle_overlay().