Skip to main content

langfuse_client_base/models/
observation.rs

1/*
2 * server
3 *
4 * ## Real-time data  The only real-time path to get tracing data is to ingest via OpenTelemetry (`POST /api/public/otel/v1/traces`) and retrieve via Observations API v2 (`GET /api/public/v2/observations`) and Metrics API v2 (`GET /api/public/v2/metrics`). Other public API write and read endpoints can delay data by about 10 minutes. Direct OpenTelemetry exporters must send the `x-langfuse-ingestion-version: 4` header; current Python and JS SDKs already do. See the [Public API docs](https://langfuse.com/docs/api-and-data-platform/features/public-api) and [real-time ingestion](https://langfuse.com/integrations/native/opentelemetry.md#real-time-ingestion).  ## Authentication  Authenticate with the API using [Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication), get API keys in the project settings:  - username: Langfuse Public Key - password: Langfuse Secret Key  ## Exports  - OpenAPI spec: https://cloud.langfuse.com/generated/api/openapi.yml - Self-hosted deployments: interactive API reference at `/api/docs` and OpenAPI spec at `/api/openapi.yaml`
5 *
6 * The version of the OpenAPI document:
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use crate::models;
12use serde::{Deserialize, Serialize};
13
14#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
15pub struct Observation {
16    /// The unique identifier of the observation
17    #[serde(rename = "id")]
18    pub id: String,
19    /// The trace ID associated with the observation
20    #[serde(rename = "traceId", deserialize_with = "Option::deserialize")]
21    pub trace_id: Option<String>,
22    /// The type of the observation
23    #[serde(rename = "type")]
24    pub r#type: String,
25    /// The name of the observation
26    #[serde(rename = "name", deserialize_with = "Option::deserialize")]
27    pub name: Option<String>,
28    /// The start time of the observation
29    #[serde(rename = "startTime")]
30    pub start_time: chrono::DateTime<chrono::FixedOffset>,
31    /// The end time of the observation.
32    #[serde(rename = "endTime", deserialize_with = "Option::deserialize")]
33    pub end_time: Option<chrono::DateTime<chrono::FixedOffset>>,
34    /// The completion start time of the observation
35    #[serde(
36        rename = "completionStartTime",
37        deserialize_with = "Option::deserialize"
38    )]
39    pub completion_start_time: Option<chrono::DateTime<chrono::FixedOffset>>,
40    /// The model used for the observation
41    #[serde(rename = "model", deserialize_with = "Option::deserialize")]
42    pub model: Option<String>,
43    /// The parameters of the model used for the observation
44    #[serde(rename = "modelParameters", deserialize_with = "Option::deserialize")]
45    pub model_parameters: Option<serde_json::Value>,
46    /// The input data of the observation
47    #[serde(rename = "input", deserialize_with = "Option::deserialize")]
48    pub input: Option<serde_json::Value>,
49    /// The version of the observation
50    #[serde(rename = "version", deserialize_with = "Option::deserialize")]
51    pub version: Option<String>,
52    /// Additional metadata of the observation
53    #[serde(rename = "metadata")]
54    pub metadata: std::collections::HashMap<String, serde_json::Value>,
55    /// The output data of the observation
56    #[serde(rename = "output", deserialize_with = "Option::deserialize")]
57    pub output: Option<serde_json::Value>,
58    #[serde(rename = "usage")]
59    pub usage: Box<models::Usage>,
60    #[serde(rename = "level")]
61    pub level: models::ObservationLevel,
62    /// The status message of the observation
63    #[serde(rename = "statusMessage", deserialize_with = "Option::deserialize")]
64    pub status_message: Option<String>,
65    /// The parent observation ID
66    #[serde(
67        rename = "parentObservationId",
68        deserialize_with = "Option::deserialize"
69    )]
70    pub parent_observation_id: Option<String>,
71    /// The prompt ID associated with the observation
72    #[serde(rename = "promptId", deserialize_with = "Option::deserialize")]
73    pub prompt_id: Option<String>,
74    /// The usage details of the observation. Key is the name of the usage metric, value is the number of units consumed. The total key is the sum of all (non-total) usage metrics or the total value ingested.
75    #[serde(rename = "usageDetails")]
76    pub usage_details: std::collections::HashMap<String, i32>,
77    /// The cost details of the observation. Key is the name of the cost metric, value is the cost in USD. The total key is the sum of all (non-total) cost metrics or the total value ingested.
78    #[serde(rename = "costDetails")]
79    pub cost_details: std::collections::HashMap<String, f64>,
80    /// The environment from which this observation originated. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'.
81    #[serde(rename = "environment")]
82    pub environment: String,
83}
84
85impl Observation {
86    pub fn new(
87        id: String,
88        trace_id: Option<String>,
89        r#type: String,
90        name: Option<String>,
91        start_time: chrono::DateTime<chrono::FixedOffset>,
92        end_time: Option<chrono::DateTime<chrono::FixedOffset>>,
93        completion_start_time: Option<chrono::DateTime<chrono::FixedOffset>>,
94        model: Option<String>,
95        model_parameters: Option<serde_json::Value>,
96        input: Option<serde_json::Value>,
97        version: Option<String>,
98        metadata: std::collections::HashMap<String, serde_json::Value>,
99        output: Option<serde_json::Value>,
100        usage: models::Usage,
101        level: models::ObservationLevel,
102        status_message: Option<String>,
103        parent_observation_id: Option<String>,
104        prompt_id: Option<String>,
105        usage_details: std::collections::HashMap<String, i32>,
106        cost_details: std::collections::HashMap<String, f64>,
107        environment: String,
108    ) -> Observation {
109        Observation {
110            id,
111            trace_id,
112            r#type,
113            name,
114            start_time,
115            end_time,
116            completion_start_time,
117            model,
118            model_parameters,
119            input,
120            version,
121            metadata,
122            output,
123            usage: Box::new(usage),
124            level,
125            status_message,
126            parent_observation_id,
127            prompt_id,
128            usage_details,
129            cost_details,
130            environment,
131        }
132    }
133}