Instrumentation and telemetry in functions(函数中的仪表化与遥测技术)¶
It is possible to emit certain types of telemetry from your functions to allow for monitoring and debugging of production workflows.
To learn how to view telemetry emitted by your functions, see our AIP observability documentation.
Supported telemetry types¶
The following table provides an overview of the types of telemetry supported by each function language. Note that a single span over the total execution duration of the function, along with a single request log, is automatically created for all function types.
| Language | Logs | Spans | Metrics |
|---|---|---|---|
| TypeScript v1 | Yes | Only product-defined spans[1] | Only product-defined metrics[2] |
| TypeScript v2 | Yes | Yes[3] | Only product-defined metrics[2] |
| Python | Yes | Yes[3] | Only product-defined metrics[2] |
[1] Product-defined spans in TypeScript v1 functions include operations like object loads and query executions.
[2] Foundry records the total execution duration for all types of functions.
[3] TypeScript v2 and Python functions automatically instrument all outbound network requests, but custom spans can also be added.
Logs¶
You can emit custom logs from functions and view them retroactively. The following examples demonstrate how to emit logs from TypeScript v1, TypeScript v2, and Python functions.
In TypeScript v2 functions, Foundry will set up the OpenTelemetry SDK's global logger provider, and you will be able to retrieve a logger from it. If you want to use third-party libraries for logging, you must configure them to emit logs through a logger obtained from the global logger provider.
``typescript tab="TypeScript v1"
export class MyFunctions {
@Function()
public myFunction(name: string): string {
console.log(This is a custom log line ${name}.);
returnHello, ${name}!`;
}
}
```typescript tab="TypeScript v2"
import { logs } from "@opentelemetry/api-logs";
const logger = logs.getLogger("my-function");
export default function myFunction(name: string): string {
logger.emit({
attributes: { LOG_MESSAGE: "This is a custom log line." },
body: { name },
});
return `Hello, ${name}!`;
}
```python tab="Python" import logging
from functions.api import function
logger = logging.getLogger(name)
@function def my_function(name: str) -> str: logger.info("This is a custom log line.") return f"Hello, {name}!"
### Spans
You can also create custom spans in TypeScript v2 and Python functions to track the duration of specific operations. The following example demonstrates how to create a custom span.
In TypeScript v2 and Python functions, Foundry will set up the OpenTelemetry SDK's global tracer provider, and you will be able to retrieve a tracer from it. If you want to use third-party libraries for tracing, you must configure them to emit traces through a tracer obtained from the global tracer provider.
```typescript tab="TypeScript v2"
import { trace } from "@opentelemetry/api";
import { Integer } from "@osdk/functions";
const tracer = trace.getTracer("my-function");
export default function sqrt(n: Integer): Integer {
const sqrt = tracer.startActiveSpan("my-custom-span", (span) => {
try {
return Math.sqrt(n);
} finally {
span.end();
}
});
return sqrt;
}
```python tab="Python" import math
from functions.api import function
from opentelemetry import trace
tracer = trace.get_tracer(name)
@function def sqrt(n: int) -> int: with tracer.start_as_current_span("my-custom-span"): return math.sqrt(n)
---
# 中文翻译
---
# 函数中的仪表化与遥测技术
您可以从函数中发出特定类型的遥测数据,以便监控和调试生产工作流。
如需了解如何查看函数发出的遥测数据,请参阅我们的 [AIP 可观测性文档](https://palantir.com/docs/foundry/aip-observability/overview/)。
## 支持的遥测类型
下表概述了每种函数语言支持的遥测类型。请注意,所有函数类型都会自动生成一个覆盖函数总执行时长的单一 span(跨度)以及一条单一请求日志。
| 语言 | 日志 | Spans(跨度) | Metrics(指标) |
| -------------- | ---- | -------------------------------- | ---------------------------------- |
| TypeScript v1 | 是 | 仅产品定义的 spans\[1] | 仅产品定义的 metrics\[2] |
| TypeScript v2 | 是 | 是\[3] | 仅产品定义的 metrics\[2] |
| Python | 是 | 是\[3] | 仅产品定义的 metrics\[2] |
\[1] TypeScript v1 函数中的产品定义 spans 包括对象加载和查询执行等操作。
\[2] Foundry 会记录所有类型函数的总执行时长。
\[3] TypeScript v2 和 Python 函数会自动对所有出站网络请求进行仪表化,但也可以添加自定义 spans。
### 日志
您可以从函数中发出自定义日志,并事后进行查看。以下示例展示了如何从 TypeScript v1、TypeScript v2 和 Python 函数中发出日志。
在 TypeScript v2 函数中,Foundry 会设置 OpenTelemetry SDK 的全局日志提供程序,您可以从该提供程序中获取一个日志记录器。如果您想使用第三方库进行日志记录,则必须将其配置为通过从全局日志提供程序获取的日志记录器来发出日志。
```typescript tab="TypeScript v1"
export class MyFunctions {
@Function()
public myFunction(name: string): string {
console.log(`这是一条自定义日志行 ${name}。`);
return `你好,${name}!`;
}
}
```typescript tab="TypeScript v2" import { logs } from "@opentelemetry/api-logs";
const logger = logs.getLogger("my-function");
export default function myFunction(name: string): string { logger.emit({ attributes: { LOG_MESSAGE: "这是一条自定义日志行。" }, body: { name }, });
return `你好,${name}!`;
}
python tab="Python"
import logging
from functions.api import function
logger = logging.getLogger(name)
@function
def my_function(name: str) -> str:
logger.info("这是一条自定义日志行。")
return f"你好,{name}!"
```
Spans(跨度)¶
您还可以在 TypeScript v2 和 Python 函数中创建自定义 spans,以跟踪特定操作的持续时间。以下示例展示了如何创建自定义 span。
在 TypeScript v2 和 Python 函数中,Foundry 会设置 OpenTelemetry SDK 的全局追踪提供程序,您可以从该提供程序中获取一个追踪器。如果您想使用第三方库进行追踪,则必须将其配置为通过从全局追踪提供程序获取的追踪器来发出追踪数据。
```typescript tab="TypeScript v2" import { trace } from "@opentelemetry/api"; import { Integer } from "@osdk/functions";
const tracer = trace.getTracer("my-function");
export default function sqrt(n: Integer): Integer { const sqrt = tracer.startActiveSpan("my-custom-span", (span) => { try { return Math.sqrt(n); } finally { span.end(); } });
return sqrt;
}
python tab="Python"
import math
from functions.api import function
from opentelemetry import trace
tracer = trace.get_tracer(name)
@function
def sqrt(n: int) -> int:
with tracer.start_as_current_span("my-custom-span"):
return math.sqrt(n)
```