var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import AnalyticsBaseModel from "./AnalyticsBaseModel/AnalyticsBaseModel";
import Route from "../../Types/API/Route";
import AnalyticsTableEngine from "../../Types/AnalyticsDatabase/AnalyticsTableEngine";
import AnalyticsTableName from "../../Types/AnalyticsDatabase/AnalyticsTableName";
import AnalyticsTableColumn, { SkipIndexType, } from "../../Types/AnalyticsDatabase/TableColumn";
import TableColumnType from "../../Types/AnalyticsDatabase/TableColumnType";
import OperationalResource from "../../Types/Database/AccessControl/OperationalResource";
import OwnedThrough from "../../Types/Database/AccessControl/OwnedThrough";
import Permission from "../../Types/Permission";
import Service from "../DatabaseModels/Service";
export var SpanKind;
(function (SpanKind) {
    SpanKind["Server"] = "SPAN_KIND_SERVER";
    SpanKind["Client"] = "SPAN_KIND_CLIENT";
    SpanKind["Producer"] = "SPAN_KIND_PRODUCER";
    SpanKind["Consumer"] = "SPAN_KIND_CONSUMER";
    SpanKind["Internal"] = "SPAN_KIND_INTERNAL";
})(SpanKind || (SpanKind = {}));
export var SpanEventType;
(function (SpanEventType) {
    SpanEventType["Exception"] = "exception";
    SpanEventType["Event"] = "event";
})(SpanEventType || (SpanEventType = {}));
export var SpanStatus;
(function (SpanStatus) {
    SpanStatus[SpanStatus["Unset"] = 0] = "Unset";
    SpanStatus[SpanStatus["Ok"] = 1] = "Ok";
    SpanStatus[SpanStatus["Error"] = 2] = "Error";
})(SpanStatus || (SpanStatus = {}));
let Span = class Span extends AnalyticsBaseModel {
    constructor() {
        const projectIdColumn = new AnalyticsTableColumn({
            key: "projectId",
            title: "Project ID",
            description: "ID of project",
            required: true,
            type: TableColumnType.ObjectID,
            isTenantId: true,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const primaryEntityIdColumn = new AnalyticsTableColumn({
            key: "primaryEntityId",
            title: "Service ID",
            description: "ID of the resource the span belongs to (Service / Host / DockerHost / KubernetesCluster / Monitor — disambiguated by primaryEntityType)",
            required: true,
            type: TableColumnType.ObjectID,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const primaryEntityTypeColumn = new AnalyticsTableColumn({
            key: "primaryEntityType",
            isLowCardinality: true,
            title: "Service Type",
            description: "Discriminator for primaryEntityId — tells the read side which resource table to dispatch to",
            required: false,
            type: TableColumnType.Text,
            skipIndex: {
                name: "idx_service_type",
                type: SkipIndexType.Set,
                params: [10],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const startTimeColumn = new AnalyticsTableColumn({
            key: "startTime",
            codec: [{ codec: "DoubleDelta" }, { codec: "ZSTD", level: 1 }],
            title: "Start Time",
            description: "When did the span start?",
            required: true,
            type: TableColumnType.DateTime64,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const endTimeColumn = new AnalyticsTableColumn({
            key: "endTime",
            /*
             * Plain ZSTD, deliberately NOT DoubleDelta: rows sort by startTime,
             * so consecutive endTime deltas jump with span duration and the
             * double-delta transform inflates instead of shrinking (measured
             * ~2.9x vs ~4-6x for plain ZSTD on endTimeUnixNano, same data).
             */
            codec: { codec: "ZSTD", level: 1 },
            title: "End Time",
            description: "When did the span end?",
            required: true,
            type: TableColumnType.DateTime64,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const startTimeUnixNanoColumn = new AnalyticsTableColumn({
            key: "startTimeUnixNano",
            title: "Start Time in Unix Nano",
            description: "When did the span start?",
            required: true,
            type: TableColumnType.UInt64,
            codec: [{ codec: "DoubleDelta" }, { codec: "ZSTD", level: 1 }],
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const durationUnixNanoColumn = new AnalyticsTableColumn({
            key: "durationUnixNano",
            title: "Duration in Unix Nano",
            description: "How long did the span last?",
            required: true,
            /*
             * Kept as LongNumber (Int128): it is aggregated as
             * AggregateFunction(avg, Int128) in proj_agg_by_service, which
             * ClickHouse cannot convert to UInt64 in place.
             */
            type: TableColumnType.LongNumber,
            codec: { codec: "ZSTD", level: 1 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const endTimeUnixNanoColumn = new AnalyticsTableColumn({
            key: "endTimeUnixNano",
            title: "End Time",
            description: "When did the span end?",
            required: true,
            type: TableColumnType.UInt64,
            /*
             * Plain ZSTD, deliberately NOT DoubleDelta: rows sort by
             * startTime, so endTime deltas jump with span duration and
             * double-delta inflates (measured compression ratio ~2.9 with
             * DoubleDelta vs ~4-6x plain ZSTD).
             */
            codec: { codec: "ZSTD", level: 1 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const traceIdColumn = new AnalyticsTableColumn({
            key: "traceId",
            title: "Trace ID",
            description: "ID of the trace",
            required: true,
            type: TableColumnType.Text,
            codec: { codec: "ZSTD", level: 1 },
            skipIndex: {
                name: "idx_trace_id",
                type: SkipIndexType.BloomFilter,
                params: [0.01],
                granularity: 1,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const spanIdColumn = new AnalyticsTableColumn({
            key: "spanId",
            title: "Span ID",
            description: "ID of the span",
            required: true,
            type: TableColumnType.Text,
            codec: { codec: "ZSTD", level: 1 },
            skipIndex: {
                name: "idx_span_id",
                type: SkipIndexType.BloomFilter,
                params: [0.01],
                granularity: 1,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const parentSpanIdColumn = new AnalyticsTableColumn({
            key: "parentSpanId",
            title: "Parent Span ID",
            description: "ID of the parent span",
            required: false,
            type: TableColumnType.Text,
            codec: { codec: "ZSTD", level: 1 },
            skipIndex: {
                name: "idx_parent_span_id",
                type: SkipIndexType.BloomFilter,
                params: [0.01],
                granularity: 1,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const traceStateColumn = new AnalyticsTableColumn({
            key: "traceState",
            title: "Trace State",
            description: "Trace State",
            required: false,
            type: TableColumnType.Text,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const attributesColumn = new AnalyticsTableColumn({
            key: "attributes",
            codec: { codec: "ZSTD", level: 3 },
            title: "Attributes",
            description: "Attributes",
            required: true,
            defaultValue: {},
            type: TableColumnType.MapStringString,
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const attributeKeysColumn = new AnalyticsTableColumn({
            key: "attributeKeys",
            codec: { codec: "ZSTD", level: 3 },
            title: "Attribute Keys",
            description: "Attribute keys extracted from attributes",
            required: true,
            defaultValue: [],
            type: TableColumnType.ArrayText,
            skipIndex: {
                name: "idx_attribute_keys",
                type: SkipIndexType.BloomFilter,
                params: [0.01],
                granularity: 1,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const entityKeysColumn = new AnalyticsTableColumn({
            key: "entityKeys",
            codec: { codec: "ZSTD", level: 3 },
            title: "Entity Keys",
            description: "Stable keys of every OpenTelemetry entity (service, host, k8s.pod, container, ...) this signal belongs to. A superset that includes the primary entity. Enables cross-cutting membership queries via has(entityKeys, :key).",
            required: true,
            defaultValue: [],
            type: TableColumnType.ArrayText,
            skipIndex: {
                name: "idx_entity_keys",
                type: SkipIndexType.BloomFilter,
                params: [0.01],
                granularity: 1,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        /*
         * Scalar per-entity-type key columns — denormalized single-value
         * siblings of `entityKeys`. Each holds the 16-hex key (see
         * Common/Utils/Telemetry/EntityKey) of the row's entity of that type,
         * or '' when the resource carries no such entity (non-Nullable String,
         * so old rows read the type default ''). Unlike the array column, a
         * scalar equality predicate is usable as an MV/sort key and gets a
         * cheaper bloom-filter probe; only the high-traffic keys
         * (service/host/k8s.pod) carry skip indexes. Stamped at ingest by the
         * same extractor that fills `entityKeys`; never part of identity.
         */
        const scalarEntityKeyColumns = [
            {
                key: "serviceEntityKey",
                title: "Service Entity Key",
                indexName: "idx_service_entity_key",
            },
            {
                key: "hostEntityKey",
                title: "Host Entity Key",
                indexName: "idx_host_entity_key",
            },
            {
                key: "k8sPodEntityKey",
                title: "Kubernetes Pod Entity Key",
                indexName: "idx_k8s_pod_entity_key",
            },
            { key: "k8sNodeEntityKey", title: "Kubernetes Node Entity Key" },
            { key: "k8sClusterEntityKey", title: "Kubernetes Cluster Entity Key" },
            { key: "containerEntityKey", title: "Container Entity Key" },
        ].map((def) => {
            return new AnalyticsTableColumn({
                key: def.key,
                title: def.title,
                description: "Scalar entity key for this entity type (see entityKeys); '' when the resource has no entity of this type.",
                required: true,
                defaultValue: "",
                type: TableColumnType.Text,
                codec: { codec: "ZSTD", level: 1 },
                skipIndex: def.indexName
                    ? {
                        name: def.indexName,
                        type: SkipIndexType.BloomFilter,
                        params: [0.01],
                        granularity: 1,
                    }
                    : undefined,
                accessControl: entityKeysColumn.accessControl,
            });
        });
        const eventsColumn = new AnalyticsTableColumn({
            key: "events",
            title: "Events",
            description: "Span Events",
            required: true,
            defaultValue: [],
            type: TableColumnType.JSONArray,
            codec: { codec: "ZSTD", level: 3 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const linksColumn = new AnalyticsTableColumn({
            key: "links",
            title: "Links",
            description: "Span Links",
            required: true,
            defaultValue: [],
            type: TableColumnType.JSON,
            codec: { codec: "ZSTD", level: 3 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const statusCodeColumn = new AnalyticsTableColumn({
            key: "statusCode",
            title: "Status Code",
            description: "Status Code",
            required: false,
            type: TableColumnType.Number,
            skipIndex: {
                name: "idx_status_code",
                type: SkipIndexType.Set,
                params: [5],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const statusMessageColumn = new AnalyticsTableColumn({
            key: "statusMessage",
            title: "Status Message",
            description: "Status Message",
            required: false,
            type: TableColumnType.Text,
            codec: { codec: "ZSTD", level: 1 },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const nameColumn = new AnalyticsTableColumn({
            key: "name",
            codec: { codec: "ZSTD", level: 3 },
            title: "Name",
            description: "Name of the span",
            required: false,
            type: TableColumnType.Text,
            skipIndex: {
                name: "idx_name",
                type: SkipIndexType.TokenBF,
                params: [10240, 3, 0],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const kindColumn = new AnalyticsTableColumn({
            key: "kind",
            isLowCardinality: true,
            title: "Kind",
            description: "Kind of the span",
            required: false,
            type: TableColumnType.Text,
            skipIndex: {
                name: "idx_kind",
                type: SkipIndexType.Set,
                params: [5],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const hasExceptionColumn = new AnalyticsTableColumn({
            key: "hasException",
            title: "Has Exception",
            description: "Whether this span contains an exception event, populated at ingest time for fast error filtering",
            required: true,
            defaultValue: false,
            type: TableColumnType.Boolean,
            skipIndex: {
                name: "idx_has_exception",
                type: SkipIndexType.Set,
                params: [2],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const isRootSpanColumn = new AnalyticsTableColumn({
            key: "isRootSpan",
            title: "Is Root Span",
            description: "Whether this span is a root span (has no parent), populated at ingest time for fast root-only filtering",
            required: true,
            defaultValue: false,
            type: TableColumnType.Boolean,
            skipIndex: {
                name: "idx_is_root_span",
                type: SkipIndexType.Set,
                params: [2],
                granularity: 4,
            },
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [],
            },
        });
        const retentionDateColumn = new AnalyticsTableColumn({
            key: "retentionDate",
            codec: [{ codec: "DoubleDelta" }, { codec: "ZSTD", level: 1 }],
            title: "Retention Date",
            description: "Date after which this row is eligible for TTL deletion, computed at ingest time as startTime + service.retainTelemetryDataForDays",
            required: true,
            type: TableColumnType.Date,
            defaultValue: undefined,
        });
        super({
            tableName: AnalyticsTableName.Span,
            tableEngine: AnalyticsTableEngine.MergeTree,
            singularName: "Span",
            pluralName: "Spans",
            crudApiPath: new Route("/span"),
            enableDocumentation: true,
            tableDescription: "OpenTelemetry distributed-tracing spans. Each span is one operation within a trace; query and aggregate them to analyze latency and errors.",
            accessControl: {
                read: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.Viewer,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.TelemetryViewer,
                    Permission.ReadTelemetryServiceTraces,
                ],
                create: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.CreateTelemetryServiceTraces,
                ],
                update: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.EditTelemetryServiceTraces,
                ],
                delete: [
                    Permission.ProjectOwner,
                    Permission.ProjectAdmin,
                    Permission.ProjectMember,
                    Permission.TelemetryAdmin,
                    Permission.TelemetryMember,
                    Permission.DeleteTelemetryServiceTraces,
                ],
            },
            tableColumns: [
                projectIdColumn,
                primaryEntityIdColumn,
                primaryEntityTypeColumn,
                startTimeColumn,
                endTimeColumn,
                startTimeUnixNanoColumn,
                durationUnixNanoColumn,
                endTimeUnixNanoColumn,
                traceIdColumn,
                spanIdColumn,
                parentSpanIdColumn,
                traceStateColumn,
                attributesColumn,
                attributeKeysColumn,
                entityKeysColumn,
                ...scalarEntityKeyColumns,
                eventsColumn,
                linksColumn,
                statusCodeColumn,
                statusMessageColumn,
                nameColumn,
                kindColumn,
                hasExceptionColumn,
                isRootSpanColumn,
                retentionDateColumn,
            ],
            projections: [
                {
                    name: "proj_agg_by_service",
                    query: "SELECT projectId, primaryEntityId, toStartOfMinute(startTime) AS minute, count() AS cnt, avg(durationUnixNano) AS avg_duration, quantile(0.99)(durationUnixNano) AS p99_duration GROUP BY projectId, primaryEntityId, minute",
                },
                {
                    name: "proj_trace_by_id",
                    query: "SELECT projectId, traceId, startTime, primaryEntityId, spanId, parentSpanId, name, durationUnixNano, statusCode, hasException ORDER BY (projectId, traceId, startTime)",
                },
                {
                    name: "proj_hist_by_minute",
                    query: "SELECT projectId, toStartOfMinute(startTime) AS minute, primaryEntityId, statusCode, isRootSpan, count() AS cnt GROUP BY projectId, minute, primaryEntityId, statusCode, isRootSpan",
                },
            ],
            sortKeys: ["projectId", "startTime", "primaryEntityId", "traceId"],
            primaryKeys: ["projectId", "startTime", "primaryEntityId", "traceId"],
            partitionKey: "toYYYYMMDD(startTime)",
            tableSettings: "ttl_only_drop_parts = 1, non_replicated_deduplication_window = 10000",
            ttlExpression: "retentionDate DELETE",
            defaultSortColumn: "startTime",
        });
    }
    get startTimeUnixNano() {
        return this.getColumnValue("startTimeUnixNano");
    }
    set startTimeUnixNano(v) {
        this.setColumnValue("startTimeUnixNano", v);
    }
    get durationUnixNano() {
        return this.getColumnValue("durationUnixNano");
    }
    set durationUnixNano(v) {
        this.setColumnValue("durationUnixNano", v);
    }
    get endTimeUnixNano() {
        return this.getColumnValue("endTimeUnixNano");
    }
    set endTimeUnixNano(v) {
        this.setColumnValue("endTimeUnixNano", v);
    }
    get name() {
        return this.getColumnValue("name");
    }
    set name(v) {
        this.setColumnValue("name", v);
    }
    get kind() {
        return this.getColumnValue("kind");
    }
    set kind(v) {
        this.setColumnValue("kind", v);
    }
    get projectId() {
        return this.getColumnValue("projectId");
    }
    set projectId(v) {
        this.setColumnValue("projectId", v);
    }
    get primaryEntityId() {
        return this.getColumnValue("primaryEntityId");
    }
    set primaryEntityId(v) {
        this.setColumnValue("primaryEntityId", v);
    }
    get primaryEntityType() {
        return this.getColumnValue("primaryEntityType");
    }
    set primaryEntityType(v) {
        this.setColumnValue("primaryEntityType", v);
    }
    get startTime() {
        return this.getColumnValue("startTime");
    }
    set startTime(v) {
        this.setColumnValue("startTime", v);
    }
    get endTime() {
        return this.getColumnValue("endTime");
    }
    set endTime(v) {
        this.setColumnValue("endTime", v);
    }
    get traceId() {
        return this.getColumnValue("traceId");
    }
    set traceId(v) {
        this.setColumnValue("traceId", v);
    }
    get spanId() {
        return this.getColumnValue("spanId");
    }
    set spanId(v) {
        this.setColumnValue("spanId", v);
    }
    get parentSpanId() {
        return this.getColumnValue("parentSpanId");
    }
    set parentSpanId(v) {
        this.setColumnValue("parentSpanId", v);
    }
    get traceState() {
        return this.getColumnValue("traceState");
    }
    set traceState(v) {
        this.setColumnValue("traceState", v);
    }
    get attributes() {
        return this.getColumnValue("attributes");
    }
    set attributes(v) {
        this.setColumnValue("attributes", v);
    }
    get attributeKeys() {
        return this.getColumnValue("attributeKeys");
    }
    set attributeKeys(v) {
        this.setColumnValue("attributeKeys", v);
    }
    get entityKeys() {
        return this.getColumnValue("entityKeys");
    }
    set entityKeys(v) {
        this.setColumnValue("entityKeys", v);
    }
    get events() {
        return this.getColumnValue("events");
    }
    set events(v) {
        this.setColumnValue("events", v);
    }
    get links() {
        return this.getColumnValue("links");
    }
    set links(v) {
        this.setColumnValue("links", v);
    }
    get statusCode() {
        return this.getColumnValue("statusCode");
    }
    set statusCode(v) {
        this.setColumnValue("statusCode", v);
    }
    get statusMessage() {
        return this.getColumnValue("statusMessage");
    }
    set statusMessage(v) {
        this.setColumnValue("statusMessage", v);
    }
    get hasException() {
        return this.getColumnValue("hasException");
    }
    set hasException(v) {
        this.setColumnValue("hasException", v);
    }
    get isRootSpan() {
        return this.getColumnValue("isRootSpan");
    }
    set isRootSpan(v) {
        this.setColumnValue("isRootSpan", v);
    }
    get retentionDate() {
        return this.getColumnValue("retentionDate");
    }
    set retentionDate(v) {
        this.setColumnValue("retentionDate", v);
    }
};
Span = __decorate([
    OperationalResource(),
    OwnedThrough("primaryEntityId", Service, { includeProjectScope: true }),
    __metadata("design:paramtypes", [])
], Span);
export default Span;
//# sourceMappingURL=Span.js.map