//
// Created by Carl.Guo on 2024/5/14.
//
#include "bpf_base.h"

#ifndef EUSPACES_APM_TRACE_H
#define EUSPACES_APM_TRACE_H

#define APM_REMOTE_ADDR_MAX_LEN 256

#define APM_TYPE_FROM_STRING_SIZE 2
#define APM_SAMPLE_STRING_SIZE 2
#define APM_HOST_ID_STRING_SIZE 16
#define APM_APP_ID_STRING_SIZE 16
#define APM_INSTANCE_ID_STRING_SIZE 16
#define APM_TRACE_ID_STRING_SIZE 32
#define APM_ASSUMED_APP_ID_STRING_SIZE 16
#define APM_SPAN_ID_STRING_SIZE 16

#define APM_TYPE_FROM_SIZE 1
#define APM_SAMPLE_SIZE 1
#define APM_HOST_ID_SIZE 8
#define APM_APP_ID_SIZE 8
#define APM_INSTANCE_ID_SIZE 8
#define APM_TRACE_ID_SIZE 16
#define APM_ASSUMED_APP_ID_SIZE 8
#define APM_SPAN_ID_SIZE 8

// cwtrace

#define CW_HEADER_KEY_LENGTH 7
#define CW_HEADER_KEY_VAL "cwtrace"
#define CW_HEADER_KEY_UFIRST_VAL "Cwtrace"
#define CW_HEADER_VAL_LENGTH 123

#define CW_HEADER_LENGTH 123

#define CW_STREAM_HEADER_LEN CW_HEADER_KEY_LENGTH + 2 + CW_HEADER_VAL_LENGTH + 2

/***********************************************************
 * Trace struct
 ***********************************************************/
struct apm_trace_key_t {
	__u32 tgid;
	__u32 pid;
	__u64 goid;
};

struct fd_trace_key_t {
	__u32 tgid;
	__u32 fd;
};

struct thread_trace_key_t {
	__u64 pid_tgid;
};


//struct apm_span_context {
//	unsigned char TraceID[APM_TRACE_ID_SIZE];
//	unsigned char SpanID[APM_SPAN_ID_SIZE];
//};

struct apm_span_context {
	unsigned char type_from[APM_TYPE_FROM_SIZE];
	unsigned char sample[APM_SAMPLE_SIZE];
	unsigned char host_id[APM_HOST_ID_SIZE];
	unsigned char app_id[APM_APP_ID_SIZE];
	unsigned char instance_id[APM_INSTANCE_ID_SIZE];
	unsigned char trace_id[APM_TRACE_ID_SIZE];
	unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
	unsigned char span_id[APM_SPAN_ID_SIZE];
};

struct apm_trace_info_t {
	/*
	 * Whether traceID is zero ?
	 * For the client to actively send request, set traceID to zero.
	 */
	//	bool is_trace_id_zero;
	//	__u32 update_time; // 从系统开机开始到创建/更新时的间隔时间单位是秒
	//	__u32 peer_fd;	   // 用于socket之间的关联
	//	__u64 thread_trace_id; // 线程追踪ID
	//	__u64 socket_id; // Records the socket associated when tracing was created (记录创建追踪时关联的socket)
	__u64 trace_id;
	struct apm_trace_key_t trace_key;

	struct fd_trace_key_t fd_trace_key;

	struct thread_trace_key_t thread_trace_key;

//	struct apm_span_context psc;

//	struct apm_span_context sc;
};

static __always_inline void cw_copy_byte_arrays(unsigned char *src, unsigned char *dst, __u32 size)
{
	for (int i = 0; i < size; i++)
	{
		dst[i] = src[i];
	}
}
#endif //EUSPACES_APM_TRACE_H
