| [ Web Proxy ] |
| Viewing: https://docs.sourcegraph.com/code_monitoring | [Back] [Original] |
Supported on Enterprise plans.
Keep on top of events in your codebase. Watch your code with code monitors and trigger actions to run automatically in response to events.
Code monitors allow you to keep track of and get notified about changes in your code. Some use cases for code monitors include getting notifications for potential secrets, anti-patterns, or common typos committed to your codebase.
Here are some starting points for your first code monitor:
Watch for consumers of deprecated endpoints
SHELLf:\.tsx?$ patterntype:regexp fetch\(['"`]/deprecated-endpoint
If youre deprecating an API or an endpoint, you may find it useful to set up a code monitor watching for new consumers. As an example, the above query will surface fetch() calls to /deprecated-endpoint within TypeScript files. Replace /deprecated-endpoint with the actual path of the endpoint being deprecated.
Get notified when a file changes
SHELLpatterntype:regexp repo:^github\.com/sourcegraph/sourcegraph$ file:SourcegraphWebApp\.tsx$ type:diff
You may want to get notified when a given file is changed, regardless of the diff contents of the change: the above query will return all changes to the SourcegraphWebApp.tsx file on the github.com/sourcegraph/sourcegraph repo.
Get notified when a specific function call is added
SHELLrepo:^github\.com/sourcegraph/sourcegraph$ type:diff select:commit.diff.added Sprintf
You may want to monitor new additions of a specific function call, for example a deprecated function or a function that introduces a security concern. This query will notify you whenever a new addition of Sprintf is added to the sourcegraph/sourcegraph repository. This query selects all diff additions marked as "+". If a call of Sprintf is both added and removed from a file, this query will still notify due to the addition.
Code monitors are made up of two main elements: Triggers and Actions.
A trigger is an event which causes execution of an action. Currently, code monitoring supports one kind of trigger: "When new search results are detected" for a particular search query. When creating a code monitor, users will be asked to specify a query as part of the trigger.
Sourcegraph will run the search query over every new commit for the searched repositories, and when new results for the query are detected, a trigger event is emitted. In response to the trigger event, any actions attached to the code monitor will be executed.
A query used in a "When new search results are detected" trigger must be a type:commit or type:diff search. This allows Sourcegraph to detect new search results periodically.
An action is executed in response to a trigger event. Currently, code monitoring supports three different actions:
Prerequisite: Ensure email notifications are configured in site configuration.
TODO type:diff patternType:keyword.
(Note that type: and patternType: are required as part of the search query for code monitoring.)You should now see the TODO code monitor on the page, and you will receive email notifications whenever the trigger fires.
You can set up code monitors to send notifications about new matching search results to Slack channels.
Webhook notifications provide a way to execute custom responses to a code monitor notification. They are implemented as a POST request to a URL of your choice. The body of the request is defined by Sourcegraph, and contains all the information available about the cause of the notification.
A webhook receiver is a service that can accept an HTTP POST request with the contents of the webhook notification. The receiver must be reachable from the Sourcegraph cluster using the URL that is configured below.
The HTTP POST request sent to the receiver will have a JSON-encoded body with the following fields:
monitorDescription: The description of the monitor as configured in the UImonitorURL: A link to the monitor configuration pagequery: The query that generated resultsresults: The list of results that triggered this notification. Contains the following sub-fields
repository: The name of the repository the commit belongs tocommit: The commit hash for the matched commit.diff: The matching diff in unified diff format. Only set if the result is a diff match.matchedDiffRanges: The character ranges of diff that matched query. Only set if the result is a diff match.message: The matching commit message. Only set if the result is a commit match.matchedMessageRanges: The character ranges of message that matched query. Only set if the result is a commit match.To put it all together, a code monitor has a flow similar to the following:
A user creates a code monitor, which consists of:
Sourcegraph runs the query periodically over new commits. When new results are detected, a notification will be sent with the configured action. It will either contain a link to the search that provided new results, or if the "Include results" setting is enabled, it will include the result contents.
There are some best practices we recommend when creating code monitors.
Every code monitor has a name that will be shown wherever the monitor is referenced. In notification actions this name is likely to be the only information about the event, so its important for identifying what was triggered, but also has to be safe to expose in plain text emails.
Each code monitor action has the ability to include the result contents when sending a notification. This is often convenient because it lets you immediately see which results triggered the notification. However, because the result contents include the code that matched the search query, they may contain sensitive information. Care should be taken to only send result contents if the destination is secure.
For example, if sending the results to a Slack channel, every user that can view that channel will also be able to view the notification messages. The channel should be properly restricted to users who should be able to view that code.
Code monitors have been designed to be performant even for large Sourcegraph instances. There are no hard limits on the number of monitors or the volume of code monitored. However, depending on a number of factors such as the number of code monitors, the number of repos monitored, the frequency of commits, and the resources allocated to your instance, it's still possible to hit soft limits. If this happens, your code monitor will continue to work reliably, but it may execute more infrequently.
Learn about how Code Monitoring fits into Sourcegraph in the architecture overview.
| Web Proxy Viewer | New URL | Original Page |