File size: 3,573 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { logs, type LogRecord } from '@opentelemetry/api-logs';
import type { Config } from '../config/config.js';
import { SERVICE_NAME } from './constants.js';
import { isTelemetrySdkInitialized } from './sdk.js';
import {
  ClearcutLogger,
  EventNames,
  type EventValue,
} from './clearcut-logger/clearcut-logger.js';
import { EventMetadataKey } from './clearcut-logger/event-metadata-key.js';
import { safeJsonStringify } from '../utils/safeJsonStringify.js';
import type {
  ConsecaPolicyGenerationEvent,
  ConsecaVerdictEvent,
} from './types.js';
import { debugLogger } from '../utils/debugLogger.js';

export function logConsecaPolicyGeneration(
  config: Config,
  event: ConsecaPolicyGenerationEvent,
): void {
  debugLogger.debug('Conseca Policy Generation Event:', event);
  const clearcutLogger = ClearcutLogger.getInstance(config);
  if (clearcutLogger) {
    const data: EventValue[] = [];

    if (config.getTelemetryLogPromptsEnabled()) {
      data.push(
        {
          gemini_cli_key: EventMetadataKey.CONSECA_USER_PROMPT,
          value: safeJsonStringify(event.user_prompt),
        },
        {
          gemini_cli_key: EventMetadataKey.CONSECA_TRUSTED_CONTENT,
          value: safeJsonStringify(event.trusted_content),
        },
        {
          gemini_cli_key: EventMetadataKey.CONSECA_GENERATED_POLICY,
          value: safeJsonStringify(event.policy),
        },
      );
    }

    if (event.error) {
      data.push({
        gemini_cli_key: EventMetadataKey.CONSECA_ERROR,
        value: event.error,
      });
    }

    clearcutLogger.enqueueLogEvent(
      clearcutLogger.createLogEvent(EventNames.CONSECA_POLICY_GENERATION, data),
    );
  }

  if (!isTelemetrySdkInitialized()) return;

  const logger = logs.getLogger(SERVICE_NAME);
  const logRecord: LogRecord = {
    body: event.toLogBody(),
    attributes: event.toOpenTelemetryAttributes(config),
  };
  logger.emit(logRecord);
}

export function logConsecaVerdict(
  config: Config,
  event: ConsecaVerdictEvent,
): void {
  debugLogger.debug('Conseca Verdict Event:', event);
  const clearcutLogger = ClearcutLogger.getInstance(config);
  if (clearcutLogger) {
    const data: EventValue[] = [
      {
        gemini_cli_key: EventMetadataKey.CONSECA_VERDICT_RESULT,
        value: safeJsonStringify(event.verdict),
      },
    ];

    if (config.getTelemetryLogPromptsEnabled()) {
      data.push(
        {
          gemini_cli_key: EventMetadataKey.CONSECA_USER_PROMPT,
          value: safeJsonStringify(event.user_prompt),
        },
        {
          gemini_cli_key: EventMetadataKey.CONSECA_GENERATED_POLICY,
          value: safeJsonStringify(event.policy),
        },
        {
          gemini_cli_key: EventMetadataKey.GEMINI_CLI_TOOL_CALL_NAME,
          value: safeJsonStringify(event.tool_call),
        },
        {
          gemini_cli_key: EventMetadataKey.CONSECA_VERDICT_RATIONALE,
          value: event.verdict_rationale,
        },
      );
    }

    if (event.error) {
      data.push({
        gemini_cli_key: EventMetadataKey.CONSECA_ERROR,
        value: event.error,
      });
    }

    clearcutLogger.enqueueLogEvent(
      clearcutLogger.createLogEvent(EventNames.CONSECA_VERDICT, data),
    );
  }

  if (!isTelemetrySdkInitialized()) return;

  const logger = logs.getLogger(SERVICE_NAME);
  const logRecord: LogRecord = {
    body: event.toLogBody(),
    attributes: event.toOpenTelemetryAttributes(config),
  };
  logger.emit(logRecord);
}