Skip to content

Instantly share code, notes, and snippets.

View wu-sheng's full-sized avatar
🌍
Working from home, travel globally

吴晟 Wu Sheng wu-sheng

🌍
Working from home, travel globally
View GitHub Profile
@wu-sheng
wu-sheng / TracerLoader.java
Created December 21, 2016 08:00
ServiceLoader of OT tracer
package io.opentracing.contrib;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;
import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
@wu-sheng
wu-sheng / TraceIdGenerator.java
Created December 26, 2016 02:53
sky-walking TraceIdGenerator
public final class TraceIdGenerator {
private static final ThreadLocal<Integer> ThreadTraceIdSequence = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 0;
}
};
private static final int PROCESS_UUID;
@wu-sheng
wu-sheng / TraceAndSpanIdGenerator.java
Created December 28, 2016 03:15
TraceAndSpanIdGenerator From nike.wingtips
/**
* Code from
* https://github.com/Nike-Inc/wingtips/blob/38d8ce83207ffee3d638bf7466b8f7058bb36498/wingtips-core/src/main/java/com/nike/wingtips/TraceAndSpanIdGenerator.java#L60
* Thanks to @adriancole
*/
public static long generate64BitRandomLong() {
byte[] random8Bytes = new byte[8];
random.nextBytes(random8Bytes);
@wu-sheng
wu-sheng / Random.Long_TraceId_Generation_banchmark.md
Last active December 28, 2016 03:24
TraceId Generation by Random.Long
  • My laptop benchmarks normal random long (including work to create the java object representing a context) is something like 40 per microsecond
  • Desktop: 2.8Ghz macbook pro
  • Using threadlocalrandom
@wu-sheng
wu-sheng / Transaction.java
Created December 28, 2016 03:43
Transaction Trace
/**
* Created by wusheng on 2016/12/21.
*/
public class Transaction {
// 当前线程事务GUID
private java.lang.String traceId;
// 相关性上级GUID
private java.lang.String refTraceId;
// 全局 GUID
private java.lang.String tripId;
@wu-sheng
wu-sheng / AgentBuilder.transform.java
Created December 29, 2016 02:59
byte-buddy agentBuilder.transform
/**
* https://github.com/stagemonitor/stagemonitor/blob/80793ac6c582944c9078b420138dda4d23517a69/stagemonitor-core/src/main/java/org/stagemonitor/core/instrument/AgentAttacher.java#L149-L166
*/
private static ClassFileTransformer initByteBuddyClassFileTransformer(AutoEvictingCachingBinaryLocator binaryLocator) {
AgentBuilder agentBuilder = createAgentBuilder(binaryLocator);
for (StagemonitorByteBuddyTransformer transformer : getStagemonitorByteBuddyTransformers()) {
agentBuilder = agentBuilder
.type(transformer.getMatcher())
.transform(transformer.getTransformer())
.asDecorator();
//Type.java
public enum Type { A, B, C, D, E, F, G, H, I, J };
// Base.java
public abstract class Base {
int i = 1;
final Type type;
public Base(Type type) {
@wu-sheng
wu-sheng / trace.proto
Created January 12, 2017 08:39
protobuf specification for google tracing
// Copyright 2016 Google Inc.
//
// Licensed 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,
@wu-sheng
wu-sheng / PhysicalCores.java
Created February 8, 2017 09:17
Read the number of "physical" hardware threads available
package veddan.physicalcores;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
@wu-sheng
wu-sheng / BootstrapAgent.java
Created January 17, 2018 03:09 — forked from raphw/BootstrapAgent.java
An example agent that intercepts a method of the bootstrap class loader.
package net.bytebuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;