Resources

You are viewing the English version of this page because it has not yet been fully translated. Interested in helping out? See Contributing.

リソースは、リソース属性としてテレメトリーを生成するエンティティを表します。 たとえば、Kubernetes上のコンテナで実行されているテレメトリーを生成するプロセスは、プロセス名、ポッド名、ネームスペース、および場合によってはデプロイメント名を持ちます。 これらの4つの属性すべてをリソースに含まれることができます。

オブザーバビリティバックエンドでは、リソース情報を使用して興味深い動作をより詳細に調査できます。 たとえば、トレースまたはメトリクスデータがシステムのレイテンシーを示している場合、それを特定のコンテナ、ポッド、またはKubernetesデプロイメントに絞り込むことができます。

Resources should be assigned to a tracer, meter, and logger provider at its initialization, and are created much like attributes:

res := resource.NewWithAttributes(
    semconv.SchemaURL,
    semconv.ServiceNameKey.String("myService"),
    semconv.ServiceVersionKey.String("1.0.0"),
    semconv.ServiceInstanceIDKey.String("abcdef12345"),
)

provider := sdktrace.NewTracerProvider(
    ...
    sdktrace.WithResource(res),
)

Note the use of the semconv package to provide conventional names for resource attributes. This helps ensure that consumers of telemetry produced with these semantic conventions can easily discover relevant attributes and understand their meaning.

Resources can also be detected automatically through resource.Detector implementations. These Detectors may discover information about the currently running process, the operating system it is running on, the cloud provider hosting that operating system instance, or any number of other resource attributes.

res, err := resource.New(
	context.Background(),
	resource.WithFromEnv(),      // Discover and provide attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables.
	resource.WithTelemetrySDK(), // Discover and provide information about the OpenTelemetry SDK used.
	resource.WithProcess(),      // Discover and provide process information.
	resource.WithOS(),           // Discover and provide OS information.
	resource.WithContainer(),    // Discover and provide container information.
	resource.WithHost(),         // Discover and provide host information.
	resource.WithAttributes(attribute.String("foo", "bar")), // Add custom resource attributes.
	// resource.WithDetectors(thirdparty.Detector{}), // Bring your own external Detector implementation.
)
if errors.Is(err, resource.ErrPartialResource) || errors.Is(err, resource.ErrSchemaURLConflict) {
	log.Println(err) // Log non-fatal issues.
} else if err != nil {
	log.Fatalln(err) // The error may be fatal.
}

最終更新 May 16, 2024: [Go] Document logs (#4463) (12f31f62)