# Enabling API activity diagnostics logs with APIM

# Enable diagnostics logs on APIM

  1. Enable diagnostics logs on APIM

Ensure you have enabled diagnostics logs on APIM resource.

alt text

alt text

# Enable Azure Monitor logs to Information level on APIM

Enable Azure diagnostic logs to Information level and change payload to 8192 bytes.

alt text

# Review the logs on Log Analytics workspace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ApiManagementGatewayLogs
| where tolower(OperationId) in ('completions_create','chatcompletions_create')
| where ResponseCode == '200'
| extend modelkey = substring(parse_json(BackendResponseBody)['model'], 0, indexof(parse_json(BackendResponseBody)['model'], '-', 0, -1, 2))
| extend model = tostring(parse_json(BackendResponseBody)['model'])
| extend prompttokens = parse_json(parse_json(BackendResponseBody)['usage'])['prompt_tokens']
| extend completiontokens = parse_json(parse_json(BackendResponseBody)['usage'])['completion_tokens']
| extend totaltokens = parse_json(parse_json(BackendResponseBody)['usage'])['total_tokens']
| extend ip = CallerIpAddress
| where model != ''
| summarize
sum(todecimal(prompttokens)),
sum(todecimal(completiontokens)),
sum(todecimal(totaltokens)),
avg(todecimal(totaltokens))
by ip, model

alt text

alt text


# Reference

  • Tutorial: Monitor published APIs
  • Enterprise Azure OpenAI