{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/shaogme/diagweave/schemas/report-otel-v0.1.0.schema.json",
  "title": "diagweave OTEL Envelope v0.1.0",
  "$defs": {
    "trace_id": {
      "type": "string",
      "pattern": "^[0-9A-Fa-f]{32}$"
    },
    "span_id": {
      "type": "string",
      "pattern": "^[0-9A-Fa-f]{16}$"
    },
    "severity_number": {
      "type": "integer",
      "enum": [1, 5, 9, 13, 17, 21]
    },
    "trace_context": {
      "type": "object",
      "additionalProperties": false,
      "description": "Fixed top-level trace-context object on each OtelEvent. This is not emitted as an attribute.",
      "properties": {
        "parent_span_id": { "$ref": "#/$defs/span_id" },
        "trace_state": { "type": "string" }
      }
    },
    "otel_attribute": {
      "type": "object",
      "description": "OTEL attribute key/value pair. For diagnostic source chains, diagweave emits `diagnostic_bag.origin_source_errors` and `diagnostic_bag.diagnostic_source_errors` as structured OtelValue objects with roots/nodes/source_roots/truncated/cycle_detected fields. The `exception.raw_data` attribute contains the full structured error data (message + type) for complete context preservation.",
      "additionalProperties": false,
      "required": ["key", "value"],
      "properties": {
        "key": {
            "type": "string",
            "description": "Examples include `exception.type`, `exception.message`, `exception.raw_data`, `diagnostic_bag.display_causes`, `diagnostic_bag.origin_source_errors`, and `diagnostic_bag.diagnostic_source_errors`."
        },
        "value": { "$ref": "#/$defs/otel_value" }
      }
    },
    "otel_value": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["String"],
          "properties": {
            "String": { "type": "string" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["Int"],
          "properties": {
            "Int": { "type": "integer" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["U64"],
          "properties": {
            "U64": { "type": "integer", "minimum": 0 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["Double"],
          "properties": {
            "Double": { "type": "number" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["Bool"],
          "properties": {
            "Bool": { "type": "boolean" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["Bytes"],
          "properties": {
            "Bytes": {
              "type": "array",
              "items": { "type": "integer", "minimum": 0, "maximum": 255 }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["Array"],
          "properties": {
            "Array": {
              "type": "array",
              "items": { "$ref": "#/$defs/otel_value" }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["KvList"],
          "properties": {
            "KvList": {
              "type": "array",
              "items": { "$ref": "#/$defs/otel_attribute" }
            }
          }
        }
      ]
    },
    "otel_event": {
    	"type": "object",
    	"additionalProperties": false,
    	"required": ["name", "attributes"],
      "properties": {
        "name": { "type": "string" },
        "body": {
          "description": "Human-readable error message string for the primary exception record, or omitted for trace-event records. Per OTel Semantic Conventions, this should be a plain string rather than a structured object.",
          "$ref": "#/$defs/otel_value"
        },
        "timestamp_unix_nano": { "type": "integer", "minimum": 0 },
        "observed_timestamp_unix_nano": { "type": "integer", "minimum": 0 },
        "severity_text": { "type": "string" },
        "severity_number": {
          "$ref": "#/$defs/severity_number"
        },
        "trace_id": {
          "$ref": "#/$defs/trace_id"
        },
        "span_id": {
          "$ref": "#/$defs/span_id"
        },
        "trace_sampled": {
          "type": "boolean"
        },
        "trace_context": {
          "description": "Fixed top-level trace-context object for the record. When present, it carries parent span metadata and trace state directly on the event.",
          "$ref": "#/$defs/trace_context"
        },
        "attributes": {
          "type": "array",
          "items": { "$ref": "#/$defs/otel_attribute" }
        }
      }
    }
  },
  "type": "object",
  "additionalProperties": false,
  "required": ["records"],
  "properties": {
    "records": {
      "type": "array",
      "items": { "$ref": "#/$defs/otel_event" }
    }
  }
}
