Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fastly) provide service version and other contextual information #608

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bundler/EdgeBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class EdgeBundler extends WebpackBundler {
'aws-sdk',
'@google-cloud/secret-manager',
'@google-cloud/storage',
'fastly:env',
].reduce((obj, ext) => {
// this makes webpack to ignore the module and just leave it as normal require.
// eslint-disable-next-line no-param-reassign
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/ComputeAtEdgeDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ service_id = ""
this.init();

await this._fastly.transact(async (version) => {
this.log.debug('--: uploading package to fastly');
this.log.debug('--: uploading package to fastly, service version', version);
await this._fastly.writePackage(version, buf);

this.log.debug('--: creating secrets dictionary');
Expand Down
45 changes: 36 additions & 9 deletions src/template/fastly-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,38 @@
/* eslint-env serviceworker */
/* global Dictionary */

export function getEnvInfo(req, env) {
const serviceVersion = env('FASTLY_SERVICE_VERSION');
const requestId = env('FASTLY_TRACE_ID');
const region = env('FASTLY_POP');
const functionName = env('FASTLY_SERVICE_ID');
const functionFQN = `${env('FASTLY_CUSTOMER_ID')}-${functionName}-${serviceVersion}`;
const txId = req.headers.get('x-transaction-id') ?? env('FASTLY_TRACE_ID');

console.debug('Env info sv: ', serviceVersion, ' reqId: ', requestId, ' region: ', region, ' functionName: ', functionName, ' functionFQN: ', functionFQN, ' txId: ', txId);

return {
functionFQN,
functionName,
region,
requestId,
serviceVersion,
txId,
};
}

async function getEnvironmentInfo(req) {
// The fastly:env import will be available in the fastly c@e environment
/* eslint-disable-next-line import/no-unresolved */
const mod = await import('fastly:env');
return getEnvInfo(req, mod.env);
}

async function handler(event) {
try {
const { request } = event;
const env = await getEnvironmentInfo(request);

console.log('Fastly Adapter is here');
let packageParams;
// eslint-disable-next-line import/no-unresolved,global-require
Expand All @@ -26,20 +55,20 @@ async function handler(event) {
},
runtime: {
name: 'compute-at-edge',
// region: request.cf.colo,
region: env.region,
},
func: {
name: null,
name: env.functionName,
package: null,
version: null,
fqn: null,
version: env.serviceVersion,
fqn: env.functionFQN,
app: null,
},
invocation: {
id: null,
deadline: null,
transactionId: null,
requestId: null,
transactionId: env.txId,
requestId: env.requestId,
},
env: new Proxy(new Dictionary('secrets'), {
get: (target, prop) => {
Expand Down Expand Up @@ -87,13 +116,11 @@ async function handler(event) {
}
}

function fastly() {
export default function fastly() {
console.log('checking for fastly environment');
/* eslint-disable-next-line no-undef */
if (CacheOverride) {
return handler;
}
return false;
}

module.exports = fastly;
4 changes: 3 additions & 1 deletion src/template/serviceworker-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
* governing permissions and limitations under the License.
*/
/* eslint-env serviceworker */
const fastly = require('./fastly-adapter.js');

import fastly from './fastly-adapter.js';

const cloudflare = require('./cloudflare-adapter.js');

/* eslint-disable no-restricted-globals */
Expand Down
5 changes: 4 additions & 1 deletion test/computeatedge.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('Fastly Compute@Edge Integration Test', () => {
});

it('Deploy a pure action to Compute@Edge', async () => {
const serviceID = '1yv1Wl7NQCFmNBkW4L8htc';

await fse.copy(path.resolve(__rootdir, 'test', 'fixtures', 'edge-action'), testRoot);
process.chdir(testRoot); // need to change .cwd() for yargs to pickup `wsk` in package.json
const builder = new CLI()
Expand All @@ -44,7 +46,7 @@ describe('Fastly Compute@Edge Integration Test', () => {
'--target', 'c@e',
'--arch', 'node', // TODO: make obsolete
'--arch', 'edge',
'--compute-service-id', '1yv1Wl7NQCFmNBkW4L8htc',
'--compute-service-id', serviceID,
'--compute-test-domain', 'possibly-working-sawfish',
'--package.params', 'HEY=ho',
'--package.params', 'ZIP=zap',
Expand All @@ -61,5 +63,6 @@ describe('Fastly Compute@Edge Integration Test', () => {
assert.ok(res);
const out = builder.cfg._logger.output;
assert.ok(out.indexOf('possibly-working-sawfish.edgecompute.app') > 0, out);
assert.ok(out.indexOf(`(${serviceID}) ok:`) > 0, `The function output should include the service ID: ${out}`);
}).timeout(10000000);
});
54 changes: 54 additions & 0 deletions test/fastly-adapter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

import assert from 'assert';
import { getEnvInfo } from '../src/template/fastly-adapter.js';

describe('Fastly Adapter Test', () => {
it('Captures the environment', () => {
const headers = new Map();
const req = { headers };
const env = (envvar) => {
switch (envvar) {
case 'FASTLY_CUSTOMER_ID': return 'cust1';
case 'FASTLY_POP': return 'fpop';
case 'FASTLY_SERVICE_ID': return 'sid999';
case 'FASTLY_SERVICE_VERSION': return '1234';
case 'FASTLY_TRACE_ID': return 'trace-id';
default: return undefined;
}
};

const info = getEnvInfo(req, env);

assert.equal(info.functionFQN, 'cust1-sid999-1234');
assert.equal(info.functionName, 'sid999');
assert.equal(info.region, 'fpop');
assert.equal(info.requestId, 'trace-id');
assert.equal(info.serviceVersion, '1234');
assert.equal(info.txId, 'trace-id');
});

it('Takes the txid from the request headers', () => {
const headers = new Map();
headers.set('foo', 'bar');
headers.set('x-transaction-id', 'tx7');
const req = { headers };
const env = (_) => 'something';

const info = getEnvInfo(req, env);

assert.equal(info.txId, 'tx7');
});
});
2 changes: 1 addition & 1 deletion test/fixtures/edge-action/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ module.exports.main = async function main(req, context) {
backend: 'httpbin.org',
});
console.log(await backendresponse.text());
return new Response(`ok: ${await context.env.HEY} ${await context.env.FOO} – ${backendresponse.status}`);
return new Response(`(${context?.func?.name}) ok: ${await context.env.HEY} ${await context.env.FOO} – ${backendresponse.status}`);
};