Compare commits
4 Commits
3a91cdee18
...
31c09d47cd
Author | SHA1 | Date |
---|---|---|
|
31c09d47cd | |
|
ca3cada076 | |
|
362b6701a6 | |
|
f962d29eda |
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/oauth-providers': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Optionally pass state as an option to oauth provider
|
|
@ -12,10 +12,11 @@ export function discordAuth(options: {
|
||||||
client_id?: string
|
client_id?: string
|
||||||
client_secret?: string
|
client_secret?: string
|
||||||
redirect_uri?: string
|
redirect_uri?: string
|
||||||
|
state?: string
|
||||||
}): MiddlewareHandler {
|
}): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
// Generate encoded "keys"
|
// Generate encoded "keys"
|
||||||
const newState = getRandomState()
|
const newState = options.state || getRandomState()
|
||||||
|
|
||||||
// Create new Auth instance
|
// Create new Auth instance
|
||||||
const auth = new AuthFlow({
|
const auth = new AuthFlow({
|
||||||
|
|
|
@ -13,9 +13,10 @@ export function facebookAuth(options: {
|
||||||
client_id?: string
|
client_id?: string
|
||||||
client_secret?: string
|
client_secret?: string
|
||||||
redirect_uri?: string
|
redirect_uri?: string
|
||||||
|
state?: string
|
||||||
}): MiddlewareHandler {
|
}): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
const newState = getRandomState()
|
const newState = options.state || getRandomState()
|
||||||
// Create new Auth instance
|
// Create new Auth instance
|
||||||
const auth = new AuthFlow({
|
const auth = new AuthFlow({
|
||||||
client_id: options.client_id || (env(c).FACEBOOK_ID as string),
|
client_id: options.client_id || (env(c).FACEBOOK_ID as string),
|
||||||
|
|
|
@ -13,9 +13,10 @@ export function githubAuth(options: {
|
||||||
scope?: GitHubScope[]
|
scope?: GitHubScope[]
|
||||||
oauthApp?: boolean
|
oauthApp?: boolean
|
||||||
redirect_uri?: string
|
redirect_uri?: string
|
||||||
|
state?: string
|
||||||
}): MiddlewareHandler {
|
}): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
const newState = getRandomState()
|
const newState = options.state || getRandomState()
|
||||||
// Create new Auth instance
|
// Create new Auth instance
|
||||||
const auth = new AuthFlow({
|
const auth = new AuthFlow({
|
||||||
client_id: options.client_id || (env(c).GITHUB_ID as string),
|
client_id: options.client_id || (env(c).GITHUB_ID as string),
|
||||||
|
|
|
@ -13,9 +13,10 @@ export function linkedinAuth(options: {
|
||||||
scope?: LinkedInScope[]
|
scope?: LinkedInScope[]
|
||||||
appAuth?: boolean
|
appAuth?: boolean
|
||||||
redirect_uri?: string
|
redirect_uri?: string
|
||||||
|
state?: string
|
||||||
}): MiddlewareHandler {
|
}): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
const newState = getRandomState()
|
const newState = options.state || getRandomState()
|
||||||
// Create new Auth instance
|
// Create new Auth instance
|
||||||
const auth = new AuthFlow({
|
const auth = new AuthFlow({
|
||||||
client_id: options.client_id || (env(c).LINKEDIN_ID as string),
|
client_id: options.client_id || (env(c).LINKEDIN_ID as string),
|
||||||
|
|
|
@ -14,10 +14,11 @@ export function xAuth(options: {
|
||||||
client_id?: string
|
client_id?: string
|
||||||
client_secret?: string
|
client_secret?: string
|
||||||
redirect_uri?: string
|
redirect_uri?: string
|
||||||
|
state?: string
|
||||||
}): MiddlewareHandler {
|
}): MiddlewareHandler {
|
||||||
return async (c, next) => {
|
return async (c, next) => {
|
||||||
// Generate encoded "keys"
|
// Generate encoded "keys"
|
||||||
const newState = getRandomState()
|
const newState = options.state || getRandomState()
|
||||||
const challenge = await getCodeChallenge()
|
const challenge = await getCodeChallenge()
|
||||||
|
|
||||||
const auth = new AuthFlow({
|
const auth = new AuthFlow({
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
# @hono/otel
|
# @hono/otel
|
||||||
|
|
||||||
|
## 0.1.1
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- [#1113](https://github.com/honojs/middleware/pull/1113) [`362b6701a6ee2843a51c1dfd5877a6164b7474fb`](https://github.com/honojs/middleware/commit/362b6701a6ee2843a51c1dfd5877a6164b7474fb) Thanks [@milohansen](https://github.com/milohansen)! - Use `req.routePath` to augment spans with the path that handled the request.
|
||||||
|
|
||||||
## 0.1.0
|
## 0.1.0
|
||||||
|
|
||||||
### Minor Changes
|
### Minor Changes
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@hono/otel",
|
"name": "@hono/otel",
|
||||||
"version": "0.1.0",
|
"version": "0.1.1",
|
||||||
"description": "OpenTelemetry middleware for Hono",
|
"description": "OpenTelemetry middleware for Hono",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
|
|
|
@ -26,6 +26,13 @@ describe('OpenTelemetry middleware', () => {
|
||||||
throw new Error('error message')
|
throw new Error('error message')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const subapp = new Hono()
|
||||||
|
subapp.get('/hello', (c) => c.text('Hello from subapp!'))
|
||||||
|
subapp.get('*', (c) => c.text('Fallthrough'))
|
||||||
|
|
||||||
|
// mount subapp
|
||||||
|
app.route('/subapp', subapp)
|
||||||
|
|
||||||
it('Should make a span', async () => {
|
it('Should make a span', async () => {
|
||||||
memoryExporter.reset()
|
memoryExporter.reset()
|
||||||
const response = await app.request('http://localhost/foo')
|
const response = await app.request('http://localhost/foo')
|
||||||
|
@ -71,4 +78,13 @@ describe('OpenTelemetry middleware', () => {
|
||||||
expect(span.name).toBe('GET /foo')
|
expect(span.name).toBe('GET /foo')
|
||||||
expect(span.attributes[ATTR_HTTP_ROUTE]).toBe('/foo')
|
expect(span.attributes[ATTR_HTTP_ROUTE]).toBe('/foo')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Issue #1112
|
||||||
|
it('Should set the correct span name for subapp', async () => {
|
||||||
|
memoryExporter.reset()
|
||||||
|
await app.request('http://localhost/subapp/hello')
|
||||||
|
const spans = memoryExporter.getFinishedSpans()
|
||||||
|
const [span] = spans
|
||||||
|
expect(span.name).toBe('GET /subapp/hello')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import type { TracerProvider } from '@opentelemetry/api'
|
import type { TracerProvider } from '@opentelemetry/api'
|
||||||
import { SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'
|
import { SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'
|
||||||
import {
|
import {
|
||||||
ATTR_HTTP_REQUEST_HEADER,
|
ATTR_HTTP_REQUEST_HEADER,
|
||||||
ATTR_HTTP_REQUEST_METHOD,
|
ATTR_HTTP_REQUEST_METHOD,
|
||||||
|
@ -10,17 +10,19 @@ import {
|
||||||
} from '@opentelemetry/semantic-conventions'
|
} from '@opentelemetry/semantic-conventions'
|
||||||
import type { Env, Input } from 'hono'
|
import type { Env, Input } from 'hono'
|
||||||
import { createMiddleware } from 'hono/factory'
|
import { createMiddleware } from 'hono/factory'
|
||||||
import metadata from '../package.json' with { type: 'json'}
|
import metadata from '../package.json' with { type: 'json' }
|
||||||
|
|
||||||
const PACKAGE_NAME = metadata.name
|
const PACKAGE_NAME = metadata.name
|
||||||
const PACKAGE_VERSION = metadata.version
|
const PACKAGE_VERSION = metadata.version
|
||||||
|
|
||||||
export type OtelOptions = {
|
export type OtelOptions =
|
||||||
augmentSpan?: false;
|
| {
|
||||||
tracerProvider?: TracerProvider
|
augmentSpan?: false
|
||||||
} | {
|
tracerProvider?: TracerProvider
|
||||||
augmentSpan: true;
|
}
|
||||||
}
|
| {
|
||||||
|
augmentSpan: true
|
||||||
|
}
|
||||||
|
|
||||||
export const otel = <E extends Env = any, P extends string = any, I extends Input = {}>(
|
export const otel = <E extends Env = any, P extends string = any, I extends Input = {}>(
|
||||||
options: OtelOptions = {}
|
options: OtelOptions = {}
|
||||||
|
@ -30,9 +32,9 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
|
||||||
const result = await next()
|
const result = await next()
|
||||||
const span = trace.getActiveSpan()
|
const span = trace.getActiveSpan()
|
||||||
if (span != null) {
|
if (span != null) {
|
||||||
const route = c.req.matchedRoutes[c.req.matchedRoutes.length - 1]
|
const routePath = c.req.routePath
|
||||||
span.setAttribute(ATTR_HTTP_ROUTE, route.path)
|
span.setAttribute(ATTR_HTTP_ROUTE, routePath)
|
||||||
span.updateName(`${c.req.method} ${route.path}`)
|
span.updateName(`${c.req.method} ${routePath}`)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
@ -40,15 +42,15 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
|
||||||
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider()
|
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider()
|
||||||
const tracer = tracerProvider.getTracer(PACKAGE_NAME, PACKAGE_VERSION)
|
const tracer = tracerProvider.getTracer(PACKAGE_NAME, PACKAGE_VERSION)
|
||||||
return createMiddleware<E, P, I>(async (c, next) => {
|
return createMiddleware<E, P, I>(async (c, next) => {
|
||||||
const route = c.req.matchedRoutes[c.req.matchedRoutes.length - 1]
|
const routePath = c.req.routePath
|
||||||
await tracer.startActiveSpan(
|
await tracer.startActiveSpan(
|
||||||
`${c.req.method} ${route.path}`,
|
`${c.req.method} ${c.req.routePath}`,
|
||||||
{
|
{
|
||||||
kind: SpanKind.SERVER,
|
kind: SpanKind.SERVER,
|
||||||
attributes: {
|
attributes: {
|
||||||
[ATTR_HTTP_REQUEST_METHOD]: c.req.method,
|
[ATTR_HTTP_REQUEST_METHOD]: c.req.method,
|
||||||
[ATTR_URL_FULL]: c.req.url,
|
[ATTR_URL_FULL]: c.req.url,
|
||||||
[ATTR_HTTP_ROUTE]: route.path,
|
[ATTR_HTTP_ROUTE]: routePath,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
async (span) => {
|
async (span) => {
|
||||||
|
@ -57,6 +59,10 @@ export const otel = <E extends Env = any, P extends string = any, I extends Inpu
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await next()
|
await next()
|
||||||
|
// Update the span name and route path now that we have the response
|
||||||
|
// because the route path may have changed
|
||||||
|
span.updateName(`${c.req.method} ${c.req.routePath}`)
|
||||||
|
span.setAttribute(ATTR_HTTP_ROUTE, c.req.routePath)
|
||||||
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, c.res.status)
|
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, c.res.status)
|
||||||
for (const [name, value] of c.res.headers.entries()) {
|
for (const [name, value] of c.res.headers.entries()) {
|
||||||
span.setAttribute(ATTR_HTTP_RESPONSE_HEADER(name), value)
|
span.setAttribute(ATTR_HTTP_RESPONSE_HEADER(name), value)
|
||||||
|
|
Loading…
Reference in New Issue