# Microsoft SQL Server Collect SQL client spans in the calling application and instance statistics through the Collector `sqlserver` receiver. Contrib 0.160.0 marks metrics beta; optional query-event logs are development. Neither statistics nor query samples create a complete server-side trace of every statement. ## Application instrumentation Use the [Java agent](../languages/java.md) for supported JDBC calls, or the [.NET guide](../languages/dotnet.md) with instrumentation for the actual SqlClient package. Configure instrumentation before creating the application's clients. Retain your application service name and export to the configured OTLP endpoint. Execute a normal application read and look for its database client span. Avoid query parameter capture during initial verification. The metrics receiver uses a different account and does not change the application's connection string. ## Prepare a monitoring login Create a dedicated SQL Server login using your normal secret-management process. For an existing login named `otel_monitor`, an administrator grants: ```sql GRANT VIEW ANY DATABASE TO otel_monitor; -- SQL Server 2022 and later: GRANT VIEW SERVER PERFORMANCE STATE TO otel_monitor; -- On an older server, use VIEW SERVER STATE instead of the line above. ``` Do not grant `sysadmin`, database creation, or application write permissions. Opt-in per-index physical-statistics metrics additionally need `CONNECT ANY DATABASE` and `VIEW ANY DEFINITION`; leave those metrics disabled unless that broader access and scrape cost have been reviewed. The example uses direct SQL connections, suitable for a Collector on another host. Windows performance-counter collection is a different deployment mode and is not configured here. Azure SQL database/service permissions can differ from a self-managed SQL Server instance; validate that target separately. ## Add direct-connection metrics Set `SQLSERVER_MONITOR_DSN` through a secret store. Its URI form can be: `sqlserver://otel_monitor:ENCODED_PASSWORD@sqlserver.internal:1433?database=master&encrypt=true&TrustServerCertificate=false`. Percent-encode password characters for URI syntax, install the issuing CA in the Collector's trust store, and use the host name on the certificate. Merge this **partial fragment** into [common setup](../../common.md). Keep the existing pipelines. It requires Contrib's `sqlserver` receiver and `resource` processor. ```yaml receivers: sqlserver/app: datasource: ${env:SQLSERVER_MONITOR_DSN} collection_interval: 30s processors: resource/sqlserver: attributes: - key: service.name value: sqlserver-app action: upsert - key: deployment.environment.name value: production action: upsert service: pipelines: metrics/sqlserver: receivers: [sqlserver/app] processors: [memory_limiter, resource/sqlserver, batch] exporters: [otlp_http/corridora] ``` Do not combine `datasource` with the alternative `username`, `password`, `server`, and `port` connection fields. Keep this pipeline separate from application OTLP so that its resource processor changes only server metric identity. ## Verify Connect as the monitor with your SQL client, using encrypted certificate-verified transport, and run: ```sql SELECT TOP (5) object_name, counter_name, cntr_value FROM sys.dm_os_performance_counters; ``` Expect rows rather than a permission error. Validate and run the Collector, then inspect `sqlserver.batch.request.rate` under service `sqlserver-app` after two intervals. Run a normal query through the instrumented application and check its separate trace. A quiet server may legitimately report zero rates. For connection failures, check SQL authentication, the TCP listener, certificate trust, and version-specific state permissions. Do not “fix” a certificate error by copying a configuration that sets `TrustServerCertificate=true`. Query samples and top queries are disabled in this example. Enabling them requires receiver event options and a separate **logs pipeline** using `sqlserver/app`, plus a review of exposed SQL text and plans. Server error-log files use the [log collection route](other-services.md#collect-an-existing-endpoint-or-log-file). Source basis: SQL Server receiver README and metadata, Contrib 0.160.0; Microsoft dynamic-management-view permissions and go-mssqldb connection-string documentation. Reviewed 2026-09-28. Finish with [verification](../../verification.md).